三种写法
带后悔的贪心
对于每一个数,如果它是非负数,那么就选择该值,如果为负数,则优先选定负数当中的较小值(即每次舍弃值时都舍弃选中值中的最小值)
从左到右枚举,遇到数值就将其加入生命值,当遇到负数时,就将其加入优先队列,每当生命值变为负数时,就减去队列中最小的值,并将该值弹出优先队列(注意优先队列是小根堆)
同为带后悔的贪心题176. 装满的油箱 - AcWing题库
#include<iostream>
#include<cstring>
#include<cstdio>
#include<queue>
#include<map>
#include<set>
#include<vector>
#include<algorithm>
#include<string>
#include<bitset>
#include<cmath>
#include<array>
//#define int ll
#define pb push_back
#define endl '\n'
#define x first
#define y second
#define Endl endl
#define pre(i,a,b) for(int i=a;i<=b;i++)
#define rep(i,b,a) for(int i=b;i>=a;i--)
#define si(x) scanf("%d",&x);
#define sl(x) scanf("%lld",&x);
#define ss(x) scanf("%s",x);
using namespace std;
typedef long long ll;
typedef pair<int, int> PII;
typedef pair<int, PII> PIII;
typedef pair<char, int> PCI;
typedef pair<double, double> PDD;
typedef pair<ll, ll> PLL;
const int N = 300010, M = 2 * N;
const int INF = 0x3f3f3f3f;
const long long LLINF = 1e18;
int dx[4] = { -1,0,1,0 }, dy[4] = { 0,1,0,-1 };
int n, m, k;
int a[N];
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
ll lowbit(ll x) { return x & -x; }
ll qmi(ll x, ll n, ll mod)
{
ll res = 1;
while (n)
{
if (n & 1)res = (1ll * res * x) % mod;
x = (1ll * x * x) % mod;
n >>= 1;
}
return res;
}
void init() {}
priority_queue<int,vector<int>, greater<int>> q;
void solve()
{
si(n);
pre(i, 1, n) si(a[i]);
ll he = 0, cnt = 0;
pre(i, 1, n)
{