HDOJ 1896 Stones 解题报告
                                                            生活随笔
收集整理的這篇文章主要介紹了
                                HDOJ 1896 Stones 解题报告
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.                        
                                題目分類:優先隊列+STL
作者:ACShiryu
做題時間:2011-7-18
Stones
Time Limit: 5000/3000 MS (Java/Others)????Memory Limit: 65535/32768 K (Java/Others)
Total Submission(s): 217????Accepted Submission(s): 107
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.
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.
題目大意是路上有很多石頭,當你遇到奇數序列的石頭就把他向前仍,偶數的不動他,如果兩個石頭一起,先考慮可以仍的比較近的石頭仍也就是比較大的石頭,這樣一直下去,直到前面所有的石頭都不可以仍了為止,求最遠的石頭距離起點多少題目這題用優先隊列非常方便.
分析;可以定義一個結構體,分別存儲石頭現在的位置和能能出去的距離到優先隊列中,然后每次取“最小的”,如果取得是偶數個就不動,取得是奇數個就要更新該石頭的位置并重新存到優先隊列中,直到隊列空,輸出最后一個石頭的位置
數據分析:程序的時間復雜度是O(nlogn),數據量最大為100,000,不會超時。要特別注意多個石頭的x一樣的情況,要優先考慮y值最小的那塊石頭
參考代碼:
1 #include<iostream>
2 #include<cstdlib>
3 #include<cstdio>
4 #include<cstring>
5 #include<algorithm>
6 #include<cmath>
7 #include<queue>
8 using namespace std;
9 struct Stone{
10 int x; //石頭的初始地
11 int y; //石頭能扔的最遠距離
12 };
13 bool operator<( Stone a, Stone b )
14 { //重載小于,按照結構體中x小的在隊頂,如果x一樣,則按照y的最小的在//隊頂
15 if( a.x== b.x ) return a.y > b.y;
16 return a.x > b.x;
17 }
18 int main()
19 {
20 int t;
21 scanf("%d",&t);//測試數據個數
22 while(t--)
23 {
24 int n;
25 int i ;
26 priority_queue<Stone>que; //定義一個Stone成員的優先//隊列
27 scanf("%d",&n);
28 Stone tmp;
29 for(i =0;i< n ; i++ )
30 {
31 scanf("%d%d",&tmp.x,&tmp.y);
32 que.push(tmp);//入隊
33 }
34 int sum =1;//判斷碰到的是第幾個石頭的標記
35 while(!que.empty())//當隊列為空就跳出循環,也就是說再//向前就沒有石頭可以遇到
36 {
37 tmp = que.top();//去隊頂元素,也就是在后面的所有//石頭中第一個碰到的石頭
38 que.pop();//出對
39 if(sum%2)
40 {//如果是奇數號石頭,則處理,否則不做處理
41 tmp . x+=tmp.y;//則向前扔y單位長度
42 que.push(tmp);//扔出去的石頭入隊
43 }
44 sum++;//石頭計數+1
45 }
46 printf("%d\n",tmp.x);//打印最后一塊石頭的坐標就是所求//的最遠距離
47 }
48 return 0;
49 }
轉載于:https://www.cnblogs.com/ACShiryu/archive/2011/07/18/2109973.html
總結
以上是生活随笔為你收集整理的HDOJ 1896 Stones 解题报告的全部內容,希望文章能夠幫你解決所遇到的問題。
 
                            
                        - 上一篇: 程序开发基础学习四(boost::sig
- 下一篇: 奥迪rs7多少钱啊?
