[CH0304]IncDec Sequence
2024-08-26 02:19:06
和NOIP2018DAY1T1类似的题目,但思维难度高多了。
这题既可以抬高路面,也可以降低路面,而且目标平面不确定,就难起来了。
但是两道题的基本思路几乎一样,同样我们将 2~n
的高度差分,1之所以不要差分是因为最终的高度是不确定的。
所以现在的目标就是将这个差分数组的任意一个位置 +1/-1
,另一个位置 -1/+1
,使 2~n
的差分数组变成 0
,显然最少次数就是 max(正数之和,负数绝对值之和)
。
第二问要求最终的可能平面有多少种,考虑操作的过程中当正的或负的已经被搞成 0
,只剩一部分负的或正的,那这对应原来的数组就是从 2~n
单调的,所以1的取值只能在最高点和最低点之间,这样才能保证次数最少,答案就是 abs(正数之和 - 负数绝对值之和) + 1
#include <set>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <assert.h>
#include <algorithm>
using namespace std;
#define LL long long
#define debug(...) fprintf(stderr, __VA_ARGS__)
#define GO debug("GO\n")
inline int rint() {
register int x = 0, f = 1; register char c;
while (!isdigit(c = getchar())) if (c == '-') f = -1;
while (x = (x << 1) + (x << 3) + (c ^ 48), isdigit(c = getchar()));
return x * f;
}
template<typename T> inline void chkmin(T &a, T b) { a > b ? a = b : 0; }
template<typename T> inline void chkmax(T &a, T b) { a < b ? a = b : 0; }
#define int LL
signed main() {
#ifndef ONLINE_JUDGE
freopen("xhc.in", "r", stdin);
freopen("xhc.out", "w", stdout);
#endif
int n;
cin >> n;
int x = 0, y = 0, last, now;
cin >> last;
for (int i = 2; i <= n; ++ i) {
cin >> now;
if (now > last) y += now - last;
else x += last - now;
last = now;
}
cout << max(x, y) << endl << abs(y - x) + 1 << endl;
return 0;
}
最新文章
- 求android ble 解决方案!
- 转:Log4j使用
- Microsoft.Bcl.Build 1.0.10 稳定版发布
- WCF开发指南之构建服务
- linux磁盘空间清理
- 使用scrapy爬取网站的商品数据
- IE兼容CSS3圆角border-radius的方法(同时兼容box-shadow,text-shadow)
- Javascript:getElementsByClassName
- Android_declare-styleable_自己定义控件的属性
- Blend4精选案例图解教程(二):找张图片玩特效
- [转]Data Structure Recovery using PIN and PyGraphviz
- centOS7 mini配置linux服务器(一)安装centOs7
- 老李分享:Android性能优化之内存泄漏2
- 201421123042 《Java程序设计》第5周学习总结
- ECMAscript5中的map
- PxCook 像素大厨 标注切图,一起搞定!专注设计本质
- c# 调试运行后,debug目录为空
- Unity Shader-法线贴图(Normal)及其原理
- 【DP】【CF1099C】 Postcard
- 用gcc编译成可执行程序 (转)