hdu 4585 set + pair
2015-08-24 15:43
651 查看
题意:
少林方丈丢了少林的交战谱,现在要你来还原。
方丈的攻击力为1000000000,他的id为1。
每个和尚都有不同的攻击力和id,他们入少林的时候会与前辈发生一场战斗,挑选对象的时候,他会选择与他攻击力差的绝对值最小的前辈来打。
当有俩前辈与其攻击力差值的绝对值相等的时候,他会选择攻击力小的那个前辈来干。
现在按照入少林时间顺序给出这些和尚的攻击力和id,让你来还原少林的交战谱。
解析:
用一个set来维护当前时间点少林里面有什么和尚,然后lower_bound表示大于或等于当前攻击力的和尚,迭代器往后减,比较一下就ok了。
代码:
少林方丈丢了少林的交战谱,现在要你来还原。
方丈的攻击力为1000000000,他的id为1。
每个和尚都有不同的攻击力和id,他们入少林的时候会与前辈发生一场战斗,挑选对象的时候,他会选择与他攻击力差的绝对值最小的前辈来打。
当有俩前辈与其攻击力差值的绝对值相等的时候,他会选择攻击力小的那个前辈来干。
现在按照入少林时间顺序给出这些和尚的攻击力和id,让你来还原少林的交战谱。
解析:
用一个set来维护当前时间点少林里面有什么和尚,然后lower_bound表示大于或等于当前攻击力的和尚,迭代器往后减,比较一下就ok了。
代码:
#pragma comment(linker, "/STACK:1677721600") #include <map> #include <set> #include <cmath> #include <queue> #include <stack> #include <vector> #include <cstdio> #include <cstdlib> #include <cstring> #include <climits> #include <cassert> #include <iostream> #include <algorithm> #define pb push_back #define mp make_pair #define LL long long #define lson lo,mi,rt<<1 #define rson mi+1,hi,rt<<1|1 #define Min(a,b) ((a)<(b)?(a):(b)) #define Max(a,b) ((a)>(b)?(a):(b)) #define mem(a,b) memset(a,b,sizeof(a)) #define FIN freopen("in.txt", "r", stdin) #define FOUT freopen("out.txt", "w", stdout) #define rep(i,a,b) for(int i=(a); i<=(b); i++) #define dec(i,a,b) for(int i=(a); i>=(b); i--) using namespace std; const int mod = 1e9 + 7; const double eps = 1e-8; const double ee = exp(1.0); const int inf = 0x3f3f3f3f; const int maxn = 1e6 + 10; const double pi = acos(-1.0); const LL iinf = 0x3f3f3f3f3f3f3f3f; int readT() { char c; int ret = 0,flg = 0; while(c = getchar(), (c < '0' || c > '9') && c != '-'); if(c == '-') flg = 1; else ret = c ^ 48; while( c = getchar(), c >= '0' && c <= '9') ret = ret * 10 + (c ^ 48); return flg ? - ret : ret; } // fight <-> id set<pair<int, int> > monk; set<pair<int, int> >::iterator lo, hi; vector<pair<int, int> > ans; int main() { #ifdef LOCAL FIN; #endif // LOCAL int n; while (~scanf("%d", &n) && n) { monk.clear(); ans.clear(); monk.insert(mp(-1, -1)); monk.insert(mp(1000000000, 1)); rep(i, 0, n - 1) { int id = readT(); int fight = readT(); lo = hi = monk.lower_bound(mp(fight, id)); lo--; if (lo -> second == -1) { monk.insert(mp(fight, id)); ans.pb(mp(id, hi -> second)); continue; } if (abs(lo -> first - fight) < abs(hi -> first - fight)) { ans.pb(mp(id, lo -> second)); } else if (abs(lo -> first - fight) > abs(hi -> first - fight)) { ans.pb(mp(id, hi -> second)); } else { if (lo -> first < hi -> first) { ans.pb(mp(id, lo -> second)); } else { ans.pb(mp(id, hi -> second)); } } monk.insert(mp(fight, id)); } int sz = ans.size(); rep(i, 0, sz - 1) { printf("%d %d\n", ans[i].first, ans[i].second); } } return 0; }
相关文章推荐
- [LeedCode OJ]#219 Contains Duplicate II
- [LeedCode OJ]#217 Contains Duplicate
- ORA-15081: failed to submit an I/O operation to a disk
- [LeedCode OJ]#172 Factorial Trailing Zeroes
- Codeforces Round #317 [AimFund Thanks-Round] (Div. 2) C(组合数+容斥)
- ubuntu编译airplay
- [LeedCode OJ]#70 Climbing Stairs
- svn cleanup failed–previous operation has not finished; run cleanup if it was interrupted
- contains选择器
- WebLogic11g-半小时让你的domain集群化
- hdu5009 Paint Pearls(离散+线性dp)
- 木马常用AIP函数
- HDU 1039.Easier Done Than Said?【字符串处理】【8月24】
- ORA-09925: Unable to create audit trail file
- POJ 1995 Raising Modulo Numbers(快速幂,同余定理)
- 模型选择准则之AIC和BIC
- Raising Modulo Numbers(poj 1995 快速幂)
- Raising Modulo Numbers(poj 1995 快速幂)
- assign、retain、copy使用异同
- POJ--1995--Raising Modulo Numbers