anon的私货

C-anon的私货_2024牛客寒假算法基础集训营5 (nowcoder.com)

题意

思路

从头到尾充分利用每一个数即可

第一个单独讨论 最后一个不需要单独讨论,

#include<iostream>
#include<cstdio>
#include<stack>
#include<vector>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<map>
#include<set>
#include<vector>
#define int long long
#define IOS ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define  long long
#define PI acos(-1.0)
using namespace std;
typedef long long ll;
const int INF = 1e18 + 10;
const int N = 2e5 + 10;
const int M = 1e7 + 10;// 节点数量 3e6就够了是为什么?
const int mod = 1e9 + 7;
int n, m, k, x, now, ans;
int qcal(int a, int b) { int res = 1; while (b) { if (b & 1) res = res * a % mod; b >>= 1; a = a * a % mod; } return res; }
int a[N], b[N];
// void gzy()
// {
//     cin >> n;
//     for (int i = 1; i <= n; i++) cin >> a[i];
//     if (n == 1)
//     {
//         cout << a[1] - 1 << endl;
//         return;
//     }
//     int tmp = 0;
//     int idx = 0;
//     int sum = 0,now = 0;
//     sum = now = a[1] - 1;
//     for (int i = 1; i < n; i++)
//     {
//         tmp = min(a[i] - now, min(a[i], a[i + 1])) - 1;
//         now = tmp;
//         sum += tmp;
//     }
//     sum += a[n] - tmp - 1;
//     cout << sum << endl;
// }

void gzy()
{
    cin >> n;
    for(int i = 1;i <= n;i ++) cin >> a[i];
    int sum = a[1] - 1;
    int aft = 0;
    a[n+1] = INF;
    for(int i = 2;i <= n;i ++)
    {
        if(a[i] <= a[i+1])
        {
            sum += min(a[i],a[i+1]) - 1 - aft;
            aft = min(a[i],a[i+1]) - 1 - aft;
        }
        else
        {
            sum += min(a[i]-aft,a[i+1]) - 1;
            aft = min((a[i]-aft,a[i+1]) - 1,min(a[i]-aft,a[i+1]) - 1);
        }   
    }
    cout << sum << endl;
}

signed main()
{
    // int _ = 1; cin >> _;
    int _ = 1;
    while (_--) gzy();
    return 0;
}
/**
 *  ┏┓   ┏┓+ +
 * ┏┛┻━━━┛┻┓ + +
 * ┃       ┃
 * ┃   ━   ┃ ++ + + +
 *  ████━████+
 *  ◥██◤ ◥██◤ +
 * ┃   ┻   ┃
 * ┃       ┃ + +
 * ┗━┓   ┏━┛
 *   ┃   ┃ + + + +Code is far away from  
 *   ┃   ┃ + bug with the animal protecting
 *   ┃    ┗━━━┓ 神兽保佑,代码无bug 
 *   ┃  	    ┣┓
 *    ┃        ┏┛
 *     ┗┓┓┏━┳┓┏┛ + + + +
 *    ┃┫┫ ┃┫┫
 *    ┗┻┛ ┗┻┛+ + + +
 */

直接默认a[n+1] = INF就行

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值