传送门:https://codeforces.com/contest/1700/problem/C

题意:

有n个数,有3种操作:

1.选取任意前缀区间[1,i],所有数大小-1。

2.选取任意后缀区间[i,n],所有数大小-1。

3.所有数大小+1。

问:要实现所有的数字都为0,最少要操作多少次。

wtn法:

左边非递增的刀,右边非递减的刀。只需左边一刀把它砍成非递减的数列,然后右边刀也好砍了。

然后就变成了一个平的数列,答案加上·他的·绝对值就行了。

#include<bits/stdc++.h> 
const int N = 2e5 + 10;
#define int long long
int a[N];
signed main() {
	int t; std::cin >> t;
	while (t--) {
		int n; std::cin >> n;
		for (int i = 1; i <= n; i++)std::cin >> a[i];
		int l = 0;
		for (int i = 1; i < n; i++) {
			l += std::max(0ll, a[i] - a[i + 1]); 
		}
		int last = 0;
		for (int i = n - 1; i >= 1; i--) {
			a[i] -= last;
			int k = std::max(0ll, a[i] - a[i + 1]);
			if (k) {
				last += k;
				a[i] -= k;
			}
		}
		
		//for (int i = 1; i <= n; i++)std::cout << a[i] << " \n"[i == n];
		int r = std::max(0ll, a[n]-a[1]);
		int m = std::abs(a[1]);
		int ans = r + l + m;
		//std::cout << l << "," << r << "," << m << std::endl;
		std::cout << ans << std::endl;
	}
}

ywh法(差分数组):

作差分b[i]=a[i]-a[i-1]

操作变为:

1.b[1]-1.,b[i]+1;

2.b[i]-1

3.b[1]+1

然后就能秒杀

posted on 2022-07-14 17:03  wtn135687  阅读(56)  评论(0)    收藏  举报