思路
记录怪物的第一次第二次出现时间
can[]表示叶妹妹是否具有攻击这个怪物的能力
num[]表示叶妹妹不能攻击该怪物的数量
sum表示叶妹妹能杀怪物的数量根据贪心策略,每一次泽鸽鸽优先攻击自己没有攻击过且第二次出现早的怪物,这样子叶妹妹就越早能杀怪
如果这一时刻不存在泽鸽鸽优先没有攻击过的怪物,那么泽哥哥就随便杀一只
代码
// Problem: 模仿游戏
// Contest: NowCoder
// URL: https://ac.nowcoder.com/acm/contest/9985/G
// Memory Limit: 524288 MB
// Time Limit: 6000 ms
//
// Powered by CP Editor (https://cpeditor.org)
#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define mp(aa,bb) make_pair(aa,bb)
#define _for(i,b) for(int i=(0);i<(b);i++)
#define rep(i,a,b) for(int i=(a);i<=(b);i++)
#define per(i,b,a) for(int i=(b);i>=(a);i--)
#define mst(abc,bca) memset(abc,bca,sizeof abc)
#define X first
#define Y second
#define lowbit(a) (a&(-a))
#define debug(a) cout<<#a<<":"<<a<<"\n"
typedef long long ll;
typedef pair<int,int> pii;
typedef unsigned long long ull;
typedef long double ld;
const int N=100010;
const int INF=0x3f3f3f3f;
const int mod=1e9+7;
const double eps=1e-6;
const double PI=acos(-1.0);
int fi[N],se[N];
int num[N];
vector<int> g[N];
bool can[N];
void init(){
rep(i,1,1e5){
fi[i]=se[i]=INF;
num[i]=can[i]=0;
g[i].clear();
}
}
struct node{
int time,id;
bool operator <(const node & t)const{
return time>t.time;
}
};
void solve(){
init();
priority_queue<node> q;
int n;cin>>n;
int ans=0,sum=0;
while(n--){
int a,b;cin>>a>>b;
g[a].pb(b);
ans=max(ans,a);
if(fi[b]>a) se[b]=fi[b],fi[b]=a;
else if(se[b]>a) se[b]=a;
}
rep(t,1,ans){
for(int x:g[t]){
if(t==fi[x]){
q.push({se[x],x});
fi[x]=0;
}
if(can[x]) sum++;
else num[x]++;
}
sum=max(sum-1,0); //叶妹妹攻击
if(!q.empty()){ //泽鸽鸽攻击
auto t=q.top();q.pop();
can[t.id]=1;
sum+=num[t.id];
sum--;
}
else sum=max(sum-1,0);
}
while(1){
if(q.empty()&&!sum) break;
sum=max(sum-1,0); //叶妹妹攻击
if(!q.empty()){ //泽鸽鸽攻击
auto t=q.top();q.pop();
can[t.id]=1;
sum+=num[t.id];
sum--;
}
else sum=max(sum-1,0);
ans++;
}
cout<<ans<<"\n";
}
int main(){
ios::sync_with_stdio(0);cin.tie(0);
int t;cin>>t;while(t--)
solve();
return 0;
}

京公网安备 11010502036488号