【枚举】CF1706 C

有人一道1400写了一个小时

Problem - C - Codeforces

题意:

思路:

首先先去观察样例:

很显然,对于n是奇数的情况,只有一种情况,直接操作偶数位就好了

主要是没搞清楚n是偶数的情况

其实有个小技巧,在模拟样例的时候特殊化成0和1会比较好看一点

Code:

cpp 复制代码
#include <bits/stdc++.h>

#define int long long

using i64 = long long;

using namespace std;

const int N = 1e5 + 10;

int h[N];

int calc(int p) {
    if (h[p] > h[p - 1] && h[p] > h[p + 1]) return 0;
    else {
        return max({h[p - 1], h[p + 1]}) - h[p] + 1;
    }
}
void solve() {
    int n;
    cin >> n;
    for (int i = 1; i <= n; i ++) {
        cin >> h[i];
    }

    int ans = 0;
    for (int i = 2; i < n; i += 2) {
        ans += calc(i);
    }

    int res = ans;
    if (n % 2 == 0) {
        for (int i = n - 2; i >= 2; i -= 2) {
            res -= calc(i);
            res += calc(i + 1);
            ans = min(ans, res);
        }
    }
    cout << ans << "\n";
}
signed main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int t = 1;
    cin >> t;
    while(t --) {
        solve();
    }
    return 0;
}
相关推荐
电饭叔15 小时前
一个构建指定坐标轴在默认点(0,0)的构造方法《python语言程序设计》2018版--第8章17题第2部分
开发语言·笔记·python
qq_2515335915 小时前
Python 查找元组中列表的数量
开发语言·python
papership15 小时前
【C++类的基本概念与定义】
开发语言·c++
计算机学姐15 小时前
基于Python的在线考试系统【2026最新】
开发语言·vue.js·后端·python·mysql·django·flask
松涛和鸣15 小时前
DAY27 Linux File IO and Standard IO Explained: From Concepts to Practice
linux·运维·服务器·c语言·嵌入式硬件·ubuntu
aini_lovee15 小时前
直接序列扩频(DSSS)通信系统MATLAB仿真指南
开发语言·matlab
天上飞的粉红小猪15 小时前
线程概念&&控制
linux·开发语言·c++
Cherry的跨界思维15 小时前
19、自动化处理双核心:Java规则引擎与Python Selenium实战全解析
java·开发语言·python·自动化·办公自动化·python办公自动化·python办公
myw07120515 小时前
湘大oj-数码积性练习笔记
c语言·数据结构·笔记·算法
了一梨15 小时前
网络编程:TCP Socket
linux·c语言·tcp/ip