题目是: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. Servlet课程0425(六) 不经过验证直接跳转---session实现不同页面之间共享数据

    在地址栏直接输入http://localhost:8080/myWebSite/wel 会发现页面也能跳转,只不过用户名和密码都为空,这是不可以的,因为没有经过验证非法登录了 Welcome,hell ...

  2. /storage/sdcard, /sdcard, /mnt/sdcard 三者的区别

    原文地址: /storage/sdcard, /sdcard, /mnt/sdcard 三者的区别 - petercao - 博客园 http://www.cnblogs.com/bluestorm/ ...

  3. js setTimeout深度递归后完成回调

    setTimout原型: iTimerID = window.setTimeout(vCode, iMilliSeconds [, sLanguage])     setTimeout有两种形式 se ...

  4. C# 第三方DLL,可以实现PDF转图片,支持32位系统、64位系统

    itextsharp.dll,是一个开源的在C#中用来生成PDF文档的库文件,不少C#爱好者用它制作出了PDF文档生成器.使用时只需在你的C#项目中添加引入此组件即可,使用方法网上有很多,自己查阅一下 ...

  5. Android开发之ListView-SimpleAdapter的使用

    SimpleAdapter: SimpleAdapter(Context context, List<? extends Map<String, ?>> data, int r ...

  6. Android开发UI之给ImageView添加蒙版

    ImageView控件添加蒙版, 通过属性:android:tint="" 比如 android:tint="#44ff0000"

  7. python中的commands模块,执行出错:'{' 不是内部或外部命令,也不是可运行的程序 或批处理文件。

    最近发现了python的commands模块,查看了下源码,使用的popen封装的,形成三个函数getstatus(), getoutput(), getstatusoutput() 源码如下: de ...

  8. [Mac][$PATH]如何修改$PATH变量

    从 stackoverflow 找到的方法 http://stackoverflow.com/questions/7703041/editing-path-variable-on-mac 首先打开终端 ...

  9. Java [Leetcode 258]Add Digits

    题目描述: Given a non-negative integer num, repeatedly add all its digits until the result has only one ...

  10. 深入解析Java对象的hashCode和hashCode在HashMap的底层数据结构的应用

    转自:http://kakajw.iteye.com/blog/935226 一.java对象的比较 等号(==): 对比对象实例的内存地址(也即对象实例的ID),来判断是否是同一对象实例:又可以说是 ...