Potions (Easy Version) CodeForces - 1526C1(优先队列)

先讲一下优先队列:

top 访问队头元素
empty 判断队列是否为空
size 返回队列内元素个数
push 插入元素到队尾 (并排序)
pop 弹出队头元素
swap 交换内容

升序和降序优先队列:

升序队列
priority_queue <int,vector,greater > q;
降序队列
priority_queue <int,vector,less >q;

题目链接:https://vjudge.z180.cn/problem/CodeForces-1526C1

This is the easy version of the problem. The only difference is that in this version n≤2000. You can make hacks only if both versions of the problem are solved.

There are n potions in a line, with potion 1 on the far left and potion n on the far right. Each potion will increase your health by ai when drunk. ai can be negative, meaning that potion will decrease will health.

You start with 0 health and you will walk from left to right, from first potion to the last one. At each potion, you may choose to drink it or ignore it. You must ensure that your health is always non-negative.

What is the largest number of potions you can drink?

Input

The first line contains a single integer n (1≤n≤2000) — the number of potions.

The next line contains n integers a1, a2, … ,an (−109≤ai≤109) which represent the change in health after drinking that potion.

Output

Output a single integer, the maximum number of potions you can drink without your health becoming negative.

Example Input

6
4 -4 1 -3 1 -3

Output

5

Note

For the sample, you can drink 5 potions by taking potions 1, 3, 4, 5 and 6. It is not possible to drink all 6 potions because your health will go negative at some point

题意:

给你n瓶🥤(饮料),每瓶🥤都有值,你的初始生命值为0,从左到右喝🥤,保证最后生命值>=0,求喝饮料的最大数。

题解1:

正值肯定要加,当为负值时,判断一下和是否为0,如果和<0,就判断当前这个值如果大于优先队列中最小的那个值,就替换。

注意:

下面这个代码不能交C++,只能交G++

#include<stdio.h>
#include<string.h>
#include<queue>
#include<stack>
#include<map>
#include<iostream>
#include<stdlib.h>
#include<math.h>
#include<algorithm>
using namespace std;
typedef long long ll;
priority_queue <ll,vector<ll>,greater<ll> > q;//按照升序排列
int main()
{
	ll n,i,j,k,x;
	scanf("%lld",&n);
	ll num=0,sum=0;
	for(i=1; i<=n; i++)
	{
		scanf("%lld",&x);
		if(x>=0)
		{
			sum+=x;
			num++;
			q.push(x);
		}
		else
		{
			if((sum+x)>=0)
			{
				sum+=x;
				num++;
				q.push(x);
			}
			else
			{
				if(!q.empty()&&x>q.top())
				{
					sum-=q.top();
					q.pop();
					q.push(x);
					sum+=x;
				}
			}
		}
	}
	printf("%lld\n",num);
	return 0;
}

题解2:

将负值存到优先队列里,如果和<0,就和加上优先队列里最大的那个值,其实就是去除最大的值。

注意:

下面这个代码可以交C++,就是运行有些慢,耐心等等就过了

#include<stdio.h>
#include<string.h>
#include<math.h>
#include<stdlib.h>
#include<stack>
#include<map>
#include<queue>
#include<iostream>
#include<algorithm>
using namespace std;
typedef long long ll;
priority_queue<ll> q;
int main()
{
	ll n,x,i,j,k;
	ll sum=0,num=0;
	scanf("%lld",&n);
	for(i=1; i<=n; i++)
	{
		scanf("%lld",&x);
		sum+=x;
		if(x>=0)
			num++;
		else
			q.push(-x);
		while(!q.empty()&&sum<0)
		{
			sum+=q.top();
			q.pop();
		}
	}
	printf("%lld\n",num+q.size());
	return 0;
}
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值