ACM: 优先队列 数据结构题 toj 290…
2016-05-19 23:19
399 查看
Solving the Problems
描述
Programming is fun, Aaron
is addicted to it. In order to improve his programming skill, he
decides to solve one programming problem per day. As you know,
different problems have different properties, some problems are so
difficult that there are few people can solve it, while some
problems are so easy that almost everyone is able to tackle
it.
Programming skill can be measured by an integer p. And
all problems are described by two integers ai and
bi. ai indicates that if and
only if P >= ai, you can solve
this problem. bi indicates that after you solve
this problem, your programming skill can be increased by
bi.
Given the initial programming skill p of Aaron, and the
information of each problem, Aaron want to know the maximal
programming skill he can reach after m days, can you help
him?
输入
Input consists of multiple
test cases (less than 40 cases)!
For each test case, the first line contains three numbers:
n, m, p (1 <= n
<= 100000, 1 <= m
<= n, 1 <= p
<= 10000), n is the number of problems
available for Aaron, m, p as mentioned above.
The following n lines each contain two numbers:
ai and bi (1 <=
ai <= 10000, 1 <=
bi <= 10000) describe the
information of the i-th problem as memtioned above.
There's a blank line between consecutive cases.
输出
For each
case, output the maximal programming skill Aaron can reach after
m days in a line.
样例输入
2 2 1
1 2
7 3
3 1 2
1 2
2 3
3 4
样例输出
3
5
题意: 现在有n个问题,
m天最多每天一题. 每道题目有相应的难度ai 和 提高bi.
只有当能力p >= ai的情况才可以解决题目. 解决之后p = p+bi;
解题思路:
1.
一开始选择排序 + 二分法. 最后将剩下的题目从bi从大到小加上. (不知道为什么WA.) 思考中
2. 还是采取优先队列, 先将瞒住p >= ai的题目进入队列.
当遇到当前的下一个p < ai+1就 p+=bi
3. 最后把m-k个全部出队列 p+=bi; (k是2中当遇到当前的下一个p <
ai+1的情况).
代码:
#include
<cstdio>
#include <iostream>
#include <algorithm>
#include <queue>
using namespace std;
#define MAX 100010
struct
nodeB
{
friend bool operator < (nodeB
n1,nodeB n2)
{
return n1.b <
n2.b; //从b大的开始取元素
}
int a,b;
}pro[MAX];
int cmp(nodeB
n1,nodeB n2)
{
if(n1.a != n2.a)
return n1.a <
n2.a;
else
return n1.b <
n2.b;
}
int n, m,
p;
nodeB now, next;
int
main()
{
int i;
// freopen("input.txt","r",stdin);
while(scanf("%d %d
%d",&n,&m,&p) !=
EOF)
{
priority_queue<nodeB>
qu;
for(i = 0; i <
n; ++i)
{
scanf("%d
%d",&pro[i].a,&pro[i].b);
}
sort(pro,pro+n,cmp);
for(i = 0; i <
n; ++i)
{
if(pro[i].a
<= p)
{
now.a
= pro[i].a;
now.b
= pro[i].b;
qu.push(now);
}
if(pro[i].a
<= p && pro[i+1].a
> p && (i+1) !=
n)
{
m--;
now
= qu.top();
qu.pop();
p
+= now.b;
if(m
== 0)
break;
}
}
while(m)
{
if(
!qu.empty() )
{
now
= qu.top();
qu.pop();
p
+= now.b;
m--;
}
else
break;
}
printf("%d\n",p);
}
return 0;
}
相关文章推荐
- 数据结构学习 线性表 链表
- [Java]常见数据结构的实现(持续更新)
- SDUT3334数据结构实验之栈七:出栈序列判定
- PAT 团体程序设计天梯赛-练习集 L2-006. 树的遍历【数据结构】
- 栈和队列测试题
- [数据结构]Binary_tree | Binary_search_tree | avl_tree
- HYSBZ 1012
- 【数据结构】串 YTU 2297 KMP算法(3)next
- 【数据结构】串 YTU 2296 KMP算法(2)next
- 【数据结构】串 YTU 2295 KMP算法(1)next
- 数据结构与算法分析(3)算法分析
- 数据结构之红黑树
- Redis各种数据结构内存占用测试
- MySQL索引背后的数据结构及算法原理
- C/C++,java 数据结构---线性链表存储数据:获取数据慢,增删快
- java 数据结构和泛型
- 数据结构之排序
- 【数据结构】平衡二叉树—AVL树
- Java 容器类内部使用的数据结构
- 数据结构提纲