跳到正文

题解

CF1987B 题解

Vitamin_B约 1 分钟
原文
文章信息与版本

文章信息

发布
2024年7月6日
收录
2025年12月12日
更新
2026年11月21日
篇幅
约 1 分钟
题目
CF1987B

版本

  1. v1当前

思路

不管怎么操作,不难得出 ∑k=∑ai\sum k=\sum a_i。所以,我们只要求最少可以取走几个连续线段即可(显然,答案就是所有最长上升子串的最高峰之和)。

代码

C++
# include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair <int, int> pii;
int t, n, x, lst, maxx;
ll sum;
int main () {
	ios::sync_with_stdio (0);
	cin.tie (0);
	cout.tie (0);
	cin >> t;
	while (t --) {
		cin >> n >> lst;
		sum = maxx = 0;
		while (-- n) {
			cin >> x;
			if (x < lst)
				maxx = max (maxx, lst - x), sum += lst - x;
			else
				lst = x;
		}
		cout << maxx + sum << '\n';
	}
	return 0;
}
原文