很简单的模拟,我是用的优先队列。不多说,上代码(这是bjfuoj的,hdu的要稍改一下):
/*
* Author : ben
*/
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <queue>
#include <set>
#include <map>
#include <stack>
#include <string>
#include <vector>
#include <deque>
#include <list>
#include <functional>
#include <numeric>
#include <cctype>
using namespace std; typedef struct Stone {
int p, d;
Stone(int pp, int dd) {
p = pp;
d = dd;
}
} Stone; inline bool operator<(const Stone &s1, const Stone &s2) {
if (s1.p != s2.p) {
return s1.p > s2.p;
}
return s1.d > s2.d;
} //输入非负整数,用法int a = get_int();
int get_int() {
int res = , ch;
while (!((ch = getchar()) >= '' && ch <= '')) {
if (ch == EOF)
return -;
}
res = ch - '';
while ((ch = getchar()) >= '' && ch <= '')
res = res * + (ch - '');
return res;
} int main() {
int n, p, d, ans;
bool flag;
while ((n = get_int()) > ) {
priority_queue<Stone> pq;
for (int i = ; i < n; i++) {
p = get_int();
d = get_int();
pq.push(Stone(p, d));
}
ans = ;
flag = true;
while (!pq.empty()) {
Stone s = pq.top();
ans = s.p;
pq.pop();
if (flag) {
s.p += s.d;
pq.push(s);
}
flag = !flag;
}
printf("%d\n", ans);
}
return ;
}