hdu 1896 Stones

题目来源:HDU 1896

 简单题目分析:
路上有许多石头,遇到第奇数个石头将其向前踢,第偶数个不做改变,问最后一个石头的位置。注意,如果有多个石头在同一个位置,则将最重的(向前踢移动的距离最小的)石头视作是在这个位置第一个遇到的。
思路:
典型的优先队列。
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <queue>

using namespace std;


struct stone
{
    int pos;   //石头的位置
    int distance; //每个石头可以被扔多远
}temp;

bool operator <(stone a,stone b)
{
    if(a.pos==b.pos)
        return a.distance>b.distance; //结构体排序,有些区别,这是pos和distance都是以小值优先
    else
        return a.pos>b.pos;
}

int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        priority_queue <stone> p;   //优先队列,以结构体为元素
        int n;
        scanf("%d",&n);
        int i;
        for(i=0;i<n;++i)
        {
            int a,b;
            scanf("%d %d",&a,&b);
            temp.pos=a;
            temp.distance=b;
            p.push(temp);
        }
        int index=1; //标记第几个石头
        stone top;
        while(!p.empty())
        {
            top=p.top();
            p.pop();
            if(index%2) //判断是不是第奇数个石头
            {
               top.pos+=top.distance;
               p.push(top);
            }
            index++;
        }
        printf("%d\n",top.pos);
    }
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值