题目是: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++对象的自销毁

    记得在学校里的时候,曾经这样写过: void MyClass::KillMe() { delete this; } 老师看到这句话的时候,眼珠子都快瞪出来了.但是运行正确啊,没什么问题. 现在想起来, ...

  2. P147、面试题26:复杂链表的复制

    题目:请实现ComplexListNode* Clone(ComplexListNode* pHead),复制一个复杂链表.在复杂链表中,每个结点除了有一个m_pNext指针指向下一个结点外,还有一个 ...

  3. 【HDOJ】4418 Time travel

    1. 题目描述K沿着$0,1,2,\cdots,n-1,n-2,n-3,\cdots,1,$的循环节不断地访问$[0, n-1]$个时光结点.某时刻,时光机故障,这导致K必须持续访问时间结点.故障发生 ...

  4. 【HDOJ】4355 Party All the Time

    好久没做过三分的题目了. /* 4355 */ #include <iostream> #include <sstream> #include <string> # ...

  5. 【HDOJ】1699 The comment in cpp

    注意测试数据12/*hduacm// abcd结果是1/*hduacm// ABCD /* 1699 */ #include <iostream> #include <sstream ...

  6. 转载:C++ list 类学习笔记

    声明:本文转自http://blog.csdn.net/whz_zb/article/details/6831817 双向循环链表list list是双向循环链表,,每一个元素都知道前面一个元素和后面 ...

  7. The only legal comparisons are between two numbers, two strings, or two dates.

    The only legal comparisons are between two numbers, two strings, or two dates. Left  hand operand is ...

  8. UVa 10214 (莫比乌斯反演 or 欧拉函数) Trees in a Wood.

    题意: 这道题和POJ 3090很相似,求|x|≤a,|y|≤b 中站在原点可见的整点的个数K,所有的整点个数为N(除去原点),求K/N 分析: 坐标轴上有四个可见的点,因为每个象限可见的点数都是一样 ...

  9. 探秘Java虚拟机——内存管理与垃圾回收

    本文主要是基于Sun JDK 1.6 Garbage Collector(作者:毕玄)的整理与总结,原文请读者在网上搜索. 1.Java虚拟机运行时的数据区 2.常用的内存区域调节参数 -Xms:初始 ...

  10. 浅谈JavaBean,Entity Bean,Enterprise Bean等Bean以及POJO的含义

      一.对于java bean,就是一个java模型组件,他为使用java类提供了一种标准的格式,在用户程序和可视化管理工具中可以自动获得这种具有标准格式的类的信息,并能够创建和管理这些类.  jav ...