题目链接:
https://acm.bnu.edu.cn/v3/problem_show.php?pid=52308
We don't wanna work!
Time Limit: 60000msMemory Limit: 524288KB
题意
acm协会的人员按努力值排序,只有前20%(向下取整)的人努力工作,现在会有人员变动(增加一个人进来或减少一个人),人员变动会导致一些人从不努力变成努力或从努力变成不努力,现在给你人员的变动情况,输出对于的日志。
增加一个人:先输出这个人归属,然后输出他加入之后引起的某个人的归属变化。
减少一个人:输出一个人走了以后引起的某个人的归属变化。
题解
用两个set维护下,一个set放不努力,另一个放努力。模拟下就可以了。
插入:先比较不努力人里面最努力的,如果没他努力,就扔不努力里面,否则就扔努力里面,然后调整不努力,努力使得人数比例满足要求。
删除:先看看要删的是努力还是不努力,然后在对应的集合里面删掉,删完之后调整。
代码
#include<map>
#include<set>
#include<cmath>
#include<queue>
#include<stack>
#include<ctime>
#include<vector>
#include<cstdio>
#include<string>
#include<bitset>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<functional>
#include<sstream>
using namespace std;
#define X first
#define Y second
#define mkp make_pair
#define lson (o<<1)
#define rson ((o<<1)|1)
#define mid (l+(r-l)/2)
#define sz() size()
#define pb(v) push_back(v)
#define all(o) (o).begin(),(o).end()
#define clr(a,v) memset(a,v,sizeof(a))
#define bug(a) cout<<#a<<" = "<<a<<endl
#define rep(i,a,b) for(int i=a;i<(b);i++)
#define scf scanf
#define prf printf
typedef long long LL;
typedef vector<int> VI;
typedef pair<int,int> PII;
typedef vector<pair<int,int> > VPII;
const int INF=0x3f3f3f3f;
const LL INFL=0x3f3f3f3f3f3f3f3fLL;
const double eps=1e-8;
const double PI = acos(-1.0);
//start----------------------------------------------------------------------
map<string, pair<int,int> > mp;
const char msg[2][33]= {"is not working now.","is working hard now."};
struct Node {
int v,t;
string nam;
Node(int v,int t,string nam):v(v),t(t),nam(nam) {}
Node() {}
bool operator <(const Node& tmp)const {
return v>tmp.v||v==tmp.v&&t>tmp.t;
}
};
struct Node2 {
int v,t;
string nam;
Node2(int v,int t,string nam):v(v),t(t),nam(nam) {}
Node2() {}
bool operator <(const Node2& tmp)const {
return v<tmp.v||v==tmp.v&&t<tmp.t;
}
};
int main() {
int n;
set<Node> s1;
set<Node2> s2;
int clk=0,tot=0;
scf("%d",&n);
rep(i,0,n) {
char nam[33];
int v;
scf("%s%d",nam,&v);
mp[nam]=mkp(v,++clk);
if(s1.sz()==0||*s1.begin()<Node(v,clk,nam)) {
s1.insert(Node(v,clk,nam));
} else {
s2.insert(Node2(v,clk,nam));
}
tot++;
int cnt=(int)(tot*0.2+eps);
while(s2.sz()>cnt) {
Node2 x=*s2.begin();
s1.insert(Node(x.v,x.t,x.nam));
s2.erase(s2.begin());
}
while(s2.sz()<cnt) {
Node x=*s1.begin();
s2.insert(Node2(x.v,x.t,x.nam));
s1.erase(s1.begin());
}
}
int m;
scf("%d",&m);
while(m--) {
char nam[33],cmd[2];
int v;
scf("%s%s",cmd,nam);
if(cmd[0]=='+') {
scf("%d",&v);
mp[nam]=mkp(v,++clk);
int flag,flag2=-1;
string buf;
if(s1.sz()==0||*s1.begin()<Node(v,clk,nam)) {
s1.insert(Node(v,clk,nam));
flag=0;
} else {
s2.insert(Node2(v,clk,nam));
flag=1;
}
tot++;
int cnt=(int)(tot*0.2+eps);
while(s2.sz()>cnt) {
Node2 x=*s2.begin();
if(x.v==v&&x.t==clk) flag=0;
else {
flag2=0;
buf=x.nam;
}
s1.insert(Node(x.v,x.t,x.nam));
s2.erase(s2.begin());
}
while(s2.sz()<cnt) {
Node x=*s1.begin();
if(x.v==v&&x.t==clk) flag=1;
else {
flag2=1;
buf=x.nam;
}
s2.insert(Node2(x.v,x.t,x.nam));
s1.erase(s1.begin());
}
prf("%s %s\n",nam,msg[flag]);
if(flag2>=0) prf("%s %s\n",buf.c_str(),msg[flag2]);
} else {
pair<int,int> x=mp[nam];
Node nd=*s1.begin();
if(x.X<nd.v||x.X==nd.v&&x.Y<=nd.t) {
set<Node>::iterator it=s1.lower_bound(Node(x.X,x.Y,nam));
if(it!=s1.end()) s1.erase(it);
} else {
set<Node2>::iterator it=s2.lower_bound(Node2(x.X,x.Y,nam));
if(it!=s2.end()) s2.erase(it);
}
tot--;
int cnt=(int)(tot*0.2+eps);
while(s2.sz()>cnt) {
Node2 x=*s2.begin();
prf("%s %s\n",x.nam.c_str(),msg[0]);
s1.insert(Node(x.v,x.t,x.nam));
s2.erase(s2.begin());
}
while(s2.sz()<cnt) {
Node x=*s1.begin();
prf("%s %s\n",x.nam.c_str(),msg[1]);
s2.insert(Node2(x.v,x.t,x.nam));
s1.erase(s1.begin());
}
}
}
return 0;
}
//end-----------------------------------------------------------------------