跳到正文

个人记录

LZM_反悔贪心

King_tissue约 36 分钟
原文
文章信息与版本

文章信息

收录
2026年7月15日
更新
2026年7月15日
篇幅
约 36 分钟

版本

  1. v1当前

反悔贪心·股票周目

当局部最优解不能得到全局最优解,而每一步的有效选择非常少,那么可以构建返回机制纠正之前的贪心。

CF865D

考虑枚举第 i 天卖出前 i 天的最低的股价的股票。

维护小根堆存前 i 天的最低股价,贪心的风险在于第 i 天不应该卖出某只股票。

构建第 j 天撤回第 i 天卖的操作反悔机制。

P2949

考虑按截止时间从小到大贪心选取任务。

维护 cnt 表示已经安排的工作,当枚举到排序后的工作 i 时。

若时间不够,应置换之前的收益较小的工作。

P4053 P15699

与 P2949 很相似,给出一个 P15699 的手写题解。

一道模板反悔贪心题。

反悔贪心:当局部最优解不能得到全局最优解,而每一步的有效选择非常少,那么可以构建返回机制纠正之前的贪心。

反悔贪心的常见应用为完成任务,每一个任务限时一个最晚的完成时间,要尽量多完成任务。

此题中上升的高度相当于完成任务的所需时间,最高的海拔限制加上完成任务的时间相当于最晚的完成时间,尽量爆炸更多的气球相当于尽量多完成任务。

所以把求出的最晚的完成时间排序,每次直接完成任务,如果时间不足以完成,就把先前的耗时最长任务撤销。

P1484

种树是我困扰了两年(?)的梦魇。

记过太多次了直接略。

P4823 拯救真人人

∑ai+bk≥H\sum a_i + b_k \ge H 就可以逃出去。

第一次贪心的时候按照 ai+bia_i + b_i 从小到大跑。

考虑反悔。因为价值都是1,可以把任意逃出去的人人和没逃出去的互换。

因为之前按照 a+ba + b 跑,所以方案一定可行。

假如第 kk 个人人跑不出去,即 ∑ai+bk<H\sum a_i + b_k < H,那么我们就可以把一个 aa 最大的拉回来给后面的垫脚,否则直接跑。

反悔贪心模拟赛

45 + 100 + 43 + 100 = 288,rk 7,我是罪人。

T1 P16789

错因:极大值 2e14 不开 int128,我是sb我是sb😭

将核心任务视为贡献极大值,于是就变成板子了。

code
C++
#include <bits/stdc++.h>
#define int long long
using namespace std;
const int INF = 2e14;
int n;
struct node {
	int t, w;
} a[200005];
bool cmp (node qwe, node ewq) {
	return qwe.t < ewq.t;
}
priority_queue<int, vector<int>, greater<int> > pq;
signed main() {
	ios::sync_with_stdio(0);
	cin.tie(0); cout.tie(0);
	cin >> n;
	for (int i = 1; i <= n; i++) {
		int zrr;
		cin >> a[i].t >> a[i].w >> zrr;
		a[i].w += INF * zrr;
	}
	sort(a + 1, a + n + 1, cmp);
	int now = 1;
    __int128 ans = 0;
	for (int i = 1; i <= n; i++) {
		if (a[i].t < now) {
			if (!pq.empty() && pq.top() < a[i].w) {
				ans -= pq.top(); pq.pop();
				ans += a[i].w; pq.push(a[i].w);
			}
		} else {
			ans += a[i].w; pq.push(a[i].w);
			now++;
		}
	}
    int ans1 = ans / INF, ans2 = ans % INF;
	cout << ans1 << " " << ans2 << "\n";
	return 0;
} 

T2 P3545

考虑与普通反贪的差异在于每一次的总值不同,但是很显然只需要每一次循环时单独用不同的值即可,必然不会有冲突。

