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. C - AtCoDeerくんと選挙速報 / AtCoDeer and Election Report

    ceil有毒啊..用ceil一直错. 思路就是模拟吧,设当前的答案是ansx和ansy. 如果比例是小于ansx的,那么就要乘以一个倍数k1,使得a * k1 >= ansx的. 所以就用cei ...

  2. Spark Mllib里的本地矩阵概念、构成(图文详解)

    不多说,直接上干货! Local matrix:本地矩阵 数组Array(1,2,3,4,5,6)被重组成一个新的2行3列的矩阵. testMatrix.scala package zhouls.bi ...

  3. MVC 知识点总结

    [此篇文章收录于其他博客,作为笔记使用] 一·  MVC MVC设计模式->MVC框架(前端开发框架),asp.net(webform) aspx M:Model (模型,负责业务逻辑处理,比如 ...

  4. Jquery3

    动画 动画动画效果一:show(时间),hide(时间)说明:时间的单位为毫秒方法toggle(时间):使用动画切换显示与隐藏动画效果二:slideDown(时间),slideUp(时间)切换:sli ...

  5. 关闭mysql validate-password插件

    mysql5.7 的validate-password对密码策略有限制,比如长度大小写,太麻烦,我习惯开发环境下为root,所以在开发环境关闭这个插件的话只需在/etc/my.cnf中添加valida ...

  6. 【简问】一些个人不会的问题,收到解答经核实OK的会在下方附注答案

    1.p标签内放行内块(如,input)适宜么(已知p是块元素,但p内不宜放置div)? 2.如何单独设置文字下划线颜色? 3.行内元素可以定位吗? 4.支持 margin:0 auto; 的元素类型有 ...

  7. 微信小程序中的target和currentTarget区别

    最近在学习微信小程序相关知识,其中提到了两个属性target和currentTarget,其中target是指向触发事件的元素(常见于事件委托中),而currentTarget是指向捕获事件的元素(即 ...

  8. Eclipse IDE配置PHP开发、调试环境

    前言 使用java语言开发的朋友想必对Eclipse开发工具已经不陌生了,那么Eclipse作为java主流的开发工具,是否能够开发PHP项目呢?答案如你所想,肯定是可以的!以下就是该IDE下如何配置 ...

  9. linux概念和体系

    1. Linux开机启动 2. Linux文件管理 3. Linux的架构 4. Linux命令行与命令 5. Linux文件管理相关命令 6. Linux文本流 7. Linux进程基础 8. Li ...

  10. python之道08

    1.有如下文件,a1.txt,里面的内容为: 某某是最好的学校, 全心全意为学生服务, 只为学生未来,不为牟利. 我说的都是真的.哈哈 分别完成以下的功能: a,将原文件全部读出来并打印. 答案 f ...