Stones
题目是:HDU1896
题目简述:输入一堆石头,每个石头有自己所在的位置p,以及自己可以抛多远的距离d。你每遇到第奇数个石头,就把石头丢出去,第偶数个石头就不管。计算出最后一个石头它所处的位置。
解法:该题我采取的是先用优先队列对石头进行排序,然后再对每个石头进行处理,奇数石头就计算出石头的新位置在插进队列去,偶数石头就删除,最后所剩的石头的位置就是所求的位置。
Σ( ̄。 ̄ノ)ノ很久没敲代码。。。。优先队列都快忘记怎么写了。。。。复习一下:优先队列
头文件:
#include <queue>
基本操作:
empty() 如果队列为空返回真
pop() 删除对顶元素
size() 返回优先队列中拥有的元素个数
top() 返回优先队列对顶元素
在默认的优先队列中,优先级高的先出队。在默认的int型中先出队的为较大的数。
声明方式:
1、普通方法:
//通过操作,按照元素从大到小的顺序出队
2、自定义优先级:
{
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、结构体声明方式:
{
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的更多相关文章
- HDU 5973 Game of Taking Stones 威佐夫博弈+大数
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5973 Game of Taking Stones Time Limit: 2000/1000 MS ...
- HDU 4573 Throw the Stones(动态三维凸包)(2013 ACM-ICPC长沙赛区全国邀请赛)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4573 Problem Description Remember our childhood? A fe ...
- codechef Jewels and Stones 题解
Soma is a fashionable girl. She absolutely loves shiny stones that she can put on as jewellery acces ...
- HDU-1896 Stones
http://acm.hdu.edu.cn/showproblem.php?pid=1896 题意:一个人从0开始走起,遇到偶数个石头就踢.要是同一位置有多个石头,则先扔最重的石头(也就是扔的最近的那 ...
- hdoj 1896 Stones【优先队列】
Stones Time Limit: 5000/3000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others)Total Subm ...
- Stones(优先队列)
Stones Time Limit: 5000/3000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others)Total Subm ...
- 动态规划,而已! CodeForces 433B - Kuriyama Mirai's Stones
Kuriyama Mirai has killed many monsters and got many (namely n) stones. She numbers the stones from ...
- HDU 1896 Stones (优先队列)
Problem Description Because of the wrong status of the bicycle, Sempr begin to walk east to west eve ...
- LeetCode --> 771. Jewels and Stones
Jewels and Stones You're given strings J representing the types of stones that are jewels, and S rep ...
- [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 ...
随机推荐
- POJ2632——Crashing Robots
Crashing Robots DescriptionIn a modernized warehouse, robots are used to fetch the goods. Careful pl ...
- Xcode中的iOS工程模板
1. Application类型 我们大部分的开发工作都是从使用Application类型模板创建iOS程序开始的.该类型共包含7个模板,具体如下所示. Master-Detail Applicati ...
- RTDX target application does not match emulation protocol!
2013-06-20 10:19:22 在CCS2.0 的emulator写dsp/bios 的程序,编译链接无错误,而点击LOAD Program下载xxx.out完成时弹出如下对话框: RTDX ...
- 关于haproxy hdr_reg(host) 的一些解释
I've recently taken over an environment using HAProxy, and I'm attempting to learn the config and wh ...
- String Split 和 Join
很多时候处理字符串数据,比如从文件中读取或者存入 - 我们可能需要加入分隔符(如CSV文件中的逗号),或使用一个分隔符来合并字符串序列. 很多人都知道使用split()的方法,但使用与其对应的Join ...
- [ffmpeg 扩展第三方库编译系列] 关于需要用到cmake 创建 mingw32编译环境问题
我在这里给出我编译的例子 cmake -G"MinGW Makefiles" -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX=& ...
- 函数buf_LRU_get_free_only
/******************************************************************//** Returns a free block from th ...
- mssql查找备注(text,ntext)类型字段为空的方法
在sql语句中,如果查找某个文本字段值为空的,可以用select * from 表 where 字段='' ,但是如果这个字段数据类型是text或者ntext,那上面的sql语句就要出错了. 解决办法 ...
- [NOI2005] 维修数列
1500: [NOI2005]维修数列 Time Limit: 10 Sec Memory Limit: 64 MBSubmit: 8397 Solved: 2530 Description In ...
- ZOJ 2676 Network Wars ★(最小割算法介绍 && 01分数规划)
[题意]给出一个带权无向图,求割集,且割集的平均边权最小. [分析] 先尝试着用更一般的形式重新叙述本问题.设向量w表示边的权值,令向量c=(1, 1, 1, --, 1)表示选边的代价,于是原问题等 ...