【枚举】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;
}
相关推荐
yangSnowy5 小时前
PHP变量回收机制
开发语言·php
C+-C资深大佬6 小时前
C++类型判断
开发语言·c++
2501_944521596 小时前
Flutter for OpenHarmony 微动漫App实战:推荐动漫实现
android·开发语言·前端·javascript·flutter·ecmascript
不绝1916 小时前
C#进阶:委托
开发语言·c#
喜欢喝果茶.6 小时前
跨.cs 文件传值(C#)
开发语言·c#
zmzb01036 小时前
C++课后习题训练记录Day74
开发语言·c++
小冷coding6 小时前
【Java】Dubbo 与 OpenFeign 的核心区别
java·开发语言·dubbo
Coder_Boy_6 小时前
基于SpringAI的在线考试系统-智能考试系统-学习分析模块
java·开发语言·数据库·spring boot·ddd·tdd
2401_894828127 小时前
从原理到实战:随机森林算法全解析(附 Python 完整代码)
开发语言·python·算法·随机森林
玄同7657 小时前
Python「焚诀」:吞噬所有语法糖的终极修炼手册
开发语言·数据库·人工智能·python·postgresql·自然语言处理·nlp