hdoj 1896 Stones 【优先队列】

来源:互联网 发布:美国反倾销数据库 编辑:程序博客网 时间:2024/06/04 19:56

题目链接: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 secondusing 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;}
0 0
原创粉丝点击