题目是:HDU1896

题目简述:输入一堆石头,每个石头有自己所在的位置p,以及自己可以抛多远的距离d。你每遇到第奇数个石头,就把石头丢出去,第偶数个石头就不管。计算出最后一个石头它所处的位置。

解法:该题我采取的是先用优先队列对石头进行排序,然后再对每个石头进行处理,奇数石头就计算出石头的新位置在插进队列去,偶数石头就删除,最后所剩的石头的位置就是所求的位置。

Σ( ̄。 ̄ノ)ノ很久没敲代码。。。。优先队列都快忘记怎么写了。。。。复习一下:优先队列

头文件:

#include <queue>

基本操作:

empty()       如果队列为空返回真

pop()           删除对顶元素

size()           返回优先队列中拥有的元素个数

top()            返回优先队列对顶元素

在默认的优先队列中,优先级高的先出队。在默认的int型中先出队的为较大的数。

声明方式:

1、普通方法:

priority_queue<int>q;
//通过操作,按照元素从大到小的顺序出队

2、自定义优先级:

struct cmp
{
operator bool ()(int x, int y)
{
return x > y; // x小的优先级高
//也可以写成其他方式,如: return p[x] > p[y];表示p[i]小的优先级高
}
};
priority_queue<int, vector<int>, cmp>q;//定义方法
//其中,第二个参数为容器类型。第三个参数为比较函数。
 

3、结构体声明方式:

struct node
{
int x, y;
friend bool operator < (node a, node b)
{
return a.x > b.x; //结构体中,x小的优先级高
}
};
priority_queue<node>q;//定义方法
//在该结构中,y为值, x为优先级。
//通过自定义operator<操作符来比较元素中的优先级。
//在重载”<”时,最好不要重载”>”,可能会发生编译错误
 
代码如下:
 #include <iostream>
#include <cstdio>
#include <queue>
using namespace std; struct node
{
friend bool operator < (node n1,node n2)
{
if(n1.p!=n2.p) return n1.p>n2.p;
else return n1.d>n2.d;
}
int p,d;
}; priority_queue<node> q; int main()
{
int n,m,number;
node x;
scanf("%d",&n);
while(n--)
{
scanf("%d",&m);
int i;
for(i=;i<m;i++)
{
scanf("%d%d",&x.p,&x.d);
q.push(x);
}
i=;
while(!q.empty())
{
if(i%)
{
x=q.top();
q.pop();
x.p+=x.d;
q.push(x);
}
else
{
number=q.top().p;
q.pop();
}
i++;
}
cout<<number<<endl;
}
return ;
}

Stones的更多相关文章

  1. HDU 5973 Game of Taking Stones 威佐夫博弈+大数

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5973 Game of Taking Stones Time Limit: 2000/1000 MS ...

  2. HDU 4573 Throw the Stones(动态三维凸包)(2013 ACM-ICPC长沙赛区全国邀请赛)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4573 Problem Description Remember our childhood? A fe ...

  3. codechef Jewels and Stones 题解

    Soma is a fashionable girl. She absolutely loves shiny stones that she can put on as jewellery acces ...

  4. HDU-1896 Stones

    http://acm.hdu.edu.cn/showproblem.php?pid=1896 题意:一个人从0开始走起,遇到偶数个石头就踢.要是同一位置有多个石头,则先扔最重的石头(也就是扔的最近的那 ...

  5. hdoj 1896 Stones【优先队列】

    Stones Time Limit: 5000/3000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)Total Subm ...

  6. Stones(优先队列)

    Stones Time Limit: 5000/3000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)Total Subm ...

  7. 动态规划,而已! CodeForces 433B - Kuriyama Mirai&#39;s Stones

    Kuriyama Mirai has killed many monsters and got many (namely n) stones. She numbers the stones from  ...

  8. HDU 1896 Stones (优先队列)

    Problem Description Because of the wrong status of the bicycle, Sempr begin to walk east to west eve ...

  9. LeetCode --> 771. Jewels and Stones

    Jewels and Stones You're given strings J representing the types of stones that are jewels, and S rep ...

  10. [Swift]LeetCode1033. 移动石子直到连续 | Moving Stones Until Consecutive

    Three stones are on a number line at positions a, b, and c. Each turn, let's say the stones are curr ...

随机推荐

  1. C++动态链接库测试实例

    前话 上一章节我导出了一个动态链接库 要使用该链接库,我们还需要该链接库对外公开的函数,即头文件 下面开始实例 测试实例 第一步--将动态链接库的dll.lib.和头文件导入项目中 文件目录如下: 项 ...

  2. ORACLE和SQL SERVER的数据同步常用方法

    ORACLE和SQL SERVER的数据同步常用方法 1. 自己编程,或者第三方工具2. 在sqlserver中,使用linkedserver,访问oracle,然后编写job进行数据同步3. 在or ...

  3. 详解js中的寄生组合式继承

    寄生组合式继承是js中最理想的继承方式, 最大限度的节省了内存空间. js中的寄生组合式继承要求是: 1.子对象有父对象属性的副本, 且这些不应该保存在子对象的prototype上.       2. ...

  4. ↗☻【HTML5秘籍 #BOOK#】第2章 构造网页的新方式

    div division 分区article 表示一个完整的.自成一体的内容块,比如博文文章或新闻报道hgroup 标注副标题 从结构上讲,它只关注顶级标题(也就是这里的h1).其他标题也会显示在浏览 ...

  5. SharePoint Designer定制MOSS/WSS表单页面

    转:http://blog.csdn.net/yl_99/article/details/7087897 方法一.使用SharePoint Designer配合enderingTemplate文件来定 ...

  6. C# 中的值类型和引用类型

    原文 C# 中的值类型和引用类型 值类型(value type):int,long,float,double,decimal,char,bool 和 struct 统称为值类型.值类型变量声明后,不管 ...

  7. cdn是什么和作用有些

    内容分发网络其基本思路是尽可能避开互联网上有可能影响数据传输速度和稳定性的瓶颈和环节,使内容传输的更快.更稳定.通过在网络各处放置节点服务器所构 成的在现有的互联网基础之上的一层智能虚拟网络,CDN系 ...

  8. Hessian介绍

    Hessian是什么   Hessian类似Web Service,是一种高效简洁的远程调用框架. Hessian的主页:http://hessian.caucho.com/   有关网上的对Hess ...

  9. mycat分布式mysql中间件(数据库切分概述)[转]

    mysql数据库切分 前言 通 过MySQLReplication功能所实现的扩展总是会受到数据库大小的限制,一旦数据库过于庞大,尤其是当写入过于频繁,很难由一台主机支撑的时 候,我们还是会面临到扩展 ...

  10. 任务栏窗口和状态图标的闪动 z

    Demo程序: 实现任务栏窗体和图标的闪动: 整个程序是基于Windows Forms的,对于任务栏右下角状态图标的闪动,创建了一个类型:NotifyIconAnimator,基本上是包装了Windo ...