hdu 1896 Stones

xiaoxiao2021-02-27  435

题目来源: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; }
转载请注明原文地址: https://www.6miu.com/read-526.html

最新回复(0)