您的位置:首页 > 其它

HDU1896Stones(优先队列)

2014-03-01 21:09 176 查看
地址http://acm.hdu.edu.cn/showproblem.php?pid=1896

题目大一比较简单,就是说在一条直线道路上有n个石头,往前走,遇到一个数一个,如果遇到的是第奇数个那就把这个石头往前扔距离D[i], 如果是第偶数个,就放置不管。

问遇到的最后一个石头距离出发点的位置是多少。

做起来也比较简单就是每遇到第奇数个石头,就将其加上D[i],放回到优先队列(priority_queue)中,然后再去掉一个石头

直接看代码:



#include <map>
#include <set>
#include <stack>
#include <queue>
#include <cmath>
#include <ctime>
#include <vector>
#include <cstdio>
#include <cctype>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
using namespace std;
#define INF 0x3f3f3f3f
#define MAX(a,b) (a > b ? a : b)
#define MIN(a,b) (a < b ? a : b)
#define mem0(a) memset(a,0,sizeof(a))
#define mem1(a) memset(a,-1,sizeof(a))
#define lson k<<1, L, mid
#define rson k<<1|1, mid+1, R

typedef long long LL;
const double eps = 1e-12;
const int MAXN = 100005;
const int MAXM = 500005;

struct Node
{
int P, D;
};

struct cmp
{
bool operator () (const Node a, const Node b)
{
if(a.P != b.P)return a.P > b.P;
else return a.D > b.D;
}
};

int T, N;
Node stone;

int main()
{
scanf("%d", &T);
while(T--)
{
scanf("%d", &N);
priority_queue<Node, vector<Node>, cmp>q;
for(int i=0;i<N;i++)
{
scanf("%d %d", &stone.P, &stone.D);
q.push(stone);
}
int isThrow=1;
while(!q.empty())
{
stone = q.top(); q.pop();
if(isThrow)
{
stone.P += stone.D;
q.push(stone);
}
isThrow = !isThrow;
}
printf("%d\n", stone.P);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: