【枚举】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;
}
相关推荐
侃侃_天下3 天前
最终的信号类
开发语言·c++·算法
echoarts3 天前
Rayon Rust中的数据并行库入门教程
开发语言·其他·算法·rust
Aomnitrix3 天前
知识管理新范式——cpolar+Wiki.js打造企业级分布式知识库
开发语言·javascript·分布式
每天回答3个问题3 天前
UE5C++编译遇到MSB3073
开发语言·c++·ue5
伍哥的传说3 天前
Vite Plugin PWA – 零配置构建现代渐进式Web应用
开发语言·前端·javascript·web app·pwa·service worker·workbox
小莞尔3 天前
【51单片机】【protues仿真】基于51单片机的篮球计时计分器系统
c语言·stm32·单片机·嵌入式硬件·51单片机
小莞尔3 天前
【51单片机】【protues仿真】 基于51单片机八路抢答器系统
c语言·开发语言·单片机·嵌入式硬件·51单片机
liujing102329293 天前
Day03_刷题niuke20250915
c语言
我是菜鸟0713号3 天前
Qt 中 OPC UA 通讯实战
开发语言·qt
JCBP_3 天前
QT(4)
开发语言·汇编·c++·qt·算法