code
C++
#include <bits/stdc++.h>
#define int long long
using namespace std;
const int INF = 1e15;
int n;
struct node {
	int t, w;
} a[300005];
//bool cmp (node qwe, node ewq) {
//	return qwe.t < ewq.t;
//}
priority_queue<pair<int, int> > pq;
signed main() {
	ios::sync_with_stdio(0);
	cin.tie(0); cout.tie(0);
	cin >> n;
	for (int i = 1; i <= n; i++) {
		cin >> a[i].t;
	}
	for (int i = 1; i <= n; i++) {
		cin >> a[i].w;
	}
//	sort(a + 1, a + n + 1, cmp);
	int now = 0, ans = 0;
	for (int i = 1; i <= n; i++) {
		now += a[i].t;
		if (a[i].w > now) {
//			cerr << i << "\n"; 
			if (!pq.empty() && pq.top().first > a[i].w) {
				now += pq.top().first; pq.pop();
				now -= a[i].w; pq.push({a[i].w, i});
			}
		} else {
			now -= a[i].w; pq.push({a[i].w, i});
			ans++;
		}
	}
	cout << ans << "\n";
	while(!pq.empty()) {
		cout << pq.top().second << " ";
		pq.pop();
	}
	return 0;
} 

T3 P3045

错因:思路错误。这集没招了😡

什么叫这题 YL 做过我还不会啊😱

注意到 KK 张优惠卷对应先买 KK 只折扣牛花费尽量低。

然后每一头牛按照原价排序。

此时每一头牛有两种选择:

  1. 换一头折扣牛出来买原价,自己去买折扣价

  2. 直接自己买原价

所以取 min 即可,注意需要三个小根堆,其中 pq3 不一定需要赋值可以开 int 其实,但是调错的时候为了确认单一变量改掉了。

code
C++
#include <bits/stdc++.h>
#define int long long
using namespace std;
int n, k, m;
bool flag[300005];
struct node {
	int first, second;
	bool operator < (const node &tmp) const {
		return first > tmp.first;
	}
};
struct zrr {
	int t, w;
} a[300005];
priority_queue<node> pq1, pq2, pq3;
//priority_queue<int, vector<int>, greater<int> > pq3;
signed main() {
	ios::sync_with_stdio(0);
	cin.tie(0); cout.tie(0);
	cin >> n >> k >> m;
	for (int i = 1; i <= n; i++) {
		cin >> a[i].t >> a[i].w;
		pq1.push({a[i].w, i});
		pq2.push({a[i].t, i});
	}
	int sum = 0, ans = 0;
	for (int i = 1; i <= k; i++) {
		if (sum + pq1.top().first > m) {
			break;
		}
		sum += pq1.top().first;
		flag[pq1.top().second] = 1; ans++;
		pq3.push({a[pq1.top().second].t - a[pq1.top().second].w, pq1.top().second});
		pq1.pop();
	} 
	if (ans < k) {
		cout << ans << "\n";
		return 0;
	}
	while(ans < n) {
		while(!pq1.empty() && flag[pq1.top().second]) pq1.pop();
		while(!pq2.empty() && flag[pq2.top().second]) pq2.pop();
		if (pq2.empty()) break;
		int v1 = pq1.top().first, v2 = pq2.top().first, v3 = pq3.top().first;
		int g1 = pq1.top().second, g2 = pq2.top().second, g3 = pq3.top().second;
		if (sum + min(v2, v1 + v3) > m) break;
		if (v2 >= v1 + v3) {
			flag[g3] = flag[g1] = 1;
			sum += (v1 + v3);
			ans++;
			pq1.pop(); pq3.pop();
			pq3.push({a[g1].t - a[g1].w, g1});
		} else {
			flag[g2] = 1;
			sum += v2;
			ans++;
			pq2.pop();
		}
	}
	cout << ans << "\n";
	return 0;
}

T4 P10736

过了,但是这集真的神了😍

定义 d[i] = h[i] - h[i - 1],即相邻高度差,所以有如下有关 d[i] 的性质。

  1. di=−1,0,1d_i = {-1, 0, 1}

  2. ∑di≥0\sum d_i ≥ 0

  3. ∑i=1n+1di=0\sum_{i = 1} ^{n + 1} d_i = 0

同时,注意到一步巧妙地转换:

Ans=∑i=1nPihi=∑i=1nPi∑j=indj=∑j=1n+1dj∑i=jnPiAns = \sum_{i = 1}^n P_i h_i = \sum_{i = 1}^n P_i \sum_{j = i}^n d_j = \sum_{j = 1}^{n + 1} d_j \sum_{i = j}^n P_i

