您的位置:首页 > 理论基础 > 数据结构算法

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;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: