AcWing_4262
2024-05-10 09:45:51
链接
- 点此跳转
思路
差分 + 贪心。
先将两个序列做差,对作完差的序列取差分序列 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;
}
AcWing_4262相关推荐
最新文章
- 效果很好的asp.net的数据库访问模型(优化,封装一体化)
- [云炬创业基础笔记]第七章创业资源测试6
- Android APP终极瘦身指南
- CF-527E(Data Center Drama) 欧拉图+构造
- 洛谷 P1462 通往奥格瑞玛的道路 Label: 最小化最大值 spfa (存多条边示例)
- python可视化界面编程 pycharm_pycharm开发一个简单界面和通用mvc模板(操作方法图解)...
- 常用安卓开发技巧汇总
- mysql emma_ubuntu mysql emma中文乱码问题解决
- Ethernet帧结构
- java初级系统项目_JavaSE-初级阶段项目-基于excel表格的学生管理系统
- solid 铝型材框架_solidworks铝型材库
- 剑指offer系列48---左旋转字符串
- Numpy高级操作大全!!!
- load Event
- Linux解决 -bash: nc: command not found问题
- 湘源里面关于缩放的问题
- 菠萝来啦。新一代VueX 来啦 他有一个特别甜的名字“Pinia”,再不学你就out了【 Pinia/Vuex5中文文档】
- linux SFTP用户创建 不允许用户登录,并且连接只允许在制定的目录下进行操作
- 批量提取 srt 字幕文件中的文字
- excel计算结果总是为0