codeforces 377B Preparing for the Contest
题目是求在支付passes之和不超过s的前提下,让所花时间尽量少的一种方案。
分析:明显有两个限制条件:passes和所花时间 ,如果同时对两个条件进行处理很难搞,只能先固定一个条件,求另一个条件的最优解 。
这题如果先固定passes不超过s,求最少时间不是那么容易 ; 反过来 若二分天数 , 先固定所花时间不超过t天 , 求最少花费,就容易多了, 可以通过贪心策略得到 : 按难度a从大到小处理每个bug (为什么不是从小到大?), 在处理某个bug时 , 要从能力b > = a 的人中选取花费c 最少的学生 。随便用个有序的数据结构 , 优先队列、map等 很容易维护一个“能力b>=a 的 按花费c 排序的 有序集合" , 当然为了不浪费资源 , 应每个学生尽量工作满 t 天。 总体复杂度为:logm*max{ m , n*logn}.
参考代码:
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <map>
using namespace std;
typedef long long LL;
const LL INF = (LL)1e12;
const int maxn = 100050;
struct STU{
int b,c,id;
bool operator < (const STU & s) const {
return c < s.c || (c==s.c && id<s.id);
}
};
map<STU , int> mp;
typedef map<STU,int>::iterator MIT;
bool cmp(const STU &a,const STU & b) {
return a.b < b.b;
}
struct BUG{
int a ,id;
bool operator < (const BUG & s) const{
return a < s.a ;
}
};
STU stu[maxn];
BUG bug[maxn];
int N , M , S;
int ans[maxn];
bool judge(int times){
mp.clear();
LL pass = 0;
int p=M-1 , q=N-1;
MIT it;
for(;p>=0;p--){
while(q>=0 && stu[q].b >= bug[p].a){
mp[stu[q]] = times;
q--;
}
if(p==M-1 || it->second <= 0) {
if(p != M-1) mp.erase(it);
it = mp.begin() ;
if(it == mp.end()) return false;
pass += it->first.c;
}
ans[bug[p].id] = it->first.id;
it->second--;
}
return pass <= S ;
}
int main()
{
scanf("%d%d%d" , &N ,&M ,&S);
int max_a = 0;
for(int i=0;i<M;i++)
scanf("%d" , &bug[i].a),bug[i].id=i+1 ,max_a = max(max_a ,bug[i].a);
for(int i=0;i<N;i++)
scanf("%d" , &stu[i].b);
for(int i=0;i<N;i++)
scanf("%d" , &stu[i].c) ,stu[i].id = i+1;
bool ok = false;
for(int i=0;i<N;i++)
if(stu[i].b >= max_a && stu[i].c <= S) ok = true;
if(!ok) {
printf("NO\n") ; return 0;
}
else printf("YES\n");
sort(bug , bug+M);
sort(stu , stu+N ,cmp);
int L = 1 , H = M ,mid;
while(L<H){
mid = L + (H-L) / 2;
if(judge(mid)) H = mid;
else L = mid+1;
}
judge(L);
for(int i=1;i<M;i++) printf("%d " , ans[i]);
printf("%d\n" ,ans[M]);
return 0;
}