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

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

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

贴优先队列的代码:

#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. 第17周翻译:SQL Server中的事务日志管理的阶梯:第5级:在完全恢复模式下管理日志

    来源:http://www.sqlservercentral.com/articles/Stairway+Series/73785/ 作者:Tony Davis, 2012/01/27 翻译:刘琼滨. ...

  2. Android系统固件定制方式

    target_product.mkAndroid系统在构建关于某种产品的固件时,一般会根据特定于该产品的具体target_product.mk来配置生成整个Android系统./target_prod ...

  3. 关于html页面元素语义化的一点思考

    这几天在看招聘公告前端工程师的要求基本都附带了html语义化的要求,所以稍微关注了下这方面的知识.对于其中的一点就是要求页面元素在去除css样式之后还能有良好的布局引发了我一点思考.作为前端刚入门的我 ...

  4. easyui前端框架01

    一. 三大前端框架的特点 1.easyui=jquery+html4 优点:快速开发.功能齐全 .免费 缺点:不好看.不支持相应式开发 2.bootstrap=jquery+html5 优点: 功能强 ...

  5. python 人脸识别试水(一)

    1.安装python,在这里我的版本是python 3.6 2.安装pycharm,我的版本是pycharm 2017 3.安装pip  pip 版本10 4.安装 numpy    :pip ins ...

  6. Java数据结构和算法(二)--队列

    上一篇文章写了栈的相关知识,而本文会讲一下队列 队列是一种特殊的线性表,在尾部插入(入队Enqueue),从头部删除(出队Dequeue),和栈的特性相反,存取数据特点是:FIFO Java中queu ...

  7. 【软件构造】第八章第三节 代码调优的设计模式和I/O

    第八章第三节 代码调优的设计模式和I/O 本节学习如何通过对代码的修改,消除性能瓶颈,提高系统性能?——代码调优.面向性 能的设计模式 Outline Java调优 代码调优的概念 单例模式(Sing ...

  8. 【传智播客】Libevent学习笔记(三):事件循环

    目录 00. 目录 01. event_base_loop函数 02. event_base_dispatch函数 03. event_base_loopexit函数 04. event_base_l ...

  9. get、post、put、delete

    form表单有两种提交方式,get和post,get没有请求体, 但是有查询字符串参数拼接在url后面,post有请求体,表单内容对用户不可见. 1.form表单以get请求提交, 2.from表单以 ...

  10. 提高CPU使用率

    某些特殊时候,需要提升下cpu的利用率,此时……………………需要一个极其简单的脚本来完成! #!/bin/bash while (true);do { for i in $(seq 100000 10 ...