HDU 5884-Sort(佇列+二分)
Sort
Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1631 Accepted Submission(s): 418
Problem Description
Recently, Bob has just learnt a naive sorting algorithm: merge sort. Now, Bob receives a task from Alice.
Alice will give Bob sorted sequences, and the -th sequence includes elements. Bob need to merge all of these sequences. He can write a program, which can merge no more than 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 cost. So Bob wants to know the smallest to make the program complete in time.
Alice will give Bob sorted sequences, and the -th sequence includes elements. Bob need to merge all of these sequences. He can write a program, which can merge no more than 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 cost. So Bob wants to know the smallest to make the program complete in time.
Input
The first line of input contains an integer ,
the number of test cases. test
cases follow.
For each test case, the first line consists two integers and .
In the next line there are integers .
For each test case, the first line consists two integers and .
In the next line there are integers .
Output
For each test cases, output the smallest .
Sample Input
1
5 25
1 2 3 4 5
Sample Output
3
Source
Recommend
題目意思:
n個元素的序列做歸併排序,每次可以選擇不超過k
個序列進行合併,合併代價為這些序列的長度和,總的合併代價不能超過T
,
求一個最小的k
。
解題思路:
先升序排列陣列,再判斷對於當前的k,序列能否被恰好分割。如果不能,就先取(n-1)%(k-1)+1個數歸併一次;之後每次從兩個隊頭取出k個數出來合併,判斷與T的大小關係。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
#include <set>
#include <iomanip>
#include <algorithm>
#include <queue>
#define maxn 200010
#define INF 0xfffffff
using namespace std;
int n;
long long x[maxn],T;
queue<int> que1,que2;
bool Valid(int k)
{
long long sum=0,ans=0;
while(!que1.empty()) que1.pop();//沒有clear()所以只能手動清空
while(!que2.empty()) que2.pop();
for(int i=0; i<n; ++i)//把陣列元素壓入佇列一
que1.push(x[i]);
if((n-1)%(k-1)!=0)//如果不能恰好分割
{
int num=(n-1)%(k-1)+1;
for(int i=0; i<num; ++i)
{
sum+=que1.front();
que1.pop();
}
que2.push(sum);
ans+=sum;
}
//每次從兩個隊頭取出k個數出來合併
while(!que1.empty())
{
sum=0;
for(int i=0; i<k; ++i)
{
if(!que1.empty() &&!que2.empty())
{
if(que1.front()<=que2.front())
{
sum+=que1.front();
que1.pop();
}
else
{
sum+=que2.front();
que2.pop();
}
}
else if(que1.empty())
{
sum+=que2.front();
que2.pop();
}
else if(que2.empty())
{
sum+=que1.front();
que1.pop();
}
}
ans+=sum;
que2.push(sum);
}
if(ans>T) return false;//此時的k不滿足
sum=0;
while(!que2.empty())
{
for(int i=0; i<k; ++i)
{
if(i==k-1)//保證取k個數
{
que2.push(sum);
ans+=sum;
sum=0;
}
if(que2.empty()) break;//不足k個數
sum+=que2.front();
que2.pop();
}
}
if(ans>T) return false;
return true;
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
scanf("%d%I64d",&n,&T);
for(int i=0; i<n; ++i)
scanf("%I64d",&x[i]);
sort(x,x+n);//注意要先排個序
int low=1,high=n-1,k,ans=1;
while(low<=high)
{
k=(low+high)/2;
if(Valid(k))//當前k滿足條件,區間縮成左半邊
{
high=k-1;
ans=k;
}
else low=k+1;
}
printf("%d\n",ans);//當前k不滿足條件,區間縮成右半邊
}
return 0;
}
/*
1
5 25
1 2 3 4 5
*/
相關文章
- hdu 3415 單調佇列佇列
- HDU 3530 單調佇列佇列
- hdu 3401 單調佇列+DP佇列
- hdu 4122 單調佇列佇列
- hdu5040 優先佇列+bfs佇列
- hdu4374單調佇列+dp佇列
- hdu5380 貪心+雙端佇列佇列
- HDU 6274 Master of Sequence(思維+樹狀陣列+二分)AST陣列
- 【BFS+優先佇列】HDU 3442 Three Kingdoms佇列
- HDU 3530 Subsequence (dp+單調佇列)佇列
- HDU4546 比賽難度 (優先佇列)佇列
- hdu 4546 優先佇列 數列組合和第m小佇列
- HDU3530 單調佇列的應用佇列
- HDU5630 Hiking(貪心+優先佇列)佇列
- hdu3415 單調佇列求區間最大和佇列
- hdu 4122Alice's mooncake shop(單調佇列)佇列
- 佇列、阻塞佇列佇列
- Safe Or Unsafe(hdu2527)哈弗曼VS優先佇列佇列
- hdu7462-字串【SAM,二分】字串
- 佇列-單端佇列佇列
- HDU 1026(優先佇列+BFS+前驅記錄)佇列
- 佇列 和 迴圈佇列佇列
- 【佇列】【懶排序】佇列Q佇列排序
- HDU 3486 Interviewe(RMQ+二分)ViewMQ
- 陣列模擬佇列 以及佇列的複用(環形佇列)陣列佇列
- 佇列 手算到機算 入門 佇列 迴圈佇列佇列
- 圖解--佇列、併發佇列圖解佇列
- 單調佇列雙端佇列佇列
- 佇列佇列
- RabbitMQ 訊息佇列之佇列模型MQ佇列模型
- Kafka 延時佇列&重試佇列Kafka佇列
- HDU 4123 Bob's Race:樹的直徑 + 單調佇列 + st表佇列
- HDU3415 Max Sum of Max-K-sub-sequence (DP+單調佇列)佇列
- Java版-資料結構-佇列(陣列佇列)Java資料結構佇列陣列
- C語言 簡單的佇列(陣列佇列)C語言佇列陣列
- 稀疏陣列、佇列陣列佇列
- 阻塞佇列一——java中的阻塞佇列佇列Java
- 07-主佇列和全域性佇列佇列