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个合并,以保证最优合并;
代码:
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <climits>
#include <cstring>
#include <string>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <vector>
#include <list>
#define rep(i,m,n) for(i=m;i<=n;i++)
#define rsp(it,s) for(set<int>::iterator it=s.begin();it!=s.end();it++)
#define mod 1000000007
#define inf 0x3f3f3f3f
#define vi vector<int>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define ll long long
#define pi acos(-1.0)
#define pii pair<int,int>
#define Lson L, mid, rt<<1
#define Rson mid+1, R, rt<<1|1
const int maxn=1e5+;
using namespace std;
ll gcd(ll p,ll q){return q==?p:gcd(q,p%q);}
ll qpow(ll p,ll q){ll f=;while(q){if(q&)f=f*p;p=p*p;q>>=;}return f;}
int n,m,k,t,a[maxn];
bool check(int k)
{
int i,j,ans=,now,cnt;
queue<int>p,q;
rep(i,,n)p.push(a[i]);
if((j=(n-)%(k-))!=)
{
now=;
rep(i,,j+)now+=p.front(),p.pop();
ans+=now,q.push(now);
}
while(p.size()+q.size()>)
{
cnt=now=;
while(cnt<k)
{
if(!p.empty()&&(q.empty()||p.front()<=q.front()))now+=p.front(),p.pop();
if(!q.empty()&&(p.empty()||q.front()<=p.front()))now+=q.front(),q.pop();
cnt++;
}
ans+=now,q.push(now);
}
return ans<=m;
}
int main()
{
int i,j;
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&n,&m);
rep(i,,n)scanf("%d",&a[i]);
sort(a+,a+n+);
int l=,r=n,ans;
while(l<=r)
{
int mid=l+r>>;
if(check(mid))ans=mid,r=mid-;
else l=mid+;
}
printf("%d\n",ans);
}
//system("Pause");
return ;
}

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. hdu_3294_Girls' research(Manacher)

    题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=3294 题意:给你一个字符和一个字符串,第一个字符表示该字符代表a,然后让你求变换后的最长回文区间并输出 ...

  2. hdu_5274_Dylans loves tree(树剖)

    题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=5274 题意:给一棵树和叶子的值,然后有单点修改操作和询问区间操作,询问的是每一个值出现的奇偶次数,如果 ...

  3. HDU1236:排名

    Problem Description 今天的上机考试虽然有实时的Ranklist,但上面的排名只是根据完成的题数排序,没有考虑  每题的分值,所以并不是最后的排名.给定录取分数线,请你写程序找出最后 ...

  4. how to write a struct to a file directly?

    Using write and read system call. Following is an example: blk.h: #include <stdlib.h> #include ...

  5. 读书有感——《从毕业生到程序员使用C#开发商业软件》

    本来想自己写个读书感悟之类的东西,但是苦于自己语文水平太差,算了,我把里面觉得很赞的内容摘抄下来就好了(学习都是从模仿开始的嘛). 书籍:<从毕业生到程序猿使用C#开发商业软件> 作者:袁 ...

  6. Java良葛格 学习笔记

    学习一个新的事物时,如果遇到一些概念无法很快理解,这可能是因为要理解概念会需要其它概念先建立起来,所以先暂时放下这个疑问也是一个学习方法,称之为“存疑” ,在以后的学习过程中待必要的概念学会后,目前的 ...

  7. vs设计界面出现“建控件时出错 响应在此上下文中不可用”

    使用VS2010设计Asp.net时出现: 只有在配置文件或 Page 指令中将 enableSessionState 设置为 true 时,才能使用会话状态.还请确保在应用程序配置的 \\ 节中包括 ...

  8. c# 快速验证代理IP是否有用

    public void yanzhen(string str, int port) { WebProxy proxyObject = new WebProxy(str, port);//str为IP地 ...

  9. size_t, ptrdiff_t, size_type, difference_type

    size_t是unsigned类型,用于指明数组长度或下标,它必须是一个正数,std::size_t ptrdiff_t是signed类型,用于存放同一数组中两个指针之间的差距,它可以负数,std:: ...

  10. 批量删除实现js+springmvc

    前台的控件 <input type='checkbox' name='isSelect' value='"+data[i].id+"' ></input>& ...