题目是: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. 64位ubuntu安装WPS

    http://jingyan.baidu.com/article/d3b74d64afd96f1f77e609a3.html http://sixipiaoyang.blog.163.com/blog ...

  2. tinyXml在linux下的使用

    [下载] 一.下载 xml 软件包:tinyxml_2_6_2.zipTinyxml(轻量级 c++)下载地址:http://sourceforge.net/projects/tinyxml/?sou ...

  3. CodeForces114E——Double Happiness(素数二次筛选)

    Double Happiness On the math lesson a teacher asked each pupil to come up with his own lucky numbers ...

  4. python set type 集合类型的数据介绍 (set frozenset)

      python支持数学中的集合概念,如:通过in,not in 可以检查某元素是否在,不在集合中. python有两种集合类型,set(可以变的,不能哈希,不能用作字典的key),frozenset ...

  5. 解决ImportError: cannot import name HTTPConnection的方法

    在写python程序的时候,使用from httplib import HTTPConnection,在run的时候提示ImportError: cannot import name HTTPConn ...

  6. IT国家重点实验室

    软件工程国家重点实验室(武汉大学): http://www.sklse.whu.edu.cn/ 南京大学: http://keysoftlab.nju.edu.cn/ 水声技术国家重点实验室 http ...

  7. hdu4635Strongly connected

    http://acm.hdu.edu.cn/showproblem.php?pid=4635 tarjan缩点 统计缩点后每个结点的出度入度 将那个包含原来点数最少的 且出度或者入度为0的大节点看作一 ...

  8. 数论/the second wave

    扩展欧几里得算法. void exgcd(int a,int b,int&x,int&y){ if(!b) { x=1;y=0;return ; } exgcd(b,a%b,x,y); ...

  9. [2015编程之美] 第一场C

    题目3 : 质数相关 时间限制:2000ms 单点时限:1000ms 内存限制:256MB 描述 两个数a和 b (a<b)被称为质数相关,是指a × p = b,这里p是一个质数.一个集合S被 ...

  10. [scu 4423] Necklace

    4423: Necklace Description baihacker bought a necklace for his wife on their wedding anniversary. A ...