您的位置:首页 > 其它

hdoj 1896 Stones 【优先队列】

2016-03-12 18:59 453 查看
题目链接:hdoj 1896 Stones

题意:给你n个石头以及每个石头可以扔出去的距离,碰到第i个石头,处理有——i为奇数,扔出去;反之忽略。问最后一个石头距离起点的距离,相同位置的石头优先可扔距离小的。

AC代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <vector>
#include <queue>
#include <map>
#define PI acos(-1.0)
#define CLR(a, b) memset(a, (b), sizeof(a))
#define fi first
#define se second
using namespace std;
typedef long long LL;
typedef pair<int, int> pii;
const int MAXN = 1e5+10;
const int INF = 0x3f3f3f3f;
const int MOD = 1e9 + 7;
void getmax(int &a, int b) {a = max(a, b); }
void getmin(int &a, int b) {a = min(a, b); }
void add(LL &x, LL y) { x += y; x %= MOD; }
struct cmp {
bool operator()(pii a, pii b) {
return a.fi != b.fi ? a.fi > b.fi : a.se > b.se;
}
};
priority_queue<pii, vector<pii>, cmp > Q;
int main()
{
//freopen("in.txt", "r", stdin);
//freopen("out.txt", "w", stdout);
int t; cin >> t;
while(t--)
{
int n; cin >> n;
for(int i = 0; i < n; i++) {
int u, v;
cin >> u >> v;
Q.push(pii(u, v));
}
int id = 0; int ans = 0;
while(!Q.empty())
{
pii u = Q.top(); Q.pop();
++id;
ans = max(ans, u.fi);
if(id & 1) {
Q.push(pii(u.fi+u.se, u.se));
}
}
cout << ans << endl;
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: