Sort

Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1038    Accepted Submission(s): 231

Problem Description
Recently, Bob has just learnt a naive sorting algorithm: merge sort. Now, Bob receives a task from Alice.
Alice will give Bob N sorted sequences, and the i-th sequence includes ai elements. Bob need to merge all of these sequences. He can write a program, which can merge no more than k sequences in one time. The cost of a merging operation is the sum of the length of these sequences. Unfortunately, Alice allows this program to use no more than T cost. So Bob wants to know the smallest k to make the program complete in time.
 
Input
The first line of input contains an integer t0, the number of test cases. t0 test cases follow.
For each test case, the first line consists two integers N (2≤N≤100000) and T (∑Ni=1ai<T<231).
In the next line there are N integers a1,a2,a3,...,aN(∀i,0≤ai≤1000).
 
Output
For each test cases, output the smallest k.
 
Sample Input
1
5 25
1 2 3 4 5
 
Sample Output
3
思路:二分+哈夫曼;
首先二分枚举k,然后关键就是check的时候,首先看一组数据:12345,当k为3的时候,按照如果我们取的话会得到21,然后再看k=4,我们的设想肯定是递减的,但是,如果我们开始就取4个的答案为25,所以这样二分就不行,但是是我们的取法不对,我们应该保证后面的大的数字 竟量只取1次,那么最后k-1个数肯定被一次加完,不会有剩余,那么N个数如果从开始能选k个,那么剩下的肯定要是k-1的倍数,那么当剩下的不是k-1的倍数,我们要在前面添加0,个数就是k-1-(N-1)%(k-1);这样就可以保证最优,也就单调了。
  1 #include<stdio.h>
2 #include<algorithm>
3 #include<string.h>
4 #include<iostream>
5 #include<queue>
6 #include<stdlib.h>
7 #include<math.h>
8 #include<set>
9 using namespace std;
10 typedef long long LL;
11 queue<int>que1;
12 queue<int>que2;
13 LL ans[100005];
14 bool check(int mid,int N,LL T);
15 int main(void)
16 {
17 int n;
18 scanf("%d",&n);
19 while(n--)
20 {
21 int N;
22 LL T;
23 scanf("%d %lld",&N,&T);
24 for(int i = 0; i < N; i++)
25 {
26 scanf("%lld",&ans[i]);
27 }
28 sort(ans,ans+N);
29 int id = 1;
30 int l = 2;
31 int r = N;
32 while(l<=r)
33 {
34 int mid = (l+r)/2;
35 if(check(mid,N,T))
36 {
37 r = mid-1;
38 id = mid;
39 }
40 else l = mid+1;
41 }
42 printf("%d\n",id);
43 }
44 return 0;
45 }
46 bool check(int mid,int N,LL T)
47 {
48 LL ask = 0;
49 while(!que1.empty())
50 {
51 que1.pop();
52 }
53 while(!que2.empty())
54 {
55 que2.pop();
56 }
57 int x = (N-1)%(mid - 1);
58 if(x!=0)
59 {
60 x = (mid-1-x)%(mid-1);
61 while(x)
62 {
63 que1.push(0);
64 x--;
65 }
66 }
67 for(int i = 0; i < N ; i++)
68 {
69 que1.push(ans[i]);
70 ask-=ans[i];
71 }
72 while(true)
73 {
74 LL sum = 0;
75 int c = mid;
76 while(c)
77 {
78 int flag = 0;
79 if(!que1.empty()&&!que2.empty())
80 {
81 int t = que1.front();
82 int tt = que2.front();
83 if(t<=tt)
84 {
85 que1.pop();
86 }
87 else
88 {
89 t = tt;
90 que2.pop();
91 }
92 sum+=t;
93 c--;
94 flag = 1;
95 }
96 else if(!que1.empty())
97 {
98 int t = que1.front();
99 que1.pop();
100 sum+=t;
101 c--;
102 flag = 1;
103 }
104 else if(!que2.empty())
105 {
106 int t = que2.front();
107 que2.pop();
108 sum+=t;
109 c--;
110 flag = 1;
111 }
112 if(!flag)
113 break;
114 }
115 que2.push(sum);
116 ask += sum;
117 if(c>0)break;
118 }
119 if(ask > T)
120 return false;
121 else return true;
122 }
 

最新文章

  1. 1.reset.css的设置
  2. Linux命令之diff
  3. jQuery的常用函数扩展
  4. 剑指Offer 链表中倒数第k个结点
  5. uva 10340 All in All
  6. Win8 IE10 只能以管理员打开的解决方法
  7. poj 1466 Girls and Boys(二分匹配之最大独立集)
  8. zk 节点故障 重连机制
  9. unity3d由于Camera.main.transform报空引用错误的解决方案
  10. 解决 .NET Core 中 GetHostAddressesAsync 引起的 EnyimMemcached 死锁问题
  11. SQL Server学习之路(六):“增删改查”之“查”
  12. 12.1、Libgdx的图像之持续性和非持续性渲染
  13. epoll ET(边缘触发) LT(水平触发)
  14. tableview前端基础设计(初级版)
  15. 天坑之路:用js给选中文字添加样式
  16. [HDU5963]朋友
  17. 选择、操作web元素-3
  18. es6 对象的扩展
  19. 洛谷P3960 列队(NOIP2017)(Splay)
  20. 整合大量开源库项目(八)能够载入Gif动画的GifImageView

热门文章

  1. C++/Python冒泡排序与选择排序算法详解
  2. act
  3. Linux 内存泄漏 valgrind
  4. Scala和Java的List集合互相转换
  5. JS模块化,Javascript 模块化管理的历史
  6. ViewStub应用
  7. IntentFilter,PendingIntent
  8. C语言编辑链接
  9. 【Linux】【Services】【Package】rpm包制作
  10. java客户端的elasticSearch索引库的相关操作