有n个怪物,每个怪物都有一个能力值a。有m个勇士,每个勇士都有一个力量P和耐力S。每天可以选择一个勇士去打怪物,必须按顺序打,如果这个勇士的能力P>=a那么就可以打败这个怪物,就必须打下一个怪物,而且最多一天只能打S个怪物。打一个怪物耐力值-1,如果力量P<a或者耐力值为0,这个勇士就得回来。这天结束,每个勇士可以使用无数次。问最少需要多少天,能把所有的怪物全部打败。
贪心即可
AC代码:
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<vector>
#include<stdlib.h>
#include<queue>
#include<map>
#include<set>
#include<iomanip>
#include<math.h>
using namespace std;
typedef long long ll;
typedef double ld;
const int INF = 0x3f3f3f3f;
const int N = 200005;
int i,j,k;
int ans,res,cnt,temp,pos;
int n,m;
int max1,max2,max3,now;
int mon[N],hp[N],nai[N];
struct node
{
int p,s;
} hero[N];
bool cmp(node a, node b)
{
return a.p<b.p;
}
int main()
{
int t;
cin>>t;
while(t--)
{
max1=0;
max2=0;
cin>>n;
for(i=1; i<=n; i++)
{
cin>>mon[i];
max2=max(max2,mon[i]);
}
cin>>m;
for(i=1; i<=m; i++)
{
cin>>hero[i].p>>hero[i].s;
}
sort(hero+1,hero+1+m,cmp);
if(max2>hero[m].p)
{
cout<<-1<<endl;
continue;
}
for(i=m; i>=1; i--)
{
max1=max(hero[i].s,max1);
nai[i]=max1;
}
for(i=1; i<=m; i++)
hp[i]=hero[i].p;
ans=1;
max3=0;
now=1;
for(i=1; i<=n; i++)
{
max3=max(mon[i],max3);///贪心
pos=lower_bound(hp+1,hp+1+m,max3)-hp;
if(nai[pos]<(i-now+1))
{///后面所有的勇士都不能一个人打死所有的怪必须下一天
now=i;
max3=mon[i];
ans++;
}
}
cout<<ans<<endl;
}
return 0;
}