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
 
 
 #include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstdlib>
#include <cstring>
#include <string>
#include <deque>
#include <queue>
using namespace std;
#define ll long long
#define N 100009
#define gep(i,a,b) for(int i=a;i<=b;i++)
#define gepp(i,a,b) for(int i=a;i>=b;i--)
#define gep1(i,a,b) for(ll i=a;i<=b;i++)
#define gepp1(i,a,b) for(ll i=a;i>=b;i--)
#define mem(a,b) memset(a,b,sizeof(a))
int n,m,t;
int sum[N],a[N];
priority_queue<int,vector<int>,greater<int> >que;
bool check(int k){
while(!que.empty()) que.pop();
int x=(n-)%(k-);//共需要归并n-1个数,每次要归并k-1个数
int add=;
if(x){//为了不影响单调性
x++;//每一次都是减去X个,又加一个。
add+=sum[x];
que.push(add);//先去掉x个,后面正常
}
gep(i,x+,n) que.push(a[i]);
int y=(n-)/(k-);//分步骤
gep(i,,y-){
int tmp=k;
int tt=;
while(tmp--){//每次k个
int v=que.top();
que.pop();
tt+=v;
}
que.push(tt);//再压入
add+=tt;
}
return add<=m;//不超过m才行
}
int main()
{
scanf("%d",&t);
while(t--){
scanf("%d%d",&n,&m);
mem(a,);
mem(sum,);
gep(i,,n) {
scanf("%d",&a[i]);
}
sort(a+,a++n);//要先排序
gep(i,,n){
sum[i]=sum[i-]+a[i];
}
int l=,r=n;//至少2个
while(l<=r){//r可能取到
int mid=(r+l)>>;
if(check(mid)) r=mid-;//r=mid是错的,会死循环
else l=mid+;
}
printf("%d\n",r+);
}
return ;
}

ACM-ICPC 2016 Qingdao Preliminary Contest G. Sort的更多相关文章

  1. ACM-ICPC 2016 Qingdao Preliminary Contest

    A I Count Two Three I will show you the most popular board game in the Shanghai Ingress Resistance T ...

  2. 贪心+离散化+线段树上二分。。。 Samara University ACM ICPC 2016-2017 Quarterfinal Qualification Contest G. Of Zorcs and Axes

    题目链接:http://codeforces.com/gym/101149/problem/G 题目大意:给你n对数字,为(a[i], b[i]),给你m对数字,为(w[i], c[i]).给n对数字 ...

  3. ACM ICPC Central Europe Regional Contest 2013 Jagiellonian University Kraków

    ACM ICPC Central Europe Regional Contest 2013 Jagiellonian University Kraków Problem A: Rubik’s Rect ...

  4. ACM ICPC 2016–2017, NEERC, Northern Subregional Contest Problem J. Java2016

    题目来源:http://codeforces.com/group/aUVPeyEnI2/contest/229510 时间限制:2s 空间限制:256MB 题目大意: 给定一个数字c 用 " ...

  5. ACM ICPC, Amman Collegiate Programming Contest (2018) Solution

    Solution A:Careful Thief 题意:给出n个区间,每个区间的每个位置的权值都是v,然后找长度为k的区间,使得这个区间的所有位置的权值加起来最大,输出最大权值, 所有区间不重叠 思路 ...

  6. [刷题]ACM ICPC 2016北京赛站网络赛 D - Pick Your Players

    Description You are the manager of a small soccer team. After seeing the shameless behavior of your ...

  7. Samara SAU ACM ICPC 2013-2014 Quarterfinal Qualification Contest

    A: 简单题,因为题目中说了不会有数据相同: #include<cstdio> #include<algorithm> #define maxn 200005 using na ...

  8. [刷题]ACM/ICPC 2016北京赛站网络赛 第1题 第3题

    第一次玩ACM...有点小紧张小兴奋.这题目好难啊,只是网赛就这么难...只把最简单的两题做出来了. 题目1: 代码: //#define _ACM_ #include<iostream> ...

  9. 最小割 D. Behind the Wall Samara University ACM ICPC 2016-2017 Quarterfinal Qualification Contest

    题目链接:http://codeforces.com/gym/101149/problem/D 题目大意: 堡垒受到攻击.堡垒是n*m的矩阵,矩阵里刚开始都是平地,然后那个数值表示在当前平地上建一面墙 ...

随机推荐

  1. Django 的一些错误以及处理

    django.template.exceptions.TemplateSyntaxError: Invalid block tag on line 589: 'static', expected 'e ...

  2. CellSet 遍历

    CellSet 结构: 查询MDX: SELECT NON EMPTY {{ {{ {{ {{ {{ AddCalculatedMembers([店铺.店铺ID].[店铺ID].Members)}} ...

  3. hystrix 应用问题

    1.问题总结, 如果项目中使用了ThreadLocal,注意hystix创建新线程时,ThreadLocal中存的是之前线程中的数据,在hystix线程中获取不到 2.问题 throwable异常参数 ...

  4. Intellij idea 创建JAVA项目

    1. 打开软件,new一个project的java项目 2. 点击下一步,此界面可通过模板生成项目,如下图 3. 填写项目名称和项目源码的保存路径,如下图 4. 点击 Finish 完成按钮即可,项目 ...

  5. Kendo MVVM 数据绑定(四) Disabled/Enabled

    Kendo MVVM 数据绑定(四) Disabled/Enabled Disabled 和 Enabled 绑定可以根据 ViewModel 的某个属性值的 true,false 来设置 DOM 元 ...

  6. ABAP事件的简单用法

    1.1.事件: 用于捕获某类对象状态的改变来触发事件的方法,并进行处理 1.2.定义:可以在类或接口中进行声明 EVENTS|CLASS-EVENTS evt  EXPORTING … VALUE(p ...

  7. js基础的自定义属性练习

    js基础的自定义属性练习: <!DOCTYPE HTML> <html> <head> <meta http-equiv="Content-Type ...

  8. AES加密示例

    最近用到对文本内容进行加密,于是查了一下常用的加密算法: DES(Data Encryption Standard):对称算法,数据加密标准,速度较快,适用于加密大量数据的场合:3DES(Triple ...

  9. elasticsearch查询方式

    1.query string a).GET /index/type/_search ===>>查询所有 b).GET /index/type/_search?q=filed:value&a ...

  10. 掌握 Azure 的注册、帐户和订阅管理 Azure 上云须知

    计划使用由世纪互联运营的 Microsoft Azure 的用户,可通过下列流程注册开通并购买所需 Azure 服务:信息获取 > 试用 > 购买 > 账户/订阅管理 > 支付 ...