Easy and Hard
链接:
C1. Potions (Easy Version) https://codeforces.com/contest/1526/problem/C1
C2. Potions (Hard Version) https://codeforces.com/contest/1526/problem/C2
题意:
喝药水,有正有负,期间要求和大于0,求最多能喝多少瓶
简单和困难差别是数据量
输入
6
4 -4 1 -3 1 -3
输出量
5
解:
思路,能喝都喝,喝完每一瓶判断一下,喝完如果变负数的和,看看喝过的有没有比这一瓶更小的(扣更多的),如果有,那瓶更小的就不喝,喝这瓶,没有,这瓶就不喝,之前没喝过负数(扣血)的,这瓶负数就不喝(因为随时都要保持和大于0)
实际代码:
Easy:
数组+sort,组成(伪)有序
#include<iostream>
#include<cmath>
#include<algorithm>
#include<cstring>
//#include<bits/stdc++.h>
using namespace std;
typedef long long int ll;
int rq[2001];//容器
int main()
{
ll sum=0;
int ans=0;
int fu=0;
int n;
cin>>n;
memset(rq,0,sizeof(rq));//初始化
for(int i=1;i<=n;i++)
{
int temp;//输入数字
cin>>temp;
sum+=temp;//加入总和
ans++;//先增加饮用数量
if(fu>0) sort(rq,rq+fu);//对容器排序
if(sum<0)//和小于0
{
if(fu>0)//前面有饮用负数
{
if(temp<rq[0])//现在喝的比之前的负数还小
{
sum-=temp;//不喝
ans--;
}
else //现在喝的比之前的负数大等
{
sum-=rq[0];//前面不喝,换成喝现在的
rq[0]=temp;//更新容器
ans--;
}
}
else //前面没有饮用负数
{
sum-=temp;//直接放弃
ans--;
}
}
else if(temp<0) rq[fu++]=temp;//加入负数容器
//cout<<sum<<" "<<ans<<endl;
}
cout<<ans<<endl;
}
Hard的话,因为数组变大,再sort导致超时,只能用更优容器解决
我的好哥哥使用了优先队列
priority_queue<ll,vector<ll>,less<ll>>s;//大顶堆
我用的是multiset
multiset<int>rq;
限制:
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output