首页 > 其他 > 详细

V - stl 的 优先队列 Ⅱ

时间:2016-03-06 23:20:00      阅读:244      评论:0      收藏:0      [点我收藏+]

Description

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.        
                

Input

In the first line, there is an Integer T(1<=T<=10), which means the test cases in the input file. Then followed by T test cases.         For each test case, I will give you an Integer N(0<N<=100,000) in the first line, which means the number of stones on the road. Then followed by N lines and there are two integers Pi(0<=Pi<=100,000) and Di(0<=Di<=1,000) in the line, which means the position of the i-th stone and how far Sempr can throw it.        
                

Output

Just output one line for one test case, as described in the Description.        
                

Sample Input

2
2
1 5
2 4
2
1 5
6 6
                

Sample Output

11
12
 
 
该题使用优先队列
我的思路是,先将第一个石头和其移动后的位置存入队列,再用移动后的位置与第二个石头的位置比较,若第二个石头较远,则移动第二个石头,将其原始位置和移动后的位置都存入队列。若第一个石头较远,则应与第三个石头的位置进行比较,再决定移动哪个。
这样的思路没有考虑到若同一位置石头数目较多,则代码中的一个t则无法记录同一位置多的石头的运动距离。
所以导致结果错误。
#include<iostream>
#include<cstdio>
#include<queue>
using namespace std;
int pi[100005],di[100005];
int main() 
{
    int T;
    scanf("%d",&T);
    while(T--){
        priority_queue<int>p;
        int n,m,t,s;
        scanf("%d",&n);
        m=n;
        for(int i=0;i<n;i++)scanf("%d%d",&pi[i],&di[i]);
        for(int i=0;i<n;i++){ 
            if(i==0){                      //将第一个石头即其移动后的位置存入队列
                p.push(pi[i]);
                p.push(pi[i]+di[i]);
                t=di[i];
            }
            else{
                if(p.size()%2==0){                 //队列中有偶数个石头位置 
                    if(p.top()>pi[i]){
                        if(i!=n-1){
                            if(p.top()<pi[i+1]){
                                p.push(p.top()+t);
                            }
                            else {
                                p.push(pi[i]);
                                continue;              //应与下一个石头进行比较后再决定移动队列顶端石头还是下一个石头   
                            }
                        }
                        else{
                            p.push(p.top()+t);
                        }
                    }
                    else if(p.top()<pi[i]){
                        p.push(pi[i]+di[i]);
                        t=di[i];
                    }
                    else{                             //两者位置相同时
                        if(di[i]<t){
                            if(i!=n-1){    
                            if(p.top()<pi[i+1])p.push(p.top()+t);
                            else {
                                p.push(pi[i]);
                                continue;
                            }
                        }
                            else p.push(p.top()+t);
                        }
                        else{
                            p.push(pi[i]+di[i]);
                            t=di[i];
                        }
                    }
                    p.push(pi[i]);
                }
                else{
                    if(p.top()>pi[i]){
                        p.push(pi[i]+di[i]);
                        if(pi[i]+di[i]>p.top())t=di[i];
                    }
                    else if(p.top()<pi[i]){
                        if(i!=n-1){
                            if(p.top()<pi[i+1])p.push(p.top()+t);
                            else {
                                p.push(pi[i]);
                                continue;
                            }
                        }
                        else p.push(p.top()+t);
                    }
                    else{
                        if(di[i]<t){
                            p.push(pi[i]+di[i]);
                            t=di[i];
                        }
                        else{
                            if(i!=n-1){
                            if(p.top()<pi[i+1])p.push(p.top()+t);
                            else {
                                p.push(pi[i]);
                                continue;
                            }
                        }
                        else p.push(p.top()+t);
                        }
                    }
                    p.push(pi[i]);
                }
            }
        }
        if(p.size()%2==1)p.push(p.top()+t);
        cout<<p.top()<<endl;
    }
    //system("pause");
    return 0;
}

解决同一位置多个(超过两个)石头的问题,应运用结构体比较简单

以下正确代码中重载“<",使队列优先级如此排列

#include<cstdio>
#include<queue>
using namespace std;
struct Stone{
    int pi;           //石头的初始地
    int di;          //石头能扔的最远距离
};
bool operator<( Stone a, Stone b )
{                                            //重载小于,按照结构体中x小的在队顶,如果x一样,则按照y的最小的在//队顶
    if( a.pi== b.pi ) return a.di > b.di;  
    return a.pi > b.pi;   
}  
int main()
{
    int t;
    scanf("%d",&t);//测试数据个数
    while(t--)
    {
        int n,i ;
        priority_queue<Stone>q;     //定义一个Stone成员的优先//队列
        scanf("%d",&n);
        Stone tmp;                      //结构体对象
        for(i =0;i<n ; i++ )
        {
            scanf("%d%d",&tmp.pi,&tmp.di);
            q.push(tmp);
        }
        int sum =1;                        //判断碰到的是第几个石头的标记
        while(!q.empty())                   //当队列为空就跳出循环,也就是说再//向前就没有石头可以遇到
        {
        tmp = q.top();  
        //cout<<"-"<<tmp.pi<<" *"<<endl;      
            q.pop();           //去队顶元素,也就是在后面的所有//石头中第一个碰到的石头
            if(sum%2)
            {                         //如果是奇数号石头,则处理,否则不做处理
                tmp.pi+=tmp.di;               //则向前扔y单位长度
                q.push(tmp);                //扔出去的石头入队
            }
            sum++;                             //石头计数+1
        }
        printf("%d\n",tmp.pi);
    }
    //system("pause");
    return 0;
}

 

V - stl 的 优先队列 Ⅱ

原文:http://www.cnblogs.com/farewell-farewell/p/5248909.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!