定义 sj=pjs_j = p_j 后缀和,则原式等于 ∑j=1n+1djsj\sum_{j = 1}^{n + 1} d_j s_j

  • 再定义前缀和 sumisum_i,原式变成 ∑i=1n+1di(sumn−sumi)\sum_{i = 1}^{n + 1} d_i (sum_n - sum_i)

因为最终 d[i] 和为 0,所以相当于选取点对 (x, y),贡献 sum[x]<sum[y]sum[x] < sum[y] 且 x<yx < y(始终 did_i 前缀和大于 0)

所以此时题目转换为了买股票😋,而且我的前缀和比老师的神秘后缀和短多了⚡

code
C++
#include <bits/stdc++.h>
#define int long long
using namespace std;
const int INF = 1e15;
int n, a[300005];
priority_queue<int, vector<int>, greater<int> > pq;
signed main() {
	ios::sync_with_stdio(0);
	cin.tie(0); cout.tie(0);
	cin >> n;
	for (int i = 1; i <= n; i++) {
		cin >> a[i]; a[i] += a[i - 1];
	}
	pq.push(0);
	int ans = 0;
	for (int i = 1; i <= n; i++) {
		pq.push(a[i]);
		if (a[i] > pq.top()) {
			ans += (a[i] - pq.top());
			pq.pop(); pq.push(a[i]);
		}
	}
	cout << ans << "\n";
	return 0;
} 

反悔贪心·种树周目

什么叫涉及链表的反悔贪心😍

P1484 种树·周目三

依旧种树。

见 Day 1 解释

code
C++
#include <bits/stdc++.h>
#define int long long
using namespace std;
int n, k, tmp[300005];
bool flag[300005];
struct zrr {
    int first, second;
    bool operator<(const zrr &t)const {
		return first < t.first;
	}
};
priority_queue<zrr> pq;
struct node {
	int l, r;
} a[300005];
signed main() {
	ios::sync_with_stdio(0);
	cin.tie(0); cout.tie(0);
	cin >> n >> k;
	for (int i = 1; i <= n; i++) {
		cin >> tmp[i];
		a[i].l = i - 1;
		a[i].r = i + 1;
		pq.push({tmp[i], i});
	}
	int ans = 0, sum = 0;
	while (!pq.empty() && ans < k) {
		if (pq.top().first < 0) break;
		zrr h = pq.top(); pq.pop();
		if (flag[h.second]) continue;
		int cur = h.second, L = a[cur].l, R = a[cur].r;
		ans++;
        sum += h.first;
		tmp[cur] = (tmp[L] - tmp[cur] + tmp[R]);
        flag[L] = flag[R] = 1;
        // sum += tmp[cur];
		a[cur].l = a[L].l;
		a[a[cur].l].r = cur;
		a[cur].r = a[R].r;
		a[a[cur].r].l = cur;
		pq.push({tmp[cur], cur});
	}
	cout << sum << "\n";
	return 0;
}

P3620

转换逻辑:

  1. 注意到电缆不穿过楼

  2. 所以相当于相邻两个”树”连边

  3. 如果要与已经连边的”树”连边,那么相当于种树时抢邻居,其贡献同样为两侧减去中央

特别地,这次必须要选 k 个,循环逻辑和排序与种树有不同。

首尾的 0 和 n 不能连电缆,所以要单独设置哨兵。

