zoukankan      html  css  js  c++  java
  • HDU 1896 Stones 优先队列

    Stones

    Time Limit: 5000/3000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)
    Total Submission(s): 455    Accepted Submission(s): 261


    Problem 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
     
      1 /*
      2 优先队列的应用
      3 题意没说清楚,看半天别人写的才明白----当遇到一个位置上有多个石子的时候,要把所有石子都遍历完,只仍遇见
      4 的为奇数的,偶数的要抛弃,总之是要把这个地方的石子都过一遍
      5 */
      6 /*
      7 //代码一:
      8 #include <iostream>
      9 #include <queue>
     10 using namespace std;
     11 
     12 struct node
     13 {
     14     int position; //目前距离起点的距离 
     15     int length; //可以扔多远 
     16 };
     17 
     18 struct cmp
     19 {
     20     bool operator () (const node &a,const node &b)
     21     {
     22         if(a.position<b.position) //将石子按离距离原点的距离排序 ,从小到大 
     23             return false ;
     24         else if(a.position == b.position ) 
     25         {
     26             if(a.length<b.length) //当石子的距离相同是,按可以扔的距离排序 也是从小到大 
     27                 return false;
     28             else 
     29                 return true;
     30         }
     31         return true;
     32     }
     33 };
     34 
     35 priority_queue<node,vector<node>,cmp> s; //建立结构体的优先队列 
     36 
     37 int main()
     38 {
     39     int n,m;
     40     node current;
     41     cin>>n;
     42     while(n--)
     43     {
     44         cin>>m;
     45         while(m--)
     46         {
     47             cin>>current.position>>current.length;
     48             s.push(current); //向队列中加入元素 
     49         }
     50         while(!s.empty())
     51         {
     52             current=s.top(); //遇到奇数的石子的时候,将石子的属性赋到 current 中 
     53             current.position=current.length+current.position; //同时将 position + length ,将石子扔出去后的距离 
     54             s.push(current); //将扔之后的石子放入队列中 
     55             s.pop();  //先弹出已经扔出去的石子
     56             s.pop();  //再弹出去偶数位的石子,所有要弹出两次 
     57         }
     58         cout<<current.position<<endl; //最后的一个石子就是要求的石子,石子所在的距离就是要求的距离 
     59     }
     60     return 0;
     61 }
     62 */
     63 
     64 //代码二:
     65 #include <iostream>
     66 #include<cstdio>
     67 #include <queue>
     68 using namespace std;
     69 
     70 struct node         //直接在结构体里边定义小于运算符
     71 {
     72     int position,length;
     73     bool operator < (const node &p) const     //定义小于操作符,也可以把&去掉,不用引用类型,时间一样
     74     {
     75         if(position != p.position)
     76             return position > p.position;
     77         else
     78             return length > p.length;
     79     }
     80 };
     81 priority_queue<node> s;    //优先队列--从小到大排序
     82 
     83 int main()
     84 {
     85     int n,m,k;
     86     node current;
     87     scanf("%d",&n);
     88     while(n--)
     89     {
     90         cin>>m;
     91         while(m--)
     92         {
     93             scanf("%d%d",&current.position,&current.length);
     94             s.push(current);
     95         }
     96         m=0;
     97         while(!s.empty())
     98         {
     99             m++;
    100             current=s.top();
    101             if(m&1) 
    102             {
    103                 current.position+=current.length;
    104                 s.push(current);
    105             }    
    106             else 
    107                 k=current.position;
    108             s.pop();
    109         }
    110         printf("%d\n",k);
    111     }
    112     return 0;
    113 }
    功不成,身已退
  • 相关阅读:
    Selenium(三)webdriver的API与定位元素
    代码验证和动手动脑
    课程作业01实验报告1,2,3
    动手动脑
    课程作业03实验报告
    课程作业02实验报告
    猜数字实验报告
    java各种问题总结和一些讨论
    java从命令行接受多个数字并求和
    java登陆界面实验报告
  • 原文地址:https://www.cnblogs.com/dongsheng/p/2661864.html
Copyright © 2011-2022 走看看