1350:to Add Which?
Submit Page Summary Time Limit: 1 Sec Memory Limit: Mb submitted: 303 Solved: 134
Description
There is a integer sequence with N integers. You can use 1 unit of cost to increase any integer in the sequence by 1.
Could you tell us the least units of $ to achieve that, the absolute value of difference between any of adjacent integ ERs isn't more than D?
Input
The first line has one integer T, means there is T test Cases.
For each test case, the first line has a integers n, D (1 <= n <=, 0 <= d < 109), which has the same meaning as above. The next line has N integers describing the sequence. Every integer in this sequence are in range [0, 109).
The size of the input file is not exceed 5MB.
Output
For each test case, print a integer in one line, indicates the desired answer.
Sample Input
35 21 3 5 3 55 11 2 3 5 65 21 7 3 5 9
Sample Output
038
Hintsourcetopic Meaning:
give you a sequence of length n
Each operation can make a number in the sequence add 1
How many times do you have to work at least?
The absolute value of the difference between the elements that can make the sequence adjacent is DPractice:
have done a very very very similar question to this topic
It's written in an array.
no priority queue used
because that's when the priority queue is not!!!!!
GG
large numbers don't get bigger, they change in decimals. This is the heart of the problem.
the thought of greed
each time the maximum number of the sequence is taken out, expand the left and right 2 points.
Example of a sample 1 7 3 5 9
the maximum is 9 expansion left (without right) 5+2 becomes 7 just 2 changed sequence: 1 7 3 7 9
Now 9 Don't look, pop the queue.
look at the largest 7 (two 7, just look at the back one), now the second 7 left of the 3 extension 3+2=5 after the extended sequence 1 7 5 7 9
now 7 pops up and there's 1 7 5.
See the biggest 7
left of extension 7:1+4 expands to 5 and 7 just 2
extension 7 to the right of the 5, found not to extend the
now the dots are expanding.
During the scaling process
Record Cost
is the result .
Note the place:
The extended point doesn't have to be expanded anymore.
left end only extends right
right endpoint just extend left
The remaining two-way extension
#include <stdio.h>#include<iostream>#include<math.h>#include<algorithm>#include<memory.h>#include<memory>#include<queue>using namespacestd;#defineMax_v 1000005typedefLong LongLL;intt,n,d; LL sum;intA[max_v];structnode{intV,id; FriendBOOL operator<(node A,node b) {returna.v<B.V; }};p Riority_queue<node>Q;intMain () {scanf ("%d",&t); while(t--) {sum=0; Node P,p1; while(!q.empty ()) Q.pop (); scanf ("%d%d",&n,&d); for(intI=0; i<n;i++) {scanf ("%d",&A[i]); P.id=i; P.V=A[i]; Q.push (P); } while(!Q.empty ()) {P=Q.top (); Q.pop (); if(p.v==A[p.id]) { if(p.id>=1) { if(p.v-a[p.id-1]>d) {sum+ = (p.v-d-a[p.id-1]); A[p.id-1]=p.v-D; P1.V=a[p.id-1]; P1.id=p.id-1; Q.push (p1); } } if(p.id<n-1) { if(p.v-a[p.id+1]>d) {sum+ = (p.v-d-a[p.id+1]); A[p.id+1]=p.v-D; P1.V=a[p.id+1]; P1.id=p.id+1; Q.push (p1); } }}} printf ("%lld\n", sum); } return 0;}/*to give you a sequence of length n each operation can make a sequence of a number plus 1 ask you the minimum number of operations can make a sequence of adjacent elements of the absolute value of the difference is the D procedure: did a very very very similar topic of the problem is open array write no priority queue because at that time will not use the priority queue !!!!! GG does not grow larger, it changes the decimal. This is the core of the problem solving the greedy mind each time the largest number of extraction sequence, expand the left and right 2 points. Example 1 7 3 5 9 Max is 9 expansion left (no right) 5+2 to 7 just 2 changed sequence: 1 7 3 7 9 Now 9 don't look, pop up the queue to see the largest 7 (two 7, look at the back of that bar), now the second 7 left 3 expands 3+2=5 sequence 1 7 5 7 9 now 7 pops up and 1 7 5 look to the left of the largest 7 expansion 7: The 1+4 expands to 5 and 7 just 2 and then expands 7 to the right of 5, The discovery does not extend the point now that the record spent in the extension is the result of the note: The extended point does not extend the left end of the extension to the right end of the extension, as long as the remaining two-way extension to the left*/
Array emulation:
#include <stdio.h>#include<math.h>#include<algorithm>using namespacestd;#defineMax_v 100005typedefLong LongLL; LL A[max_v]; LL Maxx[max_v]; LL Minx[max_v];intMain () {intT; scanf ("%d",&t); while(t--) { intN; LL D; scanf ("%d%lld",&n,&d); for(intI=0; i<n;i++) scanf ("%lld",&A[i]); maxx[0]=a[0]; for(intI=1; i<n;i++) { if(maxx[i-1]-a[i]>d) Maxx[i]=maxx[i-1]-D; ElseMaxx[i]=A[i]; } minx[n-1]=a[n-1]; for(inti=n-2; i>=0; i--) { if(minx[i+1]-a[i]>d) Minx[i]=minx[i+1]-D; ElseMinx[i]=A[i]; } LL ans=0; for(intI=0; i<n;i++) ans+ = (max (maxx[i],minx[i))-A[i]); printf ("%lld\n", ans); } return 0;}
1350:to Add Which? (priority queue + greedy or array emulation)