Sort

Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

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个合并;
   注意如果(n-1)%(k-1)!=0,则要先把(n-1)%(k-1)+1个合并,以保证最优合并;
代码:
  1. #include <iostream>
  2. #include <cstdio>
  3. #include <cstdlib>
  4. #include <cmath>
  5. #include <algorithm>
  6. #include <climits>
  7. #include <cstring>
  8. #include <string>
  9. #include <set>
  10. #include <map>
  11. #include <queue>
  12. #include <stack>
  13. #include <vector>
  14. #include <list>
  15. #define rep(i,m,n) for(i=m;i<=n;i++)
  16. #define rsp(it,s) for(set<int>::iterator it=s.begin();it!=s.end();it++)
  17. #define mod 1000000007
  18. #define inf 0x3f3f3f3f
  19. #define vi vector<int>
  20. #define pb push_back
  21. #define mp make_pair
  22. #define fi first
  23. #define se second
  24. #define ll long long
  25. #define pi acos(-1.0)
  26. #define pii pair<int,int>
  27. #define Lson L, mid, rt<<1
  28. #define Rson mid+1, R, rt<<1|1
  29. const int maxn=1e5+;
  30. using namespace std;
  31. ll gcd(ll p,ll q){return q==?p:gcd(q,p%q);}
  32. ll qpow(ll p,ll q){ll f=;while(q){if(q&)f=f*p;p=p*p;q>>=;}return f;}
  33. int n,m,k,t,a[maxn];
  34. bool check(int k)
  35. {
  36. int i,j,ans=,now,cnt;
  37. queue<int>p,q;
  38. rep(i,,n)p.push(a[i]);
  39. if((j=(n-)%(k-))!=)
  40. {
  41. now=;
  42. rep(i,,j+)now+=p.front(),p.pop();
  43. ans+=now,q.push(now);
  44. }
  45. while(p.size()+q.size()>)
  46. {
  47. cnt=now=;
  48. while(cnt<k)
  49. {
  50. if(!p.empty()&&(q.empty()||p.front()<=q.front()))now+=p.front(),p.pop();
  51. if(!q.empty()&&(p.empty()||q.front()<=p.front()))now+=q.front(),q.pop();
  52. cnt++;
  53. }
  54. ans+=now,q.push(now);
  55. }
  56. return ans<=m;
  57. }
  58. int main()
  59. {
  60. int i,j;
  61. scanf("%d",&t);
  62. while(t--)
  63. {
  64. scanf("%d%d",&n,&m);
  65. rep(i,,n)scanf("%d",&a[i]);
  66. sort(a+,a+n+);
  67. int l=,r=n,ans;
  68. while(l<=r)
  69. {
  70. int mid=l+r>>;
  71. if(check(mid))ans=mid,r=mid-;
  72. else l=mid+;
  73. }
  74. printf("%d\n",ans);
  75. }
  76. //system("Pause");
  77. return ;
  78. }

2016青岛网络赛 Sort的更多相关文章

  1. HDU 5880 Family View (2016 青岛网络赛 C题,AC自动机)

    题目链接  2016 青岛网络赛  Problem C 题意  给出一些敏感词,和一篇文章.现在要屏蔽这篇文章中所有出现过的敏感词,屏蔽掉的用$'*'$表示. 建立$AC$自动机,查询的时候沿着$fa ...

  2. 2016 年青岛网络赛---Sort(k叉哈夫曼)

    题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=5884 Problem Description Recently, Bob has just learn ...

  3. HDU - 5878 2016青岛网络赛 I Count Two Three(打表+二分)

    I Count Two Three 31.1% 1000ms 32768K   I will show you the most popular board game in the Shanghai ...

  4. HDU - 5887 2016青岛网络赛 Herbs Gathering(形似01背包的搜索)

    Herbs Gathering 10.76% 1000ms 32768K   Collecting one's own plants for use as herbal medicines is pe ...

  5. HDU5887 Herbs Gathering(2016青岛网络赛 搜索 剪枝)

    背包问题,由于数据大不容易dp,改为剪枝,先按性价比排序,若剩下的背包空间都以最高性价比选时不会比已找到的最优解更好时则剪枝,即 if(val + (LD)pk[d].val / (LD)pk[d]. ...

  6. HDU 5886 Tower Defence(2016青岛网络赛 I题,树的直径 + DP)

    题目链接  2016 Qingdao Online Problem I 题意  在一棵给定的树上删掉一条边,求剩下两棵树的树的直径中较长那的那个长度的期望,答案乘上$n-1$后输出. 先把原来那棵树的 ...

  7. HDU5880 Family View(2016青岛网络赛 AC自动机)

    题意:将匹配的串用'*'代替 tips: 1 注意内存的使用,据说g++中指针占8字节,c++4字节,所以用g++交会MLE 2 注意这种例子, 12abcdbcabc 故失败指针要一直往下走,否则会 ...

  8. 2016青岛网络赛 Barricade

    Barricade Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Proble ...

  9. 2016青岛网络赛 The Best Path

    The Best Path Time Limit: 9000/3000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others) Pr ...

随机推荐

  1. 转:jmeter之线程组

    虽然有三个添加线程组的选项,名字不一样, 创建之后,其界面是完全一样的.之前的版本只有一个线程组的名字.现在多一个setUp theread Group 与terDown Thread Group 1 ...

  2. POJ 3548 Restoring the digits

    暴力搜索.注意题目说每个字母对应的数字不同,这句话表明最多只有10个字母,所以暴力DFS绝对不会TLE. #include<cstdio> #include<cstring> ...

  3. VBS操作JS网页元素实例

    '=========================================================================='' VBScript Source File - ...

  4. Chapter 1 First Sight——31

    I took notes carefully anyway, always looking down. 不论怎么样我都仔细的记着笔记,一直低着头. I couldn't stop myself fro ...

  5. Linked List 实例

    文件功能:实现了动态建立一个学生信息的链表包括链表的创建.插入.删除.和打印输出学生信息包括姓名和分数 #include<iostream> #include<string> ...

  6. debia下安装libjpeg

    今天在编译时遇到如下问题: configure: error: no usable libjpeg; please install libjpeg devel package or equivalen ...

  7. LD_LIBRARY_PATH vs LIBRARY_PATH

    LIBRARY_PATH is used by gcc before compilation to search for directories containing libraries that n ...

  8. IoC容器Autofac正篇之类型关联(服务暴露)(八)

    类型关联  类型关联就是将类挂载到接口(一个或多个)上去,以方便外部以统一的方式进行调用(看下例). 一.As关联 我们在进行手动关联时,基本都是使用As进行关联的. 1 2 3 4 5 6 7 8 ...

  9. python--lambda和def函数

    1.Python lambda和Python def区别分析 Python支持一种有趣的语法,它允许你快速定义单行的最小函数.这些叫做lambda的函数,是从Lisp借用来的,可以用在任何需要函数的地 ...

  10. Servlet学习三:不允许直接访问jsp处理方式一过滤器

    转自:http://zy19982004.iteye.com/blog/1755189