P5250 【深基17.例5】木材仓库
#include<bits/stdc++.h>
using namespace std;
void solve(){ios::sync_with_stdio(false), cin.tie(0), cout.tie(0); int n, length, type;set<int> a;set<int>::iterator it, itup;cin >> n;for(int i = 0; i < n; i++){cin >> type >> length;it = find(a.begin(), a.end(), length);if(type == 1){if(it == a.end())a.insert(length);else cout << "Already Exist" << endl;}else if(type == 2){if(a.empty())cout << "Empty" << endl;else if(it != a.end()){cout << length << endl;a.erase(it);}else{itup = upper_bound(a.begin(), a.end(), length);if(itup == a.end()){--itup;cout << *itup << endl;a.erase(itup);}else if(itup == a.begin()){cout << *itup << endl;a.erase(itup);}else{it = --itup;itup++;if(length - *it <= *itup - length){cout << *it << endl;a.erase(it);}else{cout << *itup << endl;a.erase(itup);}}}}}
}
signed main(){ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);solve(); return 0;
}
P5266 【深基17.例6】学籍管理
#include<bits/stdc++.h>
using namespace std;void solve(){int n; cin >> n;string s;map<string, int> m;while(n--){int x; cin >> x;if(x == 1){int score;cin >> s >> score;m[s] = score;cout << "OK" << endl;}else if(x == 2){cin >> s;if(m.count(s))cout << m[s] << endl;else cout << "Not found" << endl;}else if(x == 3){cin >> s;if(m.count(s)){m.erase(s);cout << "Deleted successfully" << endl;}else cout << "Not found" << endl;}else{cout << m.size() << endl;}}}
signed main(){ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);solve(); return 0;
}
P1102 A-B 数对
#include<bits/stdc++.h>
using namespace std;void solve(){int n, c;cin >> n >> c;unordered_map<int, int> um;while(n--){int x; cin >> x;um[x]++;}long long sum = 0;for(unordered_map<int, int>::iterator it = um.begin(); it != um.end(); it++){int lenb = it -> second;int a = it -> first + c;if(um.count(a)){int lena = um[a];sum += (long long)lena * lenb;} }cout << sum << endl;
}
signed main(){ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);solve(); return 0;
}
P3879 [TJOI2010] 阅读理解
#include<bits/stdc++.h>
using namespace std;void solve(){int n; cin >> n;map<string, set<int> > a;for(int i = 1; i <= n; i++){int x; cin >> x;for(int j = 1; j <= x; j++){string s; cin >> s;a[s].insert(i);}}int q; cin >> q;while(q--){string s; cin >> s;set<int> se = a[s];for(set<int>::iterator is = se.begin(); is != se.end(); is++){cout << *is << " ";}cout << endl;}
}
signed main(){ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);solve(); return 0;
}
P1918 保龄球
#include<bits/stdc++.h>
using namespace std;void solve(){int n; cin >> n;map<int, int> mp;for(int i = 1; i <= n; i++){int x; cin >> x;mp[x] = i;}int q; cin >> q;while(q--){int y; cin >> y;if(mp.count(y))cout << mp[y] << endl;else cout << 0 << endl;}
}
signed main(){ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);solve(); return 0;
}
P4305 [JLOI2011] 不重复数字
#include<bits/stdc++.h>
using namespace std;void solve(){int t; cin >> t;map<int, int> mp;while(t--){mp.clear();int n; cin >> n;for(int i = 1; i <= n; i++){int x; cin >> x;if(!mp[x]){cout << x << " ";mp[x] = 1;}}cout << endl; }
}
signed main(){ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);solve();return 0;
}