题面

题意

题解

  1. 一开始想的是分情况讨论,比如当修改一个值后会影响几个波峰波谷的变化,又会增加几个新的波峰波谷,其实不用
  2. 因为只能改变一个值,所以我们直接枚举改变的值,改变后最优的值肯定是和左右端点相等的,对于枚举的每个点,我们只需要判断一下等于左端点减少的峰谷多还是等于右端点峰谷减少的多就可以了

代码

#include<bits/stdc++.h>using namespace std;
const int N = 1e6 + 10;int t, n;
int a[N];int check(int index) {if (index == 1 || index == n) return 0;if (a[index] > a[index + 1] && a[index] > a[index - 1]) return 1;if (a[index] < a[index + 1] && a[index] < a[index - 1]) return 1;return 0;
}int main() {cin >> t;while (t--) {cin >> n;for (int i = 1; i <= n; i++) cin >> a[i];if (n == 1 || n == 2) {cout << 0 << endl;} else {int res = 0;for (int i = 2; i < n; i++) {if (a[i] < a[i + 1] && a[i] < a[i - 1]) {res++;} else if (a[i] > a[i + 1] && a[i] > a[i - 1]) {res++;}}int manx = 1;for (int i = 2; i < n; i++) {int temp = a[i];int cnt = check(i - 1) + check(i) + check(i + 1);a[i] = a[i - 1];int cnt1 = check(i - 1) + check(i) + check(i + 1);a[i] = a[i + 1];int cnt2 = check(i - 1) + check(i) + check(i + 1);a[i] = temp;int change = max(cnt - cnt1, cnt - cnt2);manx = max(manx, change);}cout << max(0, res - manx) << endl;}}return 0;
}

codeoforces 1467 B Hills And Valleys (枚举)相关推荐

  1. Hills And Valleys CodeForces - 1467B

    Hills And Valleys CodeForces - 1467B 题意: 修改数列中的 一个 数字 使得峰(波峰.波谷)的数量最少 题解: 修改一个数,最多只能影响左右两个数,所能减少的峰的数 ...

  2. Hills And Valleys CodeForces - 1467B 思维

    给你一个数组,最多可以修改一个数,问最少的山峰和山谷数量之和. 小菜鸡做了一年. 一上来想猜个结论,让每个数等于其相邻的两个数,看了题解之后也证明的正确性,当时直接写就没后面这么多事了. 但是命运让我 ...

  3. Codeforces Round #835 (Div. 4) - D. Challenging Valleys

    D. Challenging Valleys 枚举所有相等的连续段,判断符合条件的是否只有一段即可. #include<bits/stdc++.h> using namespace std ...

  4. Making the Grade POJ - 3666(离散化+dp)

    题意: 给你n个山的高度,单独的一个数可以任意加减,让经过对每座山峰任意加减高度后变成递增或递减的序列时,求对每个数的相加或相减的数目的最小和. 题目: A straight dirt road co ...

  5. 洛谷 P2893 [USACO08FEB]修路Making the Grade 解题报告

    P2893 [USACO08FEB]修路Making the Grade 题目描述 A straight dirt road connects two fields on FJ's farm, but ...

  6. 河南省第十届大学生程序设计竞赛 A,B,C,D,F,G,H 题解

    H: Intelligent Parking Building 时间限制: 1 Sec  内存限制: 128 MB 提交: 22  解决: 20 [提交][状态][讨论版] 题目描述 There is ...

  7. 河南省第十届省赛 Plumbing the depth of lake (模拟)

    title: Plumbing the depth of lake 河南省第十届省赛 题目描述: There is a mysterious lake in the north of Tibet. A ...

  8. Codeforces Round #695(Div. 2)

    Codeforces Round #695 (Div. 2) 1467A Wizard of Orz 1467B Hills And Valleys 1467C Three Bags 1467D Su ...

  9. the spell of the rising moon

    原文: The spell of the rising moon There is a hill near my home that I often climb at night.The noise ...

  10. Codeforces Round #695 (Div. 2)ABCD题解详析

    A. Wizard of Orz 首先保证第一块板子是999,那么第二块板子不可能是999,那么令第二块板子为888 只要在第二块板到达888的时候按下暂停,那么第一块是999,第三块板子也是999 ...

最新文章

  1. 容器CPU隔离的底层实现机制
  2. day9 线程与进程、队列
  3. 顺序表的结构和9个基本运算算法
  4. wordpress怎么打开php网站,wordpress怎么做网站
  5. CF809C Find a car
  6. 东南大学2004年程序设计第一届初赛解题报告
  7. DM达梦创建表空间用户
  8. DWA泊车算法的实现
  9. sql出现列名无效的原因_SQL数据库中的数据类型与表结构的创建
  10. CA服务器的简单搭建
  11. 阶段5 3.微服务项目【学成在线】_day04 页面静态化_11-freemarker静态化测试-基于模板字符串静态化...
  12. java session时间_java设置session过期时间的实现方法
  13. win10下安装Jekyll(rubyinstaller-devkit)
  14. 用android编写计数器前后台源代码,在Android中实现计数器
  15. 创建 shell 脚本文件
  16. 【论文学习】基于区块链的档案数据保护和共享方法
  17. 学习笔记-磁盘存储管理
  18. 【郭东白架构课 模块二:创造价值】17|通用技能(下):架构师如何保障交付与沉淀知识?
  19. review设备管理
  20. 系统安装时出现 \EFI\BOOT\mmx64.sfi -Not Found

热门文章

  1. 经典Bug永流传---每周一“虫”(九)
  2. 小企业怎样选择服务器和操作系统
  3. DTL语言学习(一)模板变量和{% if %}标签的使用
  4. [labview]做一个简单实用可扩展功能的高速串口发送(接收)调试器
  5. 2019 年第 31 周 DApp 影响力排行榜 | TokenInsight
  6. 【ReactJs学习笔记总目录】
  7. 人事部门用计算机管理人事档案,计算机在人事管理中的运用分析
  8. visio和office安装冲突
  9. 题目推荐—POJ数学题目
  10. python atan_Python atan或atan2,我应该使用什么?