题目:空调
题目链接:https://www.acwing.com/problem/content/description/4265/
思路:
对希望温度与实际温度做差,再对这个做差数组做差分。我们的每次操作等价于在差分数组中选一个数加一或者选两个数一个加一,一个减一。找出差分数组中正数和和负数和中绝对值大的那个就行。
代码:
cpp
#include<bits/stdc++.h>
using namespace std;
using ll = long long;
const ll N = 100005;
const ll mod = 1e9 + 7;
const int maxn = 2005;
int gcd(int a, int b) {
return b ? gcd(b, a % b) : a;
}
int p[N];
int t[N];
int fnt[N];
int subs[N];
void solve() {
int n; cin >> n;
for (int i = 1; i <= n; i++) {
cin >> p[i];
}
for (int i = 1; i <= n; i++) {
cin >> t[i];
}
for (int i = 1; i <= n; i++) {
subs[i] = p[i] - t[i];
}
for (int i = 1; i <= n; i++) {
fnt[i] = subs[i] - subs[i - 1];
}
int sump = 0, sumn = 0;
for (int i = 1; i <= n; i++) {
if (fnt[i] > 0)
sump += fnt[i];
else
sumn += -fnt[i];
}
cout << max(sump, sumn) << '\n';
}
signed main() {
ios::sync_with_stdio(false);
cin.tie(0);
std::cout.tie(0);
int t = 1; //cin >> t;
while (t--)
solve();
return 0;
}