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. 大都市 meg

    Description 在经济全球化浪潮的影响下,习惯于漫步在清晨的乡间小路的邮递员Blue Mary也开始骑着摩托车传递邮件了.不过,她经常回忆起以前在乡间漫步的情景. 昔日,乡下有依次编号为1.. ...

  2. HttpListener Start 拒绝访问

    HttpListener Start 拒绝访问 以管理员权限运行cmd 输入: netsh http add urlacl url=http://127.0.0.1:8080/ user=HuQing

  3. Java运算符、引用数据类型、流程控制语句

    1运算符 1.1算术运算符 运算符是用来计算数据的符号. 数据可以是常量,也可以是变量. 被运算符操作的数我们称为操作数. 算术运算符最常见的操作就是将操作数参与数学计算: 运算符 运算规则 范例 结 ...

  4. java的三大特性之一继承概述

    0.继承-----注意事项 00.子类最多只能继承一个父类(指直接继承) 01.java所有的类都是Object的子类 02.JPK6.0中有202个包3777个类,接口,异常,枚举,注释和错误 03 ...

  5. 个人博客 attack.cf

    新开了个emlog搭的博客 地址:attack.cf 主要分享一下网络安全方面的东西和一些精品资源 欢迎来访

  6. jsp另外五大内置对象之-exception

    //有异常的页面 <%@ page language="java" contentType="text/html; charset=utf-8" page ...

  7. URAL 2027 URCAPL, Episode 1 (模拟)

    题意:给你一个HxW的矩阵,每个点是一个指令,根据指令进行一系列操作. 题解:模拟 #include<cstdio> #include<algorithm> using nam ...

  8. 导入文件 服务器报错,有可能是 开发时候是window 服务器是linux,两个系统的文件系统的/和\是相反的,要注意这块

    导入文件 服务器报错,有可能是 开发时候是window 服务器是linux,两个系统的文件系统的/和\是相反的,要注意这块

  9. nginx 的反向代理及缓存功能

    上游服务器的设置 server { #监听的IP及端口 listen 127.0.0.1:8080; #虚拟主机对硬解析的主机名 #server_name localhost; #charset ko ...

  10. 安装搭配VUE使用的UI框架ElementUI

    可以搭配vue的UI框架有几个,我用的是element-ui,现在呢,我要在复习一遍 1.vue init webpack-simple element-ui2.cd element-ui3.npm ...