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

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

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

贴优先队列的代码:

#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. ios---setContentOffset

    UIView * farmeView=[[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width,  self. ...

  2. js单线程和js异步操作的几种方法

    一.为什么JavaScript是单线程? JavaScript语言的一大特点就是单线程,也就是说,同一个时间只能做一件事. JavaScript的单线程,与它的用途有关.作为浏览器脚本语言,JavaS ...

  3. iview modal 弹框 模板

    iview modal 弹框 模板 <!-- * @description 上传图片 * @fileName camera.vue * @author 彭成刚 * @date // :: * @ ...

  4. 暑假集训 || bitset

    bitset是一个存储0和1的数组 可以快速的把两个bitset的每一位对应做与或啥的 在可以用01串表示某个状态的时候可以应用到它 就是有两个集合,求它们的交集 bitset <> a, ...

  5. C++变量和基本类型

    1. 如何选择类型的准则 当明确知晓数值不可能为负的时候,应该选择无符号类型. 使用int执行整数运算的时候,在实际应用中,short常常显得太小而long一般和int有一样的尺寸,如果数值超过了in ...

  6. CSS3---渲染属性

    1.计数器 CSS3计数器( CSS Counters )可以允许我们使用css对页面中的任意元素进行计数,实现类似于有序列表的功能.与有序列表相比,它的突出特性在于可以对任意元素计数,同时实现个性化 ...

  7. 杭电 1241 Oil Deposits (很好的dfs)

    Description The GeoSurvComp geologic survey company is responsible for detecting underground oil dep ...

  8. No unique bean of type..... Unsatisfied dependency of type

    比如在XXXServiceImpl里面写了aa()方法给别的地方调用 但是自己又调用了自己 在开头写了 @Autowired Private XXX xxx; xxx.aa(); 这样重复调用自己的b ...

  9. 【ORACLE】查看死锁进程并结束死锁的脚本

    --共享锁:Share:排他锁:Exclusive:行共享锁:Row-S:行排他锁:Row-X select V$SESSION.sid,v$session.SERIAL#,v$process.spi ...

  10. #region 私有字段

    #region 私有字段                   private string _读者类别;          private string _读者类别名称;          priva ...