V - stl 的 优先队列 Ⅱ
Description
Input
Output
Sample Input
Sample Output
#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 的 优先队列 Ⅱ的更多相关文章
- STL之优先队列
STL 中优先队列的使用方法(priority_queu) 基本操作: empty() 如果队列为空返回真 pop() 删除对顶元素 push() 加入一个元素 size() 返回优先队列中拥有的元素 ...
- STL priority_queue 优先队列 小记
今天做题发现一个很有趣的地方,竟然还是头一次发现,唉,还是太菜了. 做图论用STL里的priority_queue去优化prim,由于特殊需求,我需要记录生成树中是用的哪些边. 于是,我定义的优先队列 ...
- 【STL】优先队列priority_queue详解+OpenJudge-4980拯救行动
一.关于优先队列 队列(queue)这种东西广大OIer应该都不陌生,或者说,队列都不会你还学个卵啊(╯‵□′)╯︵┻━┻咳咳,通俗讲,队列是一种只允许从前端(队头)删除元素.从后端(队尾)插入元素的 ...
- STL中优先队列的使用
普通的队列是一种先进先出的数据结构,元素在队列尾追加,而从队列头删除.在优先队列中,元素被赋予优先级.当访问元素时,具有最高优先级的元素最先删除.优先队列具有最高级先出的行为特征.我们来说一下C++的 ...
- STL之优先队列(1)
优先队列用法 在优先队列中,优先级高的元素先出队列. 标准库默认使用元素类型的<操作符来确定它们之间的优先级关系. 优先队列的第一种用法: 也是最常用的用法 priority_queue< ...
- STL之优先队列(priority_queue)
转自网上大牛博客,原文地址:http://www.cnblogs.com/summerRQ/articles/2470130.html 先回顾队列的定义:队列(queue)维护了一组对象,进入队列的对 ...
- W - stl 的 优先队列 Ⅲ
Description In a speech contest, when a contestant finishes his speech, the judges will then grade h ...
- hdu 4393 Throw nails(STL之优先队列)
Problem Description The annual school bicycle contest started. ZL is a student in this school. He is ...
- hdu1716排列2(stl:next_permutation+优先队列)
排列2 Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submis ...
随机推荐
- QT5.5实现串口通信
QT5.1以上版本自带QtSerialPort集成库,只要在头文件中集成 #include <QtSerialPort/QSerialPort> #include <QtSerial ...
- (转)Ubuntu中使用dpkg安装deb文件提示依赖关系问题,仍未被配置
转载请注明出处: http://www.cnblogs.com/darkknightzh/p/5638149.html 参考网址: http://zhidao.baidu.com/link?url=b ...
- js渲染的3d玫瑰
参看下面链接: 程序员最美情人节礼物:JS渲染的3D玫瑰
- C语言基本概念
1. 标准C语言 C语言诞生于20世纪70年代,年龄比我们自己还要大,期间产生了很多标准,但是各种编译器对标准的支持不尽相同. ANSI C是使用的最广泛的一个标准,也是第一个正式标准,被称为“标准C ...
- How Many Tables(POJ 1213 求连通分量)
How Many Tables Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)T ...
- HQL(Hibernate Query language)语言
现在有两张表:student(学生表),classroom(教室表). //对象 Student 对应 student 表中有四个字段,分别是:id,name,age,classroom; publi ...
- 杀死MySQL的连接
命令 kill 执行线程号 C# 执行杀死指定的连接 1 强制Kill掉 internal protected void KillConnection(MySqlConnection c) { i ...
- PYCURL ERROR 6 - “Couldn't resolve host 'mirrorlist.centos.org'”
在虚拟机上安装的CentOS,估计是网络配置问题,导致yum update和yum install之类的功能的用不了.出现标题上面的错误. ifdown [network_adapter] ifup ...
- perl 创建文本框
my $mw = MainWindow->new(-title => "Mem monitor"); $frm_name1 = $mw -> Frame()-&g ...
- MongoDb C/java driver
1,在linux下安装客户端连接windows下 的MongoDBServer.