HDU 3717 Rescue sub-plus queue optimization (TIPS)

Source: Internet
Author: User

RescueTime limit:6000/3000 MS (java/others) Memory limit:65536/65536 K (java/others)
Total submission (s): 814 Accepted Submission (s): 198


Problem DescriptionThe Princess is trapped in a magic place. In this place, there is N magic stones. In order to rescue the princess, you should destroy all the stones. The N stones is in a straight line. We number them as S1, S2, ... Each stone has a magic strength M1, M2, ... mn. You had a powerful skill that can does some damage to the stones. To release the skill, your 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 would do some damage to the stone and its power would be decreased, and the ball would continue to f Ly left to the next stone if it power is still positive. Formally, if you stand to the right of Si and the Power Ball's initial power is p and then the ball would do Max (0, P-( I-J) * (I-J)) damage to SJ, for each J <= I. With the formula, we can see that the damage to stone SJ are 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 your do are larger than its magic strength. Note that even if a stone is destroyed, it'll not disappear; Your magic ball would do damage to it and the power would be decreased by that stone. You is not a strong enough so it can release at the most K magic balls. It would cost a lot of energy if the power of the The Magic ball is too high. So what's the minimum value of P with which can destroy all the magic stones, with no more than K magic balls? You can choose where to release each magic ball as your would, and the power of the ball must be a positive integer. 
Inputthe first line is the number of cases T (t≤100). For each case, the first line gives the integers n, K (1≤n≤50000, 1≤k≤100000). The second line is n integers, giving M1, M2, ... mn (1≤m≤109).
Outputprint minimum possible p in a line.
Sample Input
21 113 11) 4 5

Sample Output
26

Test instructions: Tell n a stone, and now use up to a maximum of K magic ball to destroy all the stones, ask each magic ball mana value at least how much. Each time the stone is destroyed, you can choose to stand on the right side of a stone, and then throw a magic ball to the left, such as standing on the right side of the first stone, then for the J Stone, will bring Max (0,p-(i-j) * (i-j)) point damage, Only when the damage value of a stone is less than 0 is it completely wiped out. It won't disappear when it's wiped out, and it'll be in that position.

Idea: Ask for the minimum value, then direct two points a magic value, and then to determine whether the condition can be satisfied. Can be thrown in any position, can throw any number (sum not more than K)

One thing to know is that from right to left, the mana value is reduced by 0 1 3 5 7 9 at a time ....

#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<&    lt;60) int N,k;int a[55555];queue<int>q;int Fun (ll x) {ll dis=0;    ll Sum=0;//sum every time the record reaches the I position, the sum of the mana value of all the balls before the loss is removed LL ff=0;    The int Ans=0;//ans is used to record the total number of thrown balls 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 (mid) Ri=mid;        else le=mid+1;    } printf ("%lld\n", RI); } return 0;}


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

HDU 3717 Rescue sub-plus queue optimization (TIPS)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.