和NOIP2018DAY1T1类似的题目,但思维难度高多了。

这题既可以抬高路面,也可以降低路面,而且目标平面不确定,就难起来了。

但是两道题的基本思路几乎一样,同样我们将 2~n 的高度差分,1之所以不要差分是因为最终的高度是不确定的。

所以现在的目标就是将这个差分数组的任意一个位置 +1/-1 ,另一个位置 -1/+1,使 2~n 的差分数组变成 0,显然最少次数就是 max(正数之和,负数绝对值之和)

第二问要求最终的可能平面有多少种,考虑操作的过程中当正的或负的已经被搞成 0 ,只剩一部分负的或正的,那这对应原来的数组就是从 2~n 单调的,所以1的取值只能在最高点和最低点之间,这样才能保证次数最少,答案就是 abs(正数之和 - 负数绝对值之和) + 1

  1. #include <set>
  2. #include <cmath>
  3. #include <cstdio>
  4. #include <cstring>
  5. #include <iostream>
  6. #include <assert.h>
  7. #include <algorithm>
  8. using namespace std;
  9. #define LL long long
  10. #define debug(...) fprintf(stderr, __VA_ARGS__)
  11. #define GO debug("GO\n")
  12. inline int rint() {
  13. register int x = 0, f = 1; register char c;
  14. while (!isdigit(c = getchar())) if (c == '-') f = -1;
  15. while (x = (x << 1) + (x << 3) + (c ^ 48), isdigit(c = getchar()));
  16. return x * f;
  17. }
  18. template<typename T> inline void chkmin(T &a, T b) { a > b ? a = b : 0; }
  19. template<typename T> inline void chkmax(T &a, T b) { a < b ? a = b : 0; }
  20. #define int LL
  21. signed main() {
  22. #ifndef ONLINE_JUDGE
  23. freopen("xhc.in", "r", stdin);
  24. freopen("xhc.out", "w", stdout);
  25. #endif
  26. int n;
  27. cin >> n;
  28. int x = 0, y = 0, last, now;
  29. cin >> last;
  30. for (int i = 2; i <= n; ++ i) {
  31. cin >> now;
  32. if (now > last) y += now - last;
  33. else x += last - now;
  34. last = now;
  35. }
  36. cout << max(x, y) << endl << abs(y - x) + 1 << endl;
  37. return 0;
  38. }

[CH0304]IncDec Sequence的更多相关文章

  1. CH-0304 IncDec Sequence

    0304 IncDec Sequence 0x00「基本算法」例题 描述 给定一个长度为 n(n≤10^5 ) 的数列 {a_1,a_2,…,a_n},每次可以选择一个区间 [l,r],使下标在这个区 ...

  2. bzoj 3043: IncDec Sequence 模拟

    3043: IncDec Sequence Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 248  Solved: 139[Submit][Statu ...

  3. Poetize6: IncDec Sequence

    3043: IncDec Sequence Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 233  Solved: 132[Submit][Statu ...

  4. 前缀和与差分之IncDec sequence

    参考链接:https://blog.csdn.net/hzk_cpp/article/details/80407014 题目链接:https://www.acwing.com/problem/cont ...

  5. 【BZOJ 3043】 3043: IncDec Sequence (差分)

    3043: IncDec Sequence Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 589  Solved: 332 Description 给 ...

  6. 【BZOJ3043】IncDec Sequence 乱搞

    [BZOJ3043]IncDec Sequence Description 给定一个长度为n的数列{a1,a2...an},每次可以选择一个区间[l,r],使这个区间内的数都加一或者都减一.问至少需要 ...

  7. BZOJ 3043: IncDec Sequence

    3043: IncDec Sequence Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 578  Solved: 325[Submit][Statu ...

  8. 2498 IncDec Sequence

    2498 IncDec Sequence  时间限制: 1 s  空间限制: 64000 KB  题目等级 : 钻石 Diamond 题解  查看运行结果     题目描述 Description 给 ...

  9. 差分:IncDec Sequence 差分数组

    突然就提到了这个东西,为了不再出现和去年联赛看见二分没学二分痛拿二等第一的情况,就去学了一下,基础还是比较简单的-- 先看一个经典例题: 给定一个长度为n的数列{a1,a2...an},每次可以选择一 ...

随机推荐

  1. careercup-扩展性和存储限制10.3

    题目 给你一个文件,里面包含40亿个整数,写一个算法找出该文件中不包含的一个整数, 假设你有1GB内存可用. 如果你只有10MB的内存呢? 解答 我们先来做个算术题,40亿个整数大概有多大? * ^ ...

  2. 如何在github上实现预览

    这个问题在网络上有很多答案,但是真正能解决的寥寥无几!接下来我就来尝试一下网络上疯传的几种方法.准备好了吗?我要开车了!!! PS:以下实验上传到github的demo采取导入本地css,js和网络上 ...

  3. Swift_闭包

    Swift_闭包 点击查看源码 闭包优化 //闭包优化 func testClosures() { //函数做参数 排序 let names = ["XuBaoAiChiYu", ...

  4. ABAP术语-Technical Object

    Technical Object 原文:http://www.cnblogs.com/qiangsheng/archive/2008/03/18/1111205.html Generic term f ...

  5. 在JSP中使用formatNumber控制要显示的小数位数

    先引入标签库 <%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %> 比 ...

  6. CYQ MAction,子表 新增,删除,修改 集合

    using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threa ...

  7. IDEA中使用单元测试@Test等,提示没有 Junit.jar包

    1.File-->Project Structure-->Modules-->右侧Dependencies-->+号-->JARs or directories... 2 ...

  8. Git 学习笔记–基本操作

    Git 与 SVN 不同,是分布式的版本控制系统,不需要主服务器即可工作,实际中为了方便各个工作者间同步工作,通常还是会设置主服务器. Git的设置及初始化: 设置全局用户信息: luojiahu@u ...

  9. Cab 安装不成功问题

    使用 iexpress.exe 成功打包了cab文件. 可下面问题来了,用静态的html调用,提示安装. 确认安装之后,却提示找不到相应的*.ocx,导致无法安装文件到系统 分析具体原因:*.ocx ...

  10. LCD触屏驱动

    tiny4412多点触摸屏驱动程序(基于I2C协议): #include <linux/kernel.h> #include <linux/module.h> #include ...