code
C++
#include <bits/stdc++.h>
#define int long long
using namespace std;
const int INF = 1e18;
int n, k, tmp[300005];
bool flag[300005];
struct zrr {
    int first, second;
    bool operator<(const zrr &t)const {
		return first > t.first;
	}
};
priority_queue<zrr> pq;
struct node {
	int l, r;
} a[300005];
signed main() {
	ios::sync_with_stdio(0);
	cin.tie(0); cout.tie(0);
	int last;
	cin >> n >> k >> last;
	for (int i = 1; i < n; i++) {
		cin >> tmp[i];
		int g = tmp[i];
		tmp[i] -= last;
		a[i].l = i - 1;
		a[i].r = i + 1;
		pq.push({tmp[i], i});
		last = g;
	}
	// n--;
	int ans = 0, sum = 0;
	tmp[0] = tmp[n] = INF;
	// while (!pq.empty() && ans < k) {
	for (int i = 1; i <= k; i++) {
		// if (pq.top().first < 0) break;
		// zrr h = pq.top(); pq.pop();
		while (flag[pq.top().second]) pq.pop();
		zrr h = pq.top(); pq.pop();
		int cur = h.second, L = a[cur].l, R = a[cur].r;
		// ans++;
        sum += h.first;
		tmp[cur] = (tmp[L] - tmp[cur] + tmp[R]);
        flag[L] = flag[R] = 1;
        // sum += tmp[cur];
		a[cur].l = a[L].l;
		a[a[cur].l].r = cur;
		a[cur].r = a[R].r;
		a[a[cur].r].l = cur;
		pq.push({tmp[cur], cur});
	}
	cout << sum << "\n";
	return 0;
}

P14374 糖果

日本属于吉林省👍

我是傻逼我是傻逼我是傻逼我是傻逼😎

我是傻逼我输入写错了🤗

一眼种树🤩

只需要每一步操作都记录答案即可,因为每一次选择或者反悔都会增加一个糖果

输入一定不要输入 k😡🥵

code
C++
#include <bits/stdc++.h>
#define int long long
using namespace std;
const int INF = -1e18;
int n, k, tmp[300005], kop[300005];
bool flag[300005];
struct zrr {
    int first, second;
    bool operator<(const zrr &t)const {
		return first < t.first;
	}
};
priority_queue<zrr> pq;
struct node {
	int l, r;
} a[300005];
signed main() {
	ios::sync_with_stdio(0);
	cin.tie(0); cout.tie(0);
	cin >> n;
	for (int i = 1; i <= n; i++) {
		cin >> tmp[i];
		a[i].l = i - 1;
		a[i].r = i + 1;
		pq.push({tmp[i], i});
	}
    tmp[0] = tmp[n + 1] = INF;
	int sum = 0, guo = 0;
	for (int i = 1; i <= n / 2 + (n % 2); i++) {
		// if (pq.top().first < 0) break;
		zrr h = pq.top(); pq.pop();
		if (flag[h.second]) {
            i--;
            continue;
        }
		int cur = h.second, L = a[cur].l, R = a[cur].r;
        sum += h.first;
		tmp[cur] = (tmp[L] - tmp[cur] + tmp[R]);
        guo++;
        kop[guo] = sum;
        flag[L] = flag[R] = 1;
        // sum += tmp[cur];
		a[cur].l = a[L].l;
		a[a[cur].l].r = cur;
		a[cur].r = a[R].r;
		a[a[cur].r].l = cur;
		pq.push({tmp[cur], cur});
	}
	for (int i = 1; i <= n / 2 + (n % 2); i++) {
		cout << kop[i] << "\n";
	}
	return 0;
}

P10478 / P6821 / P13077

三倍经验这一块

第一,考虑连续的正数或者连续的负数可以合并在一起,变成正负交错的数列。

此时消除两头的负数,否则可能和哨兵一起拼起来形成一个正值,导致答案被影响,保证数列正起正尾。

然后考虑如果正数段的数量 cntcnt 小于等于 kk 直接输出,否则要做 cnt−kcnt - k 次反悔。

每一次反悔有两种可能:

  1. 选择一个正数放弃它。

  2. 选择一个负数并加入,连接起两个整数合并为一个数。

相当于每一次反悔必须减少一个段。

此时发现每一个数的贡献等于它的绝对值取负数。

按照这个值排序大根堆每一次选择最大的负数进行反悔。

