优先队列的应用
2016-07-07 20:27
225 查看
hdu1896 点击打开链接
#include"iostream"
#include"cstdio"
#include"cstdlib"
#include"cstring"
#include"queue"
using namespace std;
typedef struct node
{
int pos;
int weight;
}point;
struct cmp
{
bool operator()(const point a,const point b)
{
if(a.pos!=b.pos) return a.pos>b.pos; //>后置
else return a.weight>b.weight; //>后置
}
};
int main()
{
int t;
cin>>t;
while(t--)
{
int n;
cin>>n;
priority_queue<node,vector<node>,cmp> q;
point stone;
while(n--)
{
cin>>stone.pos>>stone.weight;
q.push(stone);
}
stone=q.top();
bool is=1;
while(!q.empty())
{
if(is==1)
{
stone=q.top();
stone.pos+=stone.weight;
q.push(stone);
q.pop();
}
else
{
stone=q.top();
q.pop();
}
is=!is;
}
cout<<stone.pos<<endl;
}
return 0;
}
#include"iostream"
#include"cstdio"
#include"cstdlib"
#include"cstring"
#include"queue"
using namespace std;
typedef struct node
{
int pos;
int weight;
}point;
struct cmp
{
bool operator()(const point a,const point b)
{
if(a.pos!=b.pos) return a.pos>b.pos; //>后置
else return a.weight>b.weight; //>后置
}
};
int main()
{
int t;
cin>>t;
while(t--)
{
int n;
cin>>n;
priority_queue<node,vector<node>,cmp> q;
point stone;
while(n--)
{
cin>>stone.pos>>stone.weight;
q.push(stone);
}
stone=q.top();
bool is=1;
while(!q.empty())
{
if(is==1)
{
stone=q.top();
stone.pos+=stone.weight;
q.push(stone);
q.pop();
}
else
{
stone=q.top();
q.pop();
}
is=!is;
}
cout<<stone.pos<<endl;
}
return 0;
}
相关文章推荐
- chattr命令详解
- 51NOD 1267 4个数和为0(二分 + 排序)
- 利用http缓存数据
- 17.4节练习
- java基础学习总结——面向对象2
- 17.3节练习
- 01 LabVIEW的类中各个Scope的范围
- 【水】基于ege的2048
- poj 1664:放苹果
- 修复efi引导的win7
- 卷二 MFC中简单控件使用
- Swift POP+MVVM
- 【Netty4.X】TCP粘包/拆包问题的解决办法(二)
- 修改tableview索引条背景颜色变透明或其他颜色的方法
- 6P的宽度375VS414
- 16.7.7 T1:YL杯超级篮球赛
- 用Python递归实现汉诺塔问题
- 大话设计模式c++实现--原型模式
- Jquery事件委托之Safari
- 试图加载格式不正确的程序。 (异常来自 HRESULT:0x8007000B)