二、stl ,模拟,贪心等 [Cloned] W - stl 的 优先队列

原题:

Because of the wrong status of the bicycle, Sempr begin to walk east to west every morning and walk back every evening. Walking may cause a little tired, so Sempr always play some games this time. 
There are many stones on the road, when he meet a stone, he will throw it ahead as far as possible if it is the odd stone he meet, or leave it where it was if it is the even stone. Now give you some informations about the stones on the road, you are to tell me the distance from the start point to the farthest stone after Sempr walk by. Please pay attention that if two or more stones stay at the same position, you will meet the larger one(the one with the smallest Di, as described in the Input) first. 

题意:

从起点开始走会不断碰到石头,如果碰到的石头是第奇数块,就将石头扔出去,如果是第偶数块就不管他继续走,问总开始走到最后一块石头的距离是多少。

题解:

优先队列的简单应用,结构体内包括石头的位置和这块石头能扔多远,按照石头由近到远的顺序,以及如果位置相同则先碰到扔的近的那块石头来定义优先队列,不断出队然后判断是否是第奇数次碰到然后进队,直到队列为空,则最后的位置就是答案。

代码:AC

#include<cstdio>
#include<queue>
#include<algorithm>
#include<iostream>
using namespace std;
struct node{
    int a, b;
    bool friend operator < (node x,node y) {
        if(x.a != y.a) {
            return x.a > y.a;
        }
        return x.b > y.b;
    }
};
node p;
int main()
{
    int T;
	cin>>T;
    while(T--) {
        priority_queue<node> fq;
        int n;
		cin>>n;
        for(int i = 0;i < n; i++) {
			cin>>p.a>>p.b;
            fq.push(p);
        }
        int k = 1;
        while(!fq.empty()) {
            p = fq.top();
            fq.pop();
            if(k%2) {
                p.a += p.b;
                fq.push(p);
            }
            k++;
        }
		cout<<p.a<<endl;
    }
return 0;
}

猜你喜欢

转载自blog.csdn.net/npuyan/article/details/81413644
今日推荐