hdu 5884 二分+队列
2016-09-17 21:28
232 查看
链接:戳这里
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
题意:
问
思路:
1:首先想到的是二分这个答案k,然后check每个k是否可行
2:对于每个k,总共需要归并n-1个数,每次归并k-1个数
所以当(n-1)%(k-1)!=0的时候,会出现归并不能最大化个数的情况,这样会影响二分的单调性
我们先取(n-1)%(k-1)个小的数凑成一次k
接下来的数我们已经知道了肯定是(n-1)/(k-1)块了,所以可以直接想到放进优先队列,然后每次都拿前k个数,新的数继续丢进去,但是这样会T
那么?我们这样处理,将其余的数丢进队列qu1中,再开一个队列qu2,每次新的数丢进qu2中。每次再qu1和qu2中一共拿k个小的数来组成一个新的数,新的数丢到qu2队尾去。接下来证明为什么不丢进优先队列,反而队列就可以了(也就是为什么qu2都是排好序的?),每次新丢进来数都是由前k个小的数取和得来的,所以每次丢进队尾的数都比前面丢进去的数要大,随手画一下就可以了,然后拿完qu1再去拿qu2。
代码:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<string>
#include<vector>
#include <ctime>
#include<queue>
#include<set>
#include<map>
#include<list>
#include<stack>
#include<iomanip>
#include<cmath>
#include<bitset>
#define mst(ss,b) memset((ss),(b),sizeof(ss))
///#pragma comment(linker, "/STACK:102400000,102400000")
typedef long long ll;
typedef long double ld;
#define INF (1ll<<60)-1
#define Max 1e9
using namespace std;
ll a[200100],T;
int n;
queue<int> qu1,qu2;
bool pd(int k){
while(!qu1.empty()) qu1.pop();
while(!qu2.empty()) qu2.pop();
for(int i=1;i<=n;i++) qu1.push(a[i]);
int num=0;
ll sum=0,ans=0;
if((n-1)%(k-1)!=0){
num=(n-1)%(k-1)+1;
for(int i=1;i<=num;i++) {
sum+=qu1.front();
qu1.pop();
}
qu2.push(sum);
ans+=sum;
}
while(!qu1.empty()){
sum=0;
for(int i=1;i<=k;i++){
if(!qu1.empty() && !qu2.empty() ){
if(qu1.front()<=qu2.front()){
sum+=qu1.front();
qu1.pop();
} else {
sum+=qu2.front();
qu2.pop();
}
} else if(qu1.empty()){
sum+=qu2.front();
qu2.pop();
} else if(qu2.empty()){
sum+=qu1.front();
qu1.pop();
}
}
ans+=sum;
qu2.push(sum);
}
if(ans>T) return false;
sum=0;num=0;
while(!qu2.empty()){
sum+=qu2.front();
qu2.pop();
num++;
if(num==k){
qu2.push(sum);
ans+=sum;
sum=0;
num=0;
if(qu2.size()==1) break;
}
}
if(ans>T) return false;
return true;
}
int main(){
int t;
scanf("%d",&t);
while(t--){
scanf("%d%I64d",&n,&T);
for(int i=1;i<=n;i++) scanf("%I64d",&a[i]);
sort(a+1,a+n+1);
int l=2,r=n,mid,ans=1;
while(l<=r){
mid=(l+r)/2;
if(pd(mid)) {
r=mid-1;
ans=mid;
} else l=mid+1;
}
printf("%d\n",ans);
}
return 0;
}
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
题意:
nn个有序序列的归并排序。每次可以选择不超过
kk个序列进行合并,合并代价为这些序列的长度和.总的合并代价不能超过
TT,
问
kk最小是多少。
思路:
1:首先想到的是二分这个答案k,然后check每个k是否可行
2:对于每个k,总共需要归并n-1个数,每次归并k-1个数
所以当(n-1)%(k-1)!=0的时候,会出现归并不能最大化个数的情况,这样会影响二分的单调性
我们先取(n-1)%(k-1)个小的数凑成一次k
接下来的数我们已经知道了肯定是(n-1)/(k-1)块了,所以可以直接想到放进优先队列,然后每次都拿前k个数,新的数继续丢进去,但是这样会T
那么?我们这样处理,将其余的数丢进队列qu1中,再开一个队列qu2,每次新的数丢进qu2中。每次再qu1和qu2中一共拿k个小的数来组成一个新的数,新的数丢到qu2队尾去。接下来证明为什么不丢进优先队列,反而队列就可以了(也就是为什么qu2都是排好序的?),每次新丢进来数都是由前k个小的数取和得来的,所以每次丢进队尾的数都比前面丢进去的数要大,随手画一下就可以了,然后拿完qu1再去拿qu2。
代码:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<string>
#include<vector>
#include <ctime>
#include<queue>
#include<set>
#include<map>
#include<list>
#include<stack>
#include<iomanip>
#include<cmath>
#include<bitset>
#define mst(ss,b) memset((ss),(b),sizeof(ss))
///#pragma comment(linker, "/STACK:102400000,102400000")
typedef long long ll;
typedef long double ld;
#define INF (1ll<<60)-1
#define Max 1e9
using namespace std;
ll a[200100],T;
int n;
queue<int> qu1,qu2;
bool pd(int k){
while(!qu1.empty()) qu1.pop();
while(!qu2.empty()) qu2.pop();
for(int i=1;i<=n;i++) qu1.push(a[i]);
int num=0;
ll sum=0,ans=0;
if((n-1)%(k-1)!=0){
num=(n-1)%(k-1)+1;
for(int i=1;i<=num;i++) {
sum+=qu1.front();
qu1.pop();
}
qu2.push(sum);
ans+=sum;
}
while(!qu1.empty()){
sum=0;
for(int i=1;i<=k;i++){
if(!qu1.empty() && !qu2.empty() ){
if(qu1.front()<=qu2.front()){
sum+=qu1.front();
qu1.pop();
} else {
sum+=qu2.front();
qu2.pop();
}
} else if(qu1.empty()){
sum+=qu2.front();
qu2.pop();
} else if(qu2.empty()){
sum+=qu1.front();
qu1.pop();
}
}
ans+=sum;
qu2.push(sum);
}
if(ans>T) return false;
sum=0;num=0;
while(!qu2.empty()){
sum+=qu2.front();
qu2.pop();
num++;
if(num==k){
qu2.push(sum);
ans+=sum;
sum=0;
num=0;
if(qu2.size()==1) break;
}
}
if(ans>T) return false;
return true;
}
int main(){
int t;
scanf("%d",&t);
while(t--){
scanf("%d%I64d",&n,&T);
for(int i=1;i<=n;i++) scanf("%I64d",&a[i]);
sort(a+1,a+n+1);
int l=2,r=n,mid,ans=1;
while(l<=r){
mid=(l+r)/2;
if(pd(mid)) {
r=mid-1;
ans=mid;
} else l=mid+1;
}
printf("%d\n",ans);
}
return 0;
}
相关文章推荐
- HDU 5884-Sort(队列+二分)
- hdu_5884_Sort(二分+单调队列)
- hdu 5884 Sort 二分+K哈夫曼树(解决不单调问题) 队列优化
- hdu 5884 Sort 二分 + K叉哈夫曼树 两个队列
- hdu 5884 Sort(二分+哈夫曼树(队列))
- HDU 5884 Sort (二分+双队列)
- HDU 5884 青岛网络赛(二分加维护队列)
- hdu 5884- Sort 哈夫曼树构造 + 模拟优先队列 + 二分
- HDU 5884 Sort(二分+单调队列)
- hdu 5884 Sort 二分+哈夫曼树(解决不单调问题) 队列优化
- HDU 5884 Sort(二分加双队列优化)
- hdu 5884 Sort 队列+多叉哈夫曼树
- hdu 3717 Rescue 二分加队列优化(技巧)
- hdu 5289 rmp+二分+枚举后界 or单调队列 ****
- HDU 5884 Sort(二分+优先队列)
- hdu 5884 二分 + 前k优化
- Hdu 4546 组合+第k小(二分或优先队列)(西山居初赛)
- 2016 ACM/ICPC Asia Regional Qingdao Online hdu 5884 Sort (智商+队列)★
- HDU-5884-Sort-二分加多叉哈夫曼树
- hdu 3717 二分+队列维护