code
C++
#include <bits/stdc++.h>
#define int long long
using namespace std;
const int INF = -1e18;
int n, nm, k, tmp[1000005], kl[1000005];
bool flag[1000005];
struct zrr {
    int first, second;
    bool operator<(const zrr &t)const {
		return first < t.first;
	}
};
priority_queue<zrr> pq;
struct node {
	int l, r;
} a[1000005];
signed main() {
	ios::sync_with_stdio(0);
	cin.tie(0); cout.tie(0);
	cin >> nm >> k;
	bool zrrr = 1;
	for (int i = 1; i <= nm; i++) {
		cin >> kl[i];
		if (zrrr) {
			if (kl[i] > 0) tmp[++n] = kl[i], zrrr = 0;
			continue;
		}
		if ((kl[i] > 0 && kl[i - 1] <= 0) || (kl[i] <= 0 && kl[i - 1] > 0)) {
			tmp[++n] = kl[i];
		} else {
			tmp[n] += kl[i];
		}
	}
	// cout << n << "\n";
	if (tmp[n] <= 0) n--;
	tmp[0] = tmp[n + 1] = INF;
	int ans = 0, sum = 0;
	for (int i = 1; i <= n; i++) {
		a[i].l = i - 1;
		a[i].r = i + 1;
		pq.push({-abs(tmp[i]), i});
		if (tmp[i] > 0) {
			ans++; sum += tmp[i];
		}
		tmp[i] = -abs(tmp[i]);
		// if (i % 2) assert(tmp[i] >= 0);
	}
	while (!pq.empty() && ans > k) {
		// if (pq.top().first < 0) break;
		zrr h = pq.top(); pq.pop();
		if (flag[h.second]) continue;
		int cur = h.second, L = a[cur].l, R = a[cur].r;
		ans--;
        sum += h.first;
		tmp[cur] = (tmp[L] - tmp[cur] + tmp[R]);
        flag[L] = flag[R] = 1;
        // sum += tmp[cur];
		a[cur].l = a[L].l;
		a[a[cur].l].r = cur;
		a[cur].r = a[R].r;
		a[a[cur].r].l = cur;
		pq.push({tmp[cur], cur});
	}
	cout << sum << "\n";
	return 0;
}

CF436E

狗屎题😋

注意到”至少” k 颗星星其实就是 刚好 k 颗星星,因为你能拿到更多的话不如少拿一点。

尝试构建反悔贪心。

注意一个构建时极其重要的性质:每一次贪心不论选择或者反悔都应该刚好获得一颗星星

那么就会有如下一些(?)情况:

  • 选择一个最小的还未尝试过的关卡,将其打为蓝V,此时耗时 a[i]

  • 选择一个最小的蓝V关卡,将其收掉,耗时 b[i] - a[i]

  • 选择一个最小的还未尝试过的关卡,将其收掉,再返回一个最大的蓝V关卡设为还未尝试,耗时 b[i] - a[j]

  • 选择一个最小的还未尝试过的关卡,将其收掉,再返回一个最大的已收关卡设为蓝V,耗时 b[i] - (b[j] - a[j])

于是发现。?此时需要一些(?)堆来吃屎。

  • 一个小根堆 pq1,用于存 a[i]

  • 一个小根堆 pq2,用于存 b[i] - a[i]

  • 一个小根堆 pq3,用于存 b[i]

  • 一个大根堆 pq4,表示已经吃了 a[i]

  • 一个大根堆 pq5,表示又吃了 b[i] - a[i]

具体逻辑疑似过于复杂不在这里赘述。

