Codeforces Round #723 (Div. 2) C1(优先队列/dp)

https://codeforces.com/contest/1526/problem/C1

题目大意:

n≤2000 

一行有n种药剂,药剂1在最左边,药剂n在最右边。每种药剂在饮用时都会增加你ai的生命值。ai可以是负的,意味着药剂会降低生命值。

你从0生命值开始,从左向右走,从第一瓶药水走到最后一瓶。在每一种药剂上,你可以选择喝下它或者忽略它。你必须确保你的健康总是非负的。

你最多能喝多少种药水?
input 
6
4 -4 1 -3 1 -3
output 
5

//升序队列,小根堆 priority_queue <int,vector<int>,greater<int> > q;
//降序队列,大根堆 priority_queue <int,vector<int>,less<int> >q;

#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef pair<LL,LL> PII;
const LL MAXN=1e18;
const LL N=5000200,M=2002;
LL a[N];
int main()
{
   cin.tie(0); cout.tie(0); ios::sync_with_stdio(false);
   LL T=1;
   //cin>>T;
   while(T--)
   {
       LL n;
       cin>>n;
       LL sum=0,num=0,maxn=0;
       priority_queue<LL> q;//小根堆
       for(LL i=1;i<=n;i++)
       {
           cin>>a[i];
           if(a[i]>=0)
           {
               num++;
               sum+=a[i];
           }
           else
           {
               if(sum+a[i]>=0)
               {
                   sum+=a[i];
                   q.push(abs(a[i]));
               }
               else if(!q.empty())
               {
                   LL t=q.top();
                   if(t>abs(a[i]))
                   {
                       sum+=t;
                       q.pop();
                       sum+=a[i];
                       q.push(abs(a[i]));
                   }
               }
               LL res=q.size();
               maxn=max(maxn,res);
           }
       }
       cout<<num+maxn<<endl;
   }
   return 0;
}
posted @ 2022-11-03 20:18  高尔赛凡尔娟  阅读(31)  评论(0编辑  收藏  举报