当前位置:网站首页>Codeforces - 1526C1&&C2 - Potions
Codeforces - 1526C1&&C2 - Potions
2022-07-06 16:14:00 【Hello_ Ä】
Codeforces - C1&&C2 - Potions
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≤200000) — 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
Problem analysis
This question means yes n Bottle medicine , You will recover after eating a[i] Hit points or reduce a[i] Point health , You eat from left to right ( You can also choose not to eat ), Ask how many bottles of medicine you can take when your health value is not negative .
because c1 and c2 Only data size differences , I'll just follow c2 To write , This question can be directly written greedily , Just ignore those small potions .
We can prepare a small root pile , Insert the potion into the small root pile from left to right , When the sum of small root piles is negative , Just pop up the smallest element , Until the sum is not negative , In this process, maintain the maximum amount of water taken . Every insert and eject is logn, So the total complexity is nlogn.
AC Code
#include<iostream>
using namespace std;
#include<vector>
#include<algorithm>
#include<math.h>
#include<set>
#include<numeric>
#include<string>
#include<string.h>
#include<iterator>
#include<map>
#include<unordered_map>
#include<stack>
#include<list>
#include<queue>
#include<iomanip>
#define endl '\n'
#define int ll
typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll, ll>PII;
const int N = 2e5 + 50;
int n, m;
signed main()
{
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
int t;
int n;
cin >> n;
vector<int>v(n);
priority_queue<int,vector<int>,greater<int>>que;
int res = 0,ans=0;
for (int i = 0; i < n; i++)
{
cin >> v[i];
que.push(v[i]);
res += v[i];
while (res < 0)
{
res -= que.top();
que.pop();
}
ans = max(ans, (ll)que.size());
}
cout << ans << endl;
return 0;
}
边栏推荐
- (POJ - 3685) matrix (two sets and two parts)
- Penetration test (1) -- necessary tools, navigation
- frida hook so层、protobuf 数据解析
- Interval sum ----- discretization
- 渗透测试 ( 3 ) --- Metasploit Framework ( MSF )
- QT实现窗口置顶、置顶状态切换、多窗口置顶优先关系
- 计算时间差
- Opencv learning log 24 -- Hough transform 2 (maximum interval and minimum length can be limited)
- 875. 爱吃香蕉的珂珂 - 力扣(LeetCode)
- 1689. Ten - the minimum number of binary numbers
猜你喜欢

PySide6 信号、槽

分享一个在树莓派运行dash应用的实例。

Penetration testing (5) -- a collection of practical skills of scanning King nmap and penetration testing tools

C language is the watershed between low-level and high-level

Data storage in memory & loading into memory to make the program run

1605. Sum the feasible matrix for a given row and column

Codeforces Round #801 (Div. 2)A~C

Information security - threat detection - Flink broadcast stream broadcaststate dual stream merging application in filtering security logs

力扣——第298场周赛

顺丰科技智慧物流校园技术挑战赛(无t4)
随机推荐
Socket communication
C language is the watershed between low-level and high-level
window11 conda安装pytorch过程中遇到的一些问题
Codeforces Round #799 (Div. 4)A~H
(POJ - 2739) sum of constructive prime numbers (ruler or two points)
双向链表—全部操作
Bidirectional linked list - all operations
[exercise-6] (PTA) divide and conquer
Flag framework configures loguru logstore
628. Maximum product of three numbers
读取和保存zarr文件
C language must memorize code Encyclopedia
(POJ - 3258) River hopper (two points)
Gartner: five suggestions on best practices for zero trust network access
b站 实时弹幕和历史弹幕 Protobuf 格式解析
Find 3-friendly Integers
2027. Minimum number of operations to convert strings
Penetration test 2 --- XSS, CSRF, file upload, file inclusion, deserialization vulnerability
Codeforces Round #800 (Div. 2)AC
Understand what is a programming language in a popular way