code
C++
#include <bits/stdc++.h>
#define int long long
using namespace std;
const int INF = 1e18;
int n, k, a[300005], b[300005];
int flag[300005];
// first 是值 second 是 id
struct D {
    int first, second;
    bool operator<(const D &t)const {
		return first > t.first;
	}
};
struct X {
    int first, second;
    bool operator<(const X &t)const {
		return first < t.first;
	}
};
priority_queue<D> pq1, pq2, pq3;
priority_queue<X> pq4, pq5;
signed main() {
	ios::sync_with_stdio(0);
	cin.tie(0); cout.tie(0);
	cin >> n >> k;
	for (int i = 1; i <= n; i++) {
		cin >> a[i] >> b[i];
		pq1.push({a[i], i});
		pq3.push({b[i], i});
	}
	int ans = 0;
	for (int i = 1; i <= k; i++) {
		while(!pq1.empty() && flag[pq1.top().second] != 0) pq1.pop();
		while(!pq2.empty() && flag[pq2.top().second] != 1) pq2.pop();
		while(!pq3.empty() && flag[pq3.top().second] != 0) pq3.pop();
		while(!pq4.empty() && flag[pq4.top().second] != 1) pq4.pop();
		while(!pq5.empty() && flag[pq5.top().second] != 2) pq5.pop();
		int k1 = INF, k2 = INF, k3 = INF, k4 = INF;
		if (!pq1.empty()) k1 = pq1.top().first;
		if (!pq2.empty()) k2 = pq2.top().first;
		if (!pq3.empty() && !pq4.empty()) k3 = pq3.top().first - pq4.top().first;
		// if (i == 3) {
			// if (pq3.empty()) cout << "3";
			// else cout << "5";
			// cerr << pq3.top().first << " " << pq5.top().first << "\n";
		// }
		if (!pq3.empty() && !pq5.empty()) k4 = pq3.top().first - pq5.top().first;
		if (k1 <= min({k2, k3, k4})) { // k1
			int f = pq1.top().first, s = pq1.top().second;
			// cerr << "1" << "f:" << f << " " << "s:" << s << "\n";
			ans += f;
			flag[s] = 1;
			pq2.push({b[s] - a[s], s});
			pq4.push({a[s], s});
		} else if (k2 <= min({k1, k3, k4})) { // k2
			int f = pq2.top().first, s = pq2.top().second;
			// cerr << "2" << "f:" << f << " " << "s:" << s << "\n";
			ans += f;
			flag[s] = 2;
			// pq3.push({b[s], s});
			pq5.push({b[s] - a[s], s});
		} else if (k3 <= min({k1, k2, k4})) { // k3
			int f = pq3.top().first, s = pq3.top().second;
			int f2 = pq4.top().first, s2 = pq4.top().second;
			// cerr << "3" << "f:" << f << " " << "s:" << s << "\n";
			// cerr << " " << "f2:" << f2 << " " << "s2:" << s2 << "\n";
			ans += (f - f2);
			flag[s] = 2; flag[s2] = 0;
			// pq3.push({b[s], s});
			pq5.push({b[s] - a[s], s});
			pq1.push({a[s2], s2});
			pq3.push({b[s2], s2});
		} else { // k4
			int f = pq3.top().first, s = pq3.top().second;
			int f2 = pq5.top().first, s2 = pq5.top().second;
			// cerr << "4" << "f:" << f << " " << "s:" << s << "\n";
			// cerr << " " << "f2:" << f2 << " " << "s2:" << s2 << "\n";
			ans += (f - f2);
			flag[s] = 2, flag[s2] = 1;
			// pq3.push({b[s], s});
			pq5.push({b[s] - a[s], s});
			pq2.push({b[s2] - a[s2], s2});
			pq4.push({a[s2], s2});
		}
	}
	cout << ans << "\n";
	for (int i = 1; i <= n; i++) {
		cout << flag[i];
	} cout << "\n";
	return 0;
}

P9600

这题和一位。

反悔贪心模拟赛·周目二

100 + 80 + 100 + 30 = 310, rk1,我是夺冠王。

T1 P1792

还有种树五周目😘

环形种树,没了。

code
C++
#include <bits/stdc++.h>
#define int long long
using namespace std;
const int N = 2e6 + 5;
int n, m, a[N], l[N], r[N];
bool flag[N];
struct node {
	int first, second;
	bool operator < (const node A) const {
		return first < A.first;
	}	
};
priority_queue<node> pq;
signed main() {
	ios::sync_with_stdio(0);
	cin.tie(0); cout.tie(0);
	cin >> n >> m;
	if (m * 2 > n) {
		cout << "Error!";
		return 0;
	}
	for (int i = 1; i <= n; i++) {
		cin >> a[i];
		l[i] = i - 1; r[i] = i + 1;
		pq.push({a[i], i});
	}
	l[1] = n; r[n] = 1;
	int ans = 0;
	for (int i = 1; i <= m; i++) {
		node t = pq.top(); pq.pop();
		while (!pq.empty() && flag[t.second]) t = pq.top(), pq.pop();
		int cur = t.second;
		ans += t.first;
		flag[l[cur]] = flag[r[cur]] = 1;
		a[cur] = a[l[cur]] + a[r[cur]] - a[cur];
		l[cur] = l[l[cur]];
		r[l[cur]] = cur;
		r[cur] = r[r[cur]];
		l[r[cur]] = cur;
		pq.push({a[cur], cur});
	}
	cout << ans << "\n";
	return 0;
}

