C2. Potions (Hard Version)

这篇博客介绍了Codeforces 1526C2问题的三种解决方案,包括带后悔的贪心策略,利用线段树进行区间修改的方法,以及结合树上二分和贪心的复杂技术。在带后悔的贪心方法中,遇到负数时使用小根堆处理。线段树方案则通过查询前缀和数组来寻找匹配的正数。最后的线段树+树上二分的解法,通过贪心策略选择下标更大的值,以最大化选取的负数个数。
摘要由CSDN通过智能技术生成

Problem - 1526C2 - Codeforces

三种写法

带后悔的贪心

对于每一个数,如果它是非负数,那么就选择该值,如果为负数,则优先选定负数当中的较小值(即每次舍弃值时都舍弃选中值中的最小值)

从左到右枚举,遇到数值就将其加入生命值,当遇到负数时,就将其加入优先队列,每当生命值变为负数时,就减去队列中最小的值,并将该值弹出优先队列(注意优先队列是小根堆)

同为带后悔的贪心题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)
    {
  
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值