[Reference] [blog] https://blog.csdn.net/Tawn0000/article/details/82255682
Meaning of the questions:
The number n in accordance with a set of every k to combine, merge takes cost is the length of two numbers, Q: T is in the consumption of all of the desired number of the minimum k.
analysis:
Prior to the merger to deal with what fraction, because each taking the k until the last step remaining number may be less than the number of k, this result is even greater consolidation of cost, for example: 1234 5 6, k = 4, the first option 1234 and then 5610 is 31, but because the fraction is 3, to handle three fraction, namely: first take 123 then is 4566, it is 27. Apparently 27 <31.
And there is a reference to bloggers, I is started in accordance with a half + idea to consider a priority queue, and finally at the TLE complexity of O (n logn logn) a.
If a two-queue (a queue memory the original number (in ascending order and then enqueued), referred to as a queue p1, the number of combined deposit another queue, the queue referred to as P2), the time complexity is O (n logn ).
Dual Specific operation is a fetch queue p1 for k original data are added after the completion of the addition values and the values extracted p2 queues comparison, take a small number of added p2 (remember the number of the queue is taken pop off), as the original queue number in ascending order of arrangement, it will meet a small number of first-team than the number after the team! . As a result the number involved in the merger will make always start small.
AC code:
1 #include<bits/stdc++.h> 2 using namespace std; 3 typedef long long ll; 4 queue<int> q1,q2; 5 int a[100010]; 6 int n,T; 7 bool P(int mid) 8 { 9 while(q1.size()) q1.pop(); 10 while(q2.size()) q2.pop(); 11 for(int i=0;i<n;i++) q1.push(a[i]); 12 ll res=0; 13 int t=(n-1)%(mid-1); 14 if(t) 15 { 16 int p=0; 17 for(int i=0;i<t+1&&!q1.empty();i++) 18 { 19 p+=q1.front(); 20 q1.pop(); 21 } 22 res+=p; 23 q2.push(p); 24 } 25 while(1) 26 { 27 int p=0; 28 for(int i=0;i<mid;i++) 29 { 30 int x=99999999,y=99999999; 31 if(q1.empty()&&q2.empty()) break; 32 if(!q1.empty()) x=q1.front(); 33 if(!q2.empty()) y=q2.front(); 34 if(x<y) 35 { 36 p+=x; 37 q1.pop(); 38 } 39 else 40 { 41 p+=y; 42 q2.pop(); 43 } 44 } 45 res+=p; 46 if(res>T) return true; 47 if(q1.empty()&&q2.empty()) break; 48 q2.push(p); 49 } 50 if(res>T) return true; 51 else return false; 52 } 53 int main() 54 { 55 //freopen("input.txt","r",stdin); 56 int t; 57 scanf("%d",&t); 58 while(t--) 59 { 60 scanf("%d%d",&n,&T); 61 for(int i=0;i<n;i++) 62 { 63 scanf("%d",&a[i]); 64 } 65 sort(a,a+n); 66 int sd=1,ed=n; 67 while(ed - sd > 1) 68 { 69 int mid=sd+(ed-sd)/2; 70 if(P(mid)) sd=mid; 71 else ed=mid; 72 } 73 printf("%d\n",ed); 74 } 75 }