Đề bài:
Thuật toán:
- Nếu bạn sử dụng C++ thì có thể dùng cấu trúc dữ liệu có sẵn là std::set, còn nếu dùng Pascal thì sẽ phải code dài hơn một chút.
- Toàn bộ về std::set cho bạn nào dùng C++: http://yeulaptrinh.de/938/set-trong-c/
Code:
#include
using namespace std;
#define FOR(i,a,b) for (int i=(a),_b=(b);i<=_b;i=i+1)
#define FORD(i,b,a) for (int i=(b),_a=(a);i>=_a;i=i-1)
#define REP(i,n) for (int i=0,_n=(n);i<_n;i=i+1)
#define FORE(i,v) for (__typeof((v).begin()) i=(v).begin();i!=(v).end();i++)
#define ALL(v) (v).begin(),(v).end()
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define SZ(x) ((int)(x).size())
#define double db
typedef long long ll;
typedef pair PII;
const ll mod=1000000007;
ll powmod(ll a,ll b) {ll res=1;a%=mod; assert(b>=0); for(;b;b>>=1){if(b&1)res=res*a%mod;a=a*a%mod;}return res;}
const int MAXN = 1E6+3;
const int oo = 1e9+3;
set s;
set::iterator it;
int p, k, x, i;
int main() {
#ifndef ONLINE_JUDGE
freopen("test.inp", "r", stdin);
freopen("test.out", "w", stdout);
#endif
cin >> x;
while (x != 0) {
//
if (x == 1) {
cin >> k >> p;
s.insert(PII(p,k));
}
//
if (x == 2)
if (!s.empty()) {
it = s.end(); -- it;
cout << it -> se << endl;
s.erase(it);
} else cout << 0 << endl;
//
if (x==3)
if (!s.empty()) {
it = s.begin();
cout << it -> se << endl;
s.erase(it);
} else cout << 0 << endl;
//
cin >> x;
}
return 0;
}
Recent Comments