码迷,mamicode.com
首页 > 其他好文 > 详细

hdu 3717 Rescue 二分加队列优化(技巧)

时间:2015-08-19 16:47:50      阅读:120      评论:0      收藏:0      [点我收藏+]

标签:

Rescue

Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 814    Accepted Submission(s): 198


Problem Description
The princess is trapped in a magic place. In this place, there are N magic stones. In order to rescue the princess, you should destroy all the stones. The N stones are in a straight line. We number them as s1, s2, ... sn from left to right. Each stone has a magic strength m1, m2, ... mn. You have a powerful skill that can do some damage to the stones. To release the skill, you should stand to the right of some stone (si). Then you throw a power ball towards left. Initially, this ball has a power of p. When it hits a stone, it will do some damage to the stone and its power will be decreased, and the ball will continue to fly left to the next stone if its power is still positive. Formally, if you stand to the right of si and the power ball‘s initial power is p, then the ball will do Max(0, p - (i - j) * (i - j)) damage to sj, for each j <= i. So from this formula, we can see that the damage to stone sj is only determined by the initial power of the ball and the number of stones between si and sj. A stone is destroyed if the damage you do is larger than its magic strength. Note that even if a stone is destroyed, it will not disappear; your magic ball will do damage to it and the power will be decreased by that stone. You are not strong enough so that you can release at most k magic balls. It will cost a lot of energy if the power of the magic ball is too high. So what is the minimum value of p with which you can destroy all the magic stones, with no more than k magic balls? You can choose where to release each magic ball as your will, and the power of the ball must be a positive integer.
 

Input
The first line is the number of cases T (T ≤ 100). For each case, the first line gives two integers n, k (1 ≤ n ≤ 50000, 1 ≤ k ≤ 100000). The second line are n integers, giving m1, m2, ... mn (1 ≤ m ≤ 109).
 

Output
Print minimum possible p in a line.
 

Sample Input
2 1 1 1 3 1 1 4 5
 

Sample Output
2 6
 

题意:告诉n个石头,然后现在要用至多k个魔法球把石头全部消灭,问每个魔法球的魔力值至少要是多少。每次在消灭石头的时候,可以选择站在一个石头的右侧,然后向左边扔一个魔法球,比如站在第i个石头右侧,那么对于第j个石头,会带来max( 0,p-(i-j)*(i-j) )点伤害,只有当一个石头的伤害值小于0的时候它才算完全被消灭。消灭后它不会消失,还会在那个位置

思路:要求最小值,那么直接二分一个魔法值,然后去判断一下能否满足条件。可以在任意位置扔,可以扔任意多个(总和不超过k)

有一点要知道的是,从右向左,魔力值每次减少0  1  3  5  7  9 。。。。

#include <iostream>
#include <stdio.h>
#include <string>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>

using namespace std;
typedef long long ll;
#define INF (1ll<<60)

int n,k;
int a[55555];
queue<int>q;

int fun(ll x)
{
    ll dis=0;
    ll sum=0;//sum每次记录到达i位置时前面所有球去掉损失后的魔力值总和
    ll ff=0;
    int ans=0;//ans用来记录总共扔出的球的个数

    while(!q.empty()) q.pop();

    for(int i=n;i>=1;i--)
    {
        while(!q.empty())
        {
            ll tmp=q.front();
            if((tmp-i)*(tmp-i)<=x) break;

            sum-=x-(tmp-i-1)*(tmp-i-1);
            dis-=(tmp-i-1);
            ff--;
            q.pop();
        }
        sum-=2*dis+ff;
        dis+=ff;

        while(sum<a[i]+1)
        {
            if(ans>=k) return 0;
            ans++;
            q.push(i);
            sum+=x;
            ff++;
        }
    }
    return 1;
}

int main()
{
    int T;
    scanf("%d",&T);
    for(int i=1;i<=T;i++)
    {
        scanf("%d%d",&n,&k);
        for(int i=1;i<=n;i++)
            scanf("%d",&a[i]);

        ll le=1,ri=INF;
        ll mid;
        while(le<ri)
        {
            mid=(le+ri)/2;
            if(fun(mid)) ri=mid;
            else le=mid+1;
        }

        printf("%lld\n",ri);
    }
    return 0;
}


版权声明:本文为博主原创文章,未经博主允许不得转载。

hdu 3717 Rescue 二分加队列优化(技巧)

标签:

原文地址:http://blog.csdn.net/wust_zjx/article/details/47781561

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!