CF1526C

Potions (Easy Version)

题面翻译

给你一个长度为 \(n\) 的序列 \(A\),要求你找出最长的一个子序列使得这个子序列任意前缀和都非负。

其中 \(n\le 2\times 10^3\)\(-10^9\le a_i\le 10^9\)

样例 #1

样例输入 #1

6
4 -4 1 -3 1 -3

样例输出 #1

5
反悔贪心
先每个都要 存入小根堆
当tot<0时 一直吐出最小的直到tot>=0为止 同时更新答案
点击查看代码
#include<bits/stdc++.h>//反悔贪心
using namespace std;
#define int long long
int n,a[2005];
priority_queue<int,vector<int>,greater<int> >q;
signed main()
{
	ios::sync_with_stdio(false);
	cin>>n;
	for(int i=1;i<=n;i++)cin>>a[i];
	int ans=0,tot=0;
	int now=1;
	while(1)
	{
		if(now>n)break;
		tot+=a[now];
		q.push(a[now]);
		ans++;
		while(tot<0)//反悔
		{
			tot-=q.top();
			q.pop();
			ans--;
		}
		now++;
	}
	cout<<ans<<"\n";
	return 0;
}
posted @ 2023-01-21 19:21  PKU_IMCOMING  阅读(8)  评论(0)    收藏  举报