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[],di[];
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=;i<n;i++)scanf("%d%d",&pi[i],&di[i]);
for(int i=;i<n;i++){
if(i==){ //将第一个石头即其移动后的位置存入队列
p.push(pi[i]);
p.push(pi[i]+di[i]);
t=di[i];
}
else{
if(p.size()%==){ //队列中有偶数个石头位置
if(p.top()>pi[i]){
if(i!=n-){
if(p.top()<pi[i+]){
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-){
if(p.top()<pi[i+])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-){
if(p.top()<pi[i+])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-){
if(p.top()<pi[i+])p.push(p.top()+t);
else {
p.push(pi[i]);
continue;
}
}
else p.push(p.top()+t);
}
}
p.push(pi[i]);
}
}
}
if(p.size()%==)p.push(p.top()+t);
cout<<p.top()<<endl;
}
//system("pause");
return ;
}

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

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

#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 =;i<n ; i++ )
{
scanf("%d%d",&tmp.pi,&tmp.di);
q.push(tmp);
}
int sum =; //判断碰到的是第几个石头的标记
while(!q.empty()) //当队列为空就跳出循环,也就是说再//向前就没有石头可以遇到
{
tmp = q.top();
//cout<<"-"<<tmp.pi<<" *"<<endl;
q.pop(); //去队顶元素,也就是在后面的所有//石头中第一个碰到的石头
if(sum%)
{ //如果是奇数号石头,则处理,否则不做处理
tmp.pi+=tmp.di; //则向前扔y单位长度
q.push(tmp); //扔出去的石头入队
}
sum++; //石头计数+1
}
printf("%d\n",tmp.pi);
}
//system("pause");
return ;
}

V - stl 的 优先队列 Ⅱ的更多相关文章

  1. STL之优先队列

    STL 中优先队列的使用方法(priority_queu) 基本操作: empty() 如果队列为空返回真 pop() 删除对顶元素 push() 加入一个元素 size() 返回优先队列中拥有的元素 ...

  2. STL priority_queue 优先队列 小记

    今天做题发现一个很有趣的地方,竟然还是头一次发现,唉,还是太菜了. 做图论用STL里的priority_queue去优化prim,由于特殊需求,我需要记录生成树中是用的哪些边. 于是,我定义的优先队列 ...

  3. 【STL】优先队列priority_queue详解+OpenJudge-4980拯救行动

    一.关于优先队列 队列(queue)这种东西广大OIer应该都不陌生,或者说,队列都不会你还学个卵啊(╯‵□′)╯︵┻━┻咳咳,通俗讲,队列是一种只允许从前端(队头)删除元素.从后端(队尾)插入元素的 ...

  4. STL中优先队列的使用

    普通的队列是一种先进先出的数据结构,元素在队列尾追加,而从队列头删除.在优先队列中,元素被赋予优先级.当访问元素时,具有最高优先级的元素最先删除.优先队列具有最高级先出的行为特征.我们来说一下C++的 ...

  5. STL之优先队列(1)

    优先队列用法 在优先队列中,优先级高的元素先出队列. 标准库默认使用元素类型的<操作符来确定它们之间的优先级关系. 优先队列的第一种用法: 也是最常用的用法 priority_queue< ...

  6. STL之优先队列(priority_queue)

    转自网上大牛博客,原文地址:http://www.cnblogs.com/summerRQ/articles/2470130.html 先回顾队列的定义:队列(queue)维护了一组对象,进入队列的对 ...

  7. W - stl 的 优先队列 Ⅲ

    Description In a speech contest, when a contestant finishes his speech, the judges will then grade h ...

  8. hdu 4393 Throw nails(STL之优先队列)

    Problem Description The annual school bicycle contest started. ZL is a student in this school. He is ...

  9. hdu1716排列2(stl:next_permutation+优先队列)

    排列2 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submis ...

随机推荐

  1. log4j参数说明

    log4j.properties 使用 一.参数意义说明 输出级别的种类 ERROR.WARN.INFO.DEBUG ERROR 为严重错误 主要是程序的错误 WARN 为一般警告,比如session ...

  2. POJ 2195 Going Home / HDU 1533(最小费用最大流模板)

    题目大意: 有一个最大是100 * 100 的网格图,上面有 s 个 房子和人,人每移动一个格子花费1的代价,求最小代价让所有的人都进入一个房子.每个房子只能进入一个人. 算法讨论: 注意是KM 和 ...

  3. PHP怎么实现网站中,同一个用户不能同时在线?

    先上图,看个大概: 一般的原则就是,后一个用户登录时会把前一个用户踢下线. 在用户首次登录时,我们会把用户的sessionid保存到数据库,这个是用户的唯一标识.方便后边操作. 用户只有在登录时才会和 ...

  4. [原]C语言实现的快速排序,采用分治策略,递归实现

    #include<stdio.h> #define LEN 8 int a[LEN] = { 5, 2, 4, 7, 1, 3, 2, 6 }; int Partition(int a[] ...

  5. 几个STL算法:includes,set_difference、set_intersection、set_symmetric_difference、set_union, pre_permutation, next_permutation

    includes: 测试有序序列中是否包含另一个序列的全部元素. template<class inputIterator1, class inputIterator2> bool inc ...

  6. request.getParamer()

    eturns the value of a request parameter as a String, or null if the parameter does not exist. Reques ...

  7. MyEclipse6.5安装SVN插件的三种方法

    MyEclipse6.5安装SVN插件的三种方法 方法一.如果可以上网可在线安装 1. 打开Myeclipse,在菜单栏中选择Help→Software Updates→Find and Instal ...

  8. ASP.NET用户自定义控件配置

    一直以来开发中碰到要写自定义控件的时候总是习惯性的找度娘,而没有自己记住,结果今天就悲剧了,找了半天才找到,想想还是自己积累起来吧! 第一种配置方式: 配置写在webconfig文件中,位置如下: w ...

  9. 利用SQL Profiler处理开销较大的查询

    当SQL Server的性能变差时,最可能发生的是以下两件事: 首先,某些查询产生了系统资源上很大的压力.这些查询影响整个系统的性能,因为服务器无法足够快速地服务其他SQL查询. 另外,开销较大的查询 ...

  10. javascript之全局函数

    一.eval() //执行一段字符串中的javascript代码. 语法:eval(code); //可以将javascript写在字符串里面执行. var str = "document. ...