HDU 5884 Sort(二分+优先队列)

时间:2022-12-14 05:00:43

http://acm.hdu.edu.cn/showproblem.php?pid=5884

题意:
有个屌丝设计了一个程序,每次可以将k个数组进行合并,代价为这k个数组总的长度之和。现在另外一个屌丝要他最后合并成一个数组时的总代价不能超过T。求k的最小值。

思路:
贪心策略是长度越小的肯定先进行合并。一开始是直接用一个优先队列,但是这样会TLE的。。

后来改成了用两个队列进行模拟,先将数组排好序然后放入队列之中,每次合并之后的放入另一个队列之中,每次只需要再两个队列之中选择小的即可。

 #include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<vector>
#include<stack>
#include<queue>
#include<cmath>
#include<map>
#include<set>
#include<bitset>
using namespace std;
typedef long long ll;
typedef pair<int,int> pll;
const int INF = 0x3f3f3f3f;
const int maxn=+; ll n,t;
int a[maxn]; bool solve(int x)
{
queue<ll> q1,q2;
int left=(n-)%(x-);
if(left) for(int i=;i<=x--left;i++) q1.push();
for(int i=;i<=n;i++) q1.push(a[i]);
ll sum=; while(!q1.empty() || !q2.empty())
{
int tmp=;
for(int i=;i<=x;i++)
{
if(q1.empty())
{
tmp+=q2.front();
q2.pop();
}
else if(q2.empty())
{
tmp+=q1.front();
q1.pop();
}
else
{
int u=q1.front();
int v=q2.front();
if(u<v)
{
tmp+=u;
q1.pop();
}
else
{
tmp+=v;
q2.pop();
}
}
}
sum+=tmp;
if(!q1.empty() || !q2.empty()) q2.push(tmp);
}
return sum<=t;
} int main()
{
//freopen("in.txt","r",stdin);
int T;
scanf("%d",&T);
while(T--)
{
scanf("%lld%lld",&n,&t);
for(int i=;i<=n;i++) scanf("%d",&a[i]);
sort(a+,a+n+);
int ans=;
int l=,r=n;
while(l<=r)
{
int mid=(l+r)/;
if(solve(mid)) {ans=mid;r=mid-;}
else l=mid+;
}
printf("%d\n",ans);
}
return ;
}