【枚举】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;
}
相关推荐
QX_hao3 小时前
【Go】--反射(reflect)的使用
开发语言·后端·golang
inferno3 小时前
Maven基础(二)
java·开发语言·maven
我是李武涯3 小时前
从`std::mutex`到`std::lock_guard`与`std::unique_lock`的演进之路
开发语言·c++
yuuki2332333 小时前
【数据结构】用顺序表实现通讯录
c语言·数据结构·后端
史不了4 小时前
静态交叉编译rust程序
开发语言·后端·rust
ad钙奶长高高4 小时前
【C语言】扫雷游戏详解
c语言
读研的武5 小时前
DashGo零基础入门 纯Python的管理系统搭建
开发语言·python
Andy5 小时前
Python基础语法4
开发语言·python
但要及时清醒5 小时前
ArrayList和LinkedList
java·开发语言