日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 默认站点!

默认站点

當前位置: 首頁 >

HDOJ 1896 Stones 解题报告

發布時間:2023/12/1 40 豆豆
默认站点 收集整理的這篇文章主要介紹了 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


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

題目大意是路上有很多石頭,當你遇到奇數序列的石頭就把他向前仍,偶數的不動他,如果兩個石頭一起,先考慮可以仍的比較近的石頭仍也就是比較大的石頭,這樣一直下去,直到前面所有的石頭都不可以仍了為止,求最遠的石頭距離起點多少題目這題用優先隊列非常方便.

分析;可以定義一個結構體,分別存儲石頭現在的位置和能能出去的距離到優先隊列中,然后每次取最小的,如果取得是偶數個就不動,取得是奇數個就要更新該石頭的位置并重新存到優先隊列中,直到隊列空,輸出最后一個石頭的位置

數據分析:程序的時間復雜度是Onlogn,數據量最大為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 解题报告的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得默认站点網站內容還不錯,歡迎將默认站点推薦給好友。