博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
HDU 1896:Stones(优先队列)
阅读量:6125 次
发布时间:2019-06-21

本文共 2651 字,大约阅读时间需要 8 分钟。

Stones

Time Limit: 5000/3000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)

Total Submission(s): 4165    Accepted Submission(s): 2699

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

 

题意

有n个石子,每个石子的位置和能丢出去的距离已经给了出来。如果石子是在第奇数个,将该位置的最重的石子扔出去(如果只有一个石子,不需要选;如果有多个石子,将丢出去距离最近的那个扔出去)。问最后距离起点最远的石子距离起点有多远。

思路

定义一个结构体的优先队列,按照题目要求设置优先级,将题目中已知的石子的位置和丢出去的距离放入优先队列中。

然后拿出来优先队列中的最上面的石子,如果该石子在奇数位置,进行移动后重新加入到队列中;如果是偶数,不用管他。用一个数maxx记录石子到达的最远距离,当队列为空的时候,停止操作,输出maxx

AC代码

#include 
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define ll long long#define ms(a) memset(a,0,sizeof(a))#define pi acos(-1.0)#define INF 0x3f3f3f3fconst double E=exp(1);const int maxn=1e6+10;using namespace std;struct wzy{ int place,dis;}s;bool operator < (const wzy &a,const wzy &b){ if(a.place==b.place) return a.dis>b.dis; else return a.place>b.place;}int main(int argc, char const *argv[]){ ios::sync_with_stdio(false); int t; int n; int p,d; cin>>t; while(t--) { priority_queue
que; cin>>n; for(int i=1;i<=n;i++) { cin>>p>>d; s.place=p; s.dis=d; que.push(s); } int maxx=0; for(int i=1;;i++) { if(que.empty()) break; s=que.top(); que.pop(); if(i%2) { s.place+=s.dis; maxx=max(s.place,maxx); que.push(s); } } cout<
<

 

转载于:https://www.cnblogs.com/Friends-A/p/10324438.html

你可能感兴趣的文章
HDOJ 2020 绝对值排序
查看>>
HDOJ/HDU 2560 Buildings(嗯~水题)
查看>>
Maven编译时跳过Test
查看>>
Spring Boot 整合Spring Security 和Swagger2 遇到的问题小结
查看>>
[20170628]12C ORA-54032.txt
查看>>
除以2
查看>>
高可用集群原理解析
查看>>
Nginx配置URL转向tomcat
查看>>
极客Web前端开发资源大荟萃#001
查看>>
让div固定在某个位置
查看>>
Java开发环境Docker镜像
查看>>
从无到有,WebService Apache Axis2初步实践
查看>>
任务调度(一)——jdk自带的Timer
查看>>
UIKit框架(15)PCH头文件
查看>>
整理看到的好的文档
查看>>
Linux磁盘管理和文件系统管理
查看>>
linux运维人员的成功面试总结案例分享
查看>>
Windows DHCP Server基于MAC地址过滤客户端请求实现IP地址的分配
查看>>
命令查询每个文件文件数
查看>>
《跟阿铭学Linux》第8章 文档的压缩与打包:课后习题与答案
查看>>