中等的模拟题,自己写的太搓了。
- 自己原来写了离散化,后来删了
- 原来超时,后来加了一个很小的优化
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<queue>
#include<cmath>
#include<map>
#include<stack>
#include<set>
#include<bitset>
#include<unordered_map>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
#define pb(x) push_back(x)
#define cls(x, val) memset(x, val, sizeof(x))
#define fi first
#define se second
#define mp(x, y) make_pair(x, y)
#define inc(i, l, r) for(int i=l; i<=r; i++)
const int inf = 0x3f3f3f3f;
const int maxn = 3e5+10;
int a[maxn];
int price[200000];
unordered_map<int, int> mp;
int m, n;
int col;
int b[maxn];
int op_num;
struct Node{
int id;
int price;
int type;
bool operator < (const Node &b) const {
if(price!=b.price) return price>b.price;
else if(type != b.type) return type<b.type;
return id<b.id;
}
};
set<Node> st[55];
int k[60];
void add(int type, int commodity, int score){
st[type].insert(Node{commodity, score, type});
}
void del(int type, int commodity){
if(st[type].count(Node{commodity, mp[commodity], type})!=0)
st[type].erase(Node{commodity, mp[commodity], type});
}
void process(int lim, int type){
if(st[type].empty()){
cout<<-1<<"
";
return ;
}
set<Node>::iterator it = st[type].begin();
for(; it!=st[type].end(); it++){
lim--;
if(lim==0){
cout<<it->id<<"
";
break;
}
else {
cout<<it->id<<" ";
}
}
}
int main(){
ios::sync_with_stdio(false);
cin>>m>>n;
for(int i=1; i<=n; i++){
cin>>a[i]>>price[i];
mp[a[i]] = price[i];
}
for(int i=0; i<m; i++){
for(int j=1; j<=n; j++){
st[i].insert(Node{a[j], price[j], i});
}
}
cin>>op_num;
int op, type, commodity, score;
for(int i=1; i<=op_num; i++){
cin>>op;
if(op == 1){
cin>>type>>commodity>>score;
mp[commodity] = score;
add(type, commodity, score);
}
else if(op == 2){
cin>>type>>commodity;
del(type, commodity);
}
else{
int K;
cin>>K;
int tot = 0;
for(int j=0; j<m; j++) cin>>k[j], tot += k[j];
if(K>=tot)
for(int j=0; j<m; j++){
process(k[j], j);
}
else{
vector<Node> pq;
pq.clear();
set<Node>::iterator it;
for(int j=0; j<m; j++){
int cnt = 0;
for(it=st[j].begin(); it!=st[j].end()&&cnt<min(K, k[j]); it++){
pq.push_back(*it);
cnt++;
}
}
// cout<<"--------------"<<endl;
// for(int j=0; j<pq.size(); j++){
// cout<<pq[j].id<<endl;
// }
// cout<<"---------------"<<endl;
sort(pq.begin(), pq.end());
vector<int> ans[60];
for(int j=0; j<60; j++) ans[j].clear();
for(int j=0; j<K; j++){
int cnt = 0;
for(int s=0; s<m; s++){
if(st[s].count(pq[j])!=0){
ans[s].push_back(pq[j].id);
}
}
}
for(int j=0; j<m; j++){
if(ans[j].size() == 0){
cout<<-1<<"
";
}
else{
int sz = ans[j].size();
for(int s=0; s<sz-1; s++) cout<<ans[j][s]<<" ";
cout<<ans[j][sz-1]<<"
";
}
}
}
}
}
return 0;
}
/*
2 3
1 3
2 2
3 1
8
3 100 1 1
1 0 4 3
1 0 5 1
3 10 2 2
3 10 1 1
2 0 1
3 2 1 1
3 1 1 1
*/