感谢《啊哈!算法》的讲解,水鸟弄懂了什么是优先队列。

题意是:在路上有很多石子,给出他们的初始位置和小明能够将他们扔出的距离,当小明遇到奇数个石子的时候就会把它扔出,遇到偶数个就会忽略他,一直走到路上没有石子为止,求解最后一个石子的位置。

一开始用排序做的,果断超时,看了题解才知道这是优先队列。

贴优先队列的代码:

#include<stdio.h>
int n;
struct st
{
int pos,dis;
};
st stone[];
void swap(int a,int b)
{
st t;
t=stone[a];
stone[a]=stone[b];
stone[b]=t;
}
void siftdown(int i)
{
int t,flag=;
while((i<<)<=n&&flag==)
{
if(stone[i].pos>stone[i<<].pos||(stone[i].pos==stone[i<<].pos&&stone[i].dis>stone[i<<].dis))
{
t=i<<;
}
else
t=i;
if((i<<|)<=n)
{
if(stone[t].pos>stone[i<<|].pos||(stone[t].pos==stone[i<<|].pos&&stone[t].dis>stone[i<<|].dis))
{
t=(i<<|);
}
}
if(t!=i)
{
swap(t,i);
i=t;
}
else
flag=;
}
}
int main()
{
int t,tt;
scanf("%d",&t);
for(tt=;tt<t;tt++)
{
scanf("%d",&n);
for(int i=;i<=n;i++)
{
scanf("%d%d",&stone[i].pos,&stone[i].dis);
}
for(int i=n/;i>=;i--)
{
siftdown(i);
}
int step=;
int ans;
while(n>)
{
step++;
if(step&)
{
stone[].pos+=stone[].dis;
ans=stone[].pos;
siftdown();
}
else
{
stone[]=stone[n];
n--;
siftdown();
}
}
printf("%d\n",ans);
}
}

然后屌丝发现c++里边有优先队列的容器:priority_queue

贴容器的使用方法(其实并不怎么懂,只是会用了):

#include<stdio.h>
#include<queue>
using namespace std;
struct st
{
int pos,dis;
};
struct cmp
{
bool operator()(const st &a,const st &b)
{
if(a.pos!=b.pos)
return a.pos>b.pos;
return a.dis>b.dis;
}
};
st stone[];
int main()
{
int t,tt,n;
priority_queue<st,vector<st>,cmp>q;
scanf("%d",&t);
for(tt=;tt<t;tt++)
{
scanf("%d",&n);
for(int i=;i<=n;i++)
{
scanf("%d%d",&stone[i].pos,&stone[i].dis);
q.push(stone[i]);
}
int step=,ans;
st ttt;
while(!q.empty())
{
step++;
ttt=q.top();
q.pop();
if(step&)
{
ttt.pos+=ttt.dis;
q.push(ttt);
}
if(q.empty()){printf("%d\n",ttt.pos);}
}
}
//printf("%d\n",ans);
}

HDU 1896 【留个使用priority_queue容器的样例】的更多相关文章

  1. 详解C++ STL priority_queue 容器

    详解C++ STL priority_queue 容器 本篇随笔简单介绍一下\(C++STL\)中\(priority_queue\)容器的使用方法和常见的使用技巧. priority_queue容器 ...

  2. hdu 1003 MAX SUM 简单的dp,测试样例之间输出空行

    测试样例之间输出空行,if(t>0) cout<<endl; 这样出最后一组测试样例之外,其它么每组测试样例之后都会输出一个空行. dp[i]表示以a[i]结尾的最大值,则:dp[i ...

  3. Stones HDU 1896

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1896 题目大意: 有n个石头,每个石头有:p  它所在的位置 ,d  它能扔多远 从0 开始,遇到第奇 ...

  4. hdu 1896.Stones 解题报告

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1896 题目意思:给出 n 块石头的初始位置和能到达的距离.对于第奇数次遇到的石头才抛掷,偶数次的就忽略 ...

  5. hdu 1509 & hdu 1873 & hdu 1896 (基础优先队列)

    http://acm.hdu.edu.cn/showproblem.php?pid=1509 裸的优先队列的应用,输入PUT的时候输入名字,值和优先值进队列,输入GRT的时候输出优先值小的名字和对应的 ...

  6. HDU 1896 Stones (优先队列)

    Problem Description Because of the wrong status of the bicycle, Sempr begin to walk east to west eve ...

  7. HDU 1896 Stones (优先队列)

    Stones Time Limit: 5000/3000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)Total Subm ...

  8. HDU 1113 Word Amalgamation (map 容器 + string容器)

    http://acm.hdu.edu.cn/showproblem.php?pid=1113 Problem Description In millions of newspapers across ...

  9. HDU 1896 Stones --优先队列+搜索

    一直向前搜..做法有点像模拟.但是要用到出队入队,有点像搜索. 代码: #include <iostream> #include <cstdio> #include <c ...

随机推荐

  1. SQL的top 100 percent用法

    sql="select top 30 * from data where title='"&title1&"' order by id desc" ...

  2. 一个SAP开发人员的双截棍之路

    由于种种原因,Jerry最近加入了SAP成都研究院的一个演讲俱乐部,这个俱乐部主要是提高大家的英语演讲能力. 说来Jerry也是大一下期和大二上期一次性高分通过四六级考试的,但是当毕业进入SAP成都研 ...

  3. Devops 技术图谱

  4. mysql踩坑记录之limit和sum函数混合使用问题

    问题复盘本次复盘会用一个很简单的订单表作为示例. 数据准备订单表建表语句如下(这里偷懒了,使用了自增ID,实际开发中不建议使用自增ID作为订单ID) CREATE TABLE `order` ( `i ...

  5. unnamed not found for the web module

    intellij idea tomcat 启动报错not found for the web module 使用intellij idea 创建tomcat项目的时候会出现该错误: 启动tomcat的 ...

  6. ibatis经验

    1.insert,update,delete 返回值(1).insert 返回的为插入的主键值,但必须在配置文件中加入<selectKey/>   如果主键值为String<sele ...

  7. sqlserver还原3101

    1.出现错误"3101" 2.解决办法:删除数据库之后还原(有风险)或者获得数据库的独占访问权(用sql语句) 参考:https://www.2cto.com/database/2 ...

  8. 洛谷P1035 级数求和

    #include <iostream> using namespace std; int main(){ long k,i; cin >> k; double s=0.0; ; ...

  9. [LUOGU] P3611 [USACO17JAN]Cow Dance Show奶牛舞蹈

    https://www.luogu.org/problemnew/show/P3611 二分答案+优先队列 二分O(logn) 判一次正确性O(nlogn) 总体O(nlognlogn) 为了让pri ...

  10. (1) LVS基本概念和三种模式

    网站架构中,负载均衡技术是实现网站架构伸缩性的主要手段之一. 所谓"伸缩性",是指可以不断向集群中添加新的服务器来提升性能.缓解不断增加的并发用户访问压力.通俗地讲,就是一头牛拉不 ...