T2 P6821

已经讲过了😁

但是细节过于多挂了 20 分拉完了😎

后面的负数没有清掉我是谢允哲💋

代码略🤢

T3 P15726

难在哪😍😍😍

  1. 注意到匹配时选择尽量大的左括号,使用大根堆存储左括号序列。

  2. 每进入一个右括号,考虑与最大的左括号进行匹配。

但是并不一定每一次选择的右括号都是最优的,因此后面的右括号出现时有可能需要令其反悔。

  1. 所以一个很巧妙的办法是,在每一次右括号配对后加入一个虚拟的左括号,其价值为负的这个右括号的值,那么后面的右括号需要反悔时,选择这个左括号相当于抵消了这个需要反悔的右括号。
code
C++
#include <bits/stdc++.h>
#define int long long
using namespace std;
const int N = 2e6 + 5;
int n, m, a[N], l[N], r[N];
bool flag[N];
string s;
// struct node {
// 	int first, second;
// 	bool operator < (const node A) const {
// 		return A.first > first;
// 	}	
// };
priority_queue<int> pq;
signed main() {
	ios::sync_with_stdio(0);
	cin.tie(0); cout.tie(0);
	cin >> n >> s;
	s = " " + s;
	int ans = 0;
	for (int i = 1; i <= n; i++) {
		cin >> a[i];
		if (s[i] == '(') {
			pq.push(a[i]);
		} else {
			if (!pq.empty()) {
				if (pq.top() + a[i] < 0) continue;
				ans += pq.top() + a[i];
				pq.pop();
				pq.push(-a[i]);
			}
		}
	}
	cout << ans << "\n";
	return 0;
}

T4 P16289

疑似想的过于简单了?

低价时提前买电用于高价时使用。

每一个任务的电来源仅有电池的电和现场买两种可能。

对于一次任务:

  • 第一步:满足当次任务的电量需求

若 cic_i 价格低,直接全买掉

否则先用电池,比 cic_i 大再单独买

  • 第二步:反悔

若 cic_i 足够低,替换之前的高价电。

  • 第三步:处理电池容量

定义后缀数组 hzihz_i 表示第 ii 个及以后的任务的需求量。

不会超过 min(k,hzi)min(k, hz_i),超过的部分直接还回去。

  • 第四步:若电池容量未满

以 cic_i 的价格将其充满。

因为需要反悔高价电维护低价电,所以像普通的反悔贪心那样使用堆不合适,可以使用单调队列。

code
C++
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N = 2e6 + 5;
int n, k, w[N], c[N];
int q[N], head = 1, tail, num[N];
__int128 sum;
void out (__int128 ans) {
	if (ans == 0) {
		cout << 0;
		return;
	}
	__int128 k = 1;
	while(k < ans) k *= 10;
	k /= 10;
	while(k) {
		int zrr = ans / k;
		ans %= k;
		cout << zrr;
		k /= 10;
	}
}
signed main() {
	ios::sync_with_stdio(0);
	cin.tie(0); cout.tie(0);
	cin >> n >> k;
	for (int i = 1; i <= n; i++) {
		cin >> w[i];
	}
	for (int i = 1; i <= n; i++) {
		cin >> c[i];
	}
    int ans = 0;
    for (int i = 1; i <= n; i++) {
        while (head <= tail && c[q[tail]] >= c[i]) {
			ans -= num[q[tail--]];
		}	
        q[++tail] = i;
        num[i] = w[i] + k - ans;
        ans = w[i] + k;
        for (int j = head; j <= tail && w[i]; j++) {
            int fail = min(w[i], num[q[j]]);
            num[q[j]] -= fail;
            w[i] -= fail;
            sum += c[q[j]] * fail;
            ans -= fail;
            if (num[q[j]] == 0) head++;
        }
    }
    out(sum);
    return 0;
}
原文