链接
思路
差分 + 贪心。
先将两个序列做差,对作完差的序列取差分序列 s s s 。
我们需要求将 s s s 转化为全零差分序列的最小操作次数 t t t 。
证明:
- 将相应的操作取逆操作就能由全零的差分序列得到 s s s ,由 s s s 得到差序列,在加回去得原序列。
因为只能区间加减
1
1
1 ,结论是为 t = max(pos, neg)
。
证明:
- 设差分序列中正数的和为 p o s pos pos ,负数的和为 n e g neg neg 。
- 不妨设 p o s > n e g pos > neg pos>neg 。
- 首先可以进行 n e g neg neg 次操作,将所有负数归零。
- 然后再进行 p o s − n e g pos - neg pos−neg 次操作,将所有正数归零,减 1 1 1 对第 n − 1 n - 1 n−1 个数操作(不影响差序列)。
- 所以当 p o s > n e g pos > neg pos>neg 时, t = p o s t = pos t=pos 。
- 同理,当 n e g > p o s neg > pos neg>pos 时, t = n e g t = neg t=neg 。
- 综上所述, t = m a x ( p o s , n e g ) t=max(pos,neg) t=max(pos,neg) 。
代码
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <string>
#include <iostream>
#include <sstream>
#include <algorithm>
#include <cmath>
#include <vector>
#include <stack>
#include <queue>
#include <deque>
#include <bitset>
#include <set>
#include <map>
#include <unordered_set>
#include <unordered_map>
#define endl '\n'
#define fi first
#define se second
#define PI acos(-1)
#define LL long long
#define INF 0x3f3f3f3f
#define lowbit(x) (-x&x)
#define PII pair<int, int>
#define ULL unsigned long long
#define PIL pair<int, long long>
#define mem(a, b) memset(a, b, sizeof a)
#define rev(x) reverse(x.begin(), x.end())
#define IOS ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
using namespace std;
const int N = 1e5 + 10;
int a[N];
int n;
void solve() {
cin >> n;
for (int i = 1; i <= n; i ++ ) cin >> a[i];
for (int i = 1; i <= n; i ++ ) {
int b;
cin >> b;
a[i] -= b;
}
for (int i = n; i; i -- ) a[i] -= a[i - 1];
int pos = 0, neg = 0;
for (int i = 1; i <= n; i ++ ) {
if (a[i] > 0) pos += a[i];
else neg -= a[i];
}
cout << max(pos, neg) << endl;
}
int main() {
IOS;
solve();
return 0;
}