HDU 1896 【留个使用priority_queue容器的样例】
感谢《啊哈!算法》的讲解,水鸟弄懂了什么是优先队列。
题意是:在路上有很多石子,给出他们的初始位置和小明能够将他们扔出的距离,当小明遇到奇数个石子的时候就会把它扔出,遇到偶数个就会忽略他,一直走到路上没有石子为止,求解最后一个石子的位置。
一开始用排序做的,果断超时,看了题解才知道这是优先队列。
贴优先队列的代码:
#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容器的样例】的更多相关文章
- 详解C++ STL priority_queue 容器
详解C++ STL priority_queue 容器 本篇随笔简单介绍一下\(C++STL\)中\(priority_queue\)容器的使用方法和常见的使用技巧. priority_queue容器 ...
- hdu 1003 MAX SUM 简单的dp,测试样例之间输出空行
测试样例之间输出空行,if(t>0) cout<<endl; 这样出最后一组测试样例之外,其它么每组测试样例之后都会输出一个空行. dp[i]表示以a[i]结尾的最大值,则:dp[i ...
- Stones HDU 1896
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1896 题目大意: 有n个石头,每个石头有:p 它所在的位置 ,d 它能扔多远 从0 开始,遇到第奇 ...
- hdu 1896.Stones 解题报告
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1896 题目意思:给出 n 块石头的初始位置和能到达的距离.对于第奇数次遇到的石头才抛掷,偶数次的就忽略 ...
- hdu 1509 & hdu 1873 & hdu 1896 (基础优先队列)
http://acm.hdu.edu.cn/showproblem.php?pid=1509 裸的优先队列的应用,输入PUT的时候输入名字,值和优先值进队列,输入GRT的时候输出优先值小的名字和对应的 ...
- HDU 1896 Stones (优先队列)
Problem Description Because of the wrong status of the bicycle, Sempr begin to walk east to west eve ...
- HDU 1896 Stones (优先队列)
Stones Time Limit: 5000/3000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others)Total Subm ...
- HDU 1113 Word Amalgamation (map 容器 + string容器)
http://acm.hdu.edu.cn/showproblem.php?pid=1113 Problem Description In millions of newspapers across ...
- HDU 1896 Stones --优先队列+搜索
一直向前搜..做法有点像模拟.但是要用到出队入队,有点像搜索. 代码: #include <iostream> #include <cstdio> #include <c ...
随机推荐
- ZigBee cc2530芯片学习 error记录(1)
ZigBee cc2530芯片学习 error记录 Error[e46]: Undefined external "LcdInit" referred in main( xxx ...
- HYSBZ 1588 营业额统计 (Splay树)
题意:给出一个公司每一天的营业额,求每天的最小波动值之和.该天的最小波动值= min { 绝对值| 该天以前某一天的营业额-该天的营业额 | }.第一天的最小波动值就是其自己. 思路:Splay伸展树 ...
- Unity3D_最简单的开始界面_结束界面
开始界面1.创建一个新的场景添加button 2.C#脚本LoadingGame.cs using System.Collections;using System.Collections.Generi ...
- xorequation(DFS完全枚举)
题目 有一个含有N个未知数的方程如下: x1^x2^...^xn= V,给定N,V,再给定正整数a1,a2,...an满足1≤ai≤9且∏Ni=1(ai+1) ≤ 32768,请输出所有满足0≤xi ...
- ansible2.7学习笔记系列
写在前面:ansible的资料网上很多,本人也是参考网上资料,做总结,如有错误,麻烦指出,谢谢. 所谓学习笔记,就是不断成长的过程,也许一段时间后有更深入理解了,就会继续更新笔记. 笔记定位:目前写的 ...
- 字符串类型:str, bytes 的区别
Python 3最重要的新特性之一是对字符串和二进制数据流做了明确的区分. 文本总是 Unicode,由 str 类型表示, 二进制数据则由 bytes 类型表示. Python 3不会以任意隐式的方 ...
- springmvc下载那些事
文件的上传下载一般在项目中还是非常实用的,此处专门整理一下文件的下载,至于文件的上传实现将在后续中补上.文件的下载多用于模板文件的下载,这在项目中用的还是挺多的.今天用到了就整理出来了,以供搬运工们借 ...
- 【struts2】学习笔记
常见问题及注意事项: 1.下载struts2时,要看清所下载的版本,不同版本web.xml配置路径不同! 2. 导入jar包时,导入的包要完全准确,缺少或过多的会导致缺失或冲突! 3. Registe ...
- NLog在asp.net core中的应用
Asp.net core中,自带的Log是在当selfhost运行时,在控制台中输出,不便于查阅,如果用一个log架框,把日志持久化,便于查询. NLog是一个免费的日志记录框架,专门为.net平台下 ...
- python_面向对象笔记
继承 什么是继承? 继承是一种新建类的方式,新建的类称为子类或派生类父类又称为基类.超类 子类可以“遗传”父类的属性,从而可以减少代码冗余 如何寻找继承关系?先抽象,再继承,继承描述的就是一种父子关系 ...