UPC——Contest2971 - 2021秋组队训练赛第十五场

B题【思维+单调栈】

Beautiful Mountains
有点单调栈的意思

#include <bits/stdc++.h>
#pragma GCC optimize(3, "Ofast", "inline")
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<long long, long long> pll;
const int N = 1e5 + 10;
const int M = 2e6 + 1000;
const int inf = 1e9;
const int mod = 1000000007;
const int pi = acos(-1);
#define IOS                  \
    ios::sync_with_stdio(0); \
    cin.tie(0);              \
    cout.tie(0);
ll n, len;
ll a[N], up[N], down[N];
int main()
{
    // IOS;
    scanf("%lld", &n);
    for (int i = 1; i <= n; i++)
        scanf("%lld", &a[i]);
    up[n] = down[n] = n;
    for (int i = n - 1; i >= 1; i--)
    {
        if (a[i] == -1 || a[i] <= a[i + 1] || a[i + 1] == -1)
            up[i] = up[i + 1]; //+1不是-1
        else
            up[i] = i; //莫忘了else
        if (a[i] == -1 || a[i] >= a[i + 1] || a[i + 1] == -1)
            down[i] = down[i + 1]; //+1不是-1
        else
            down[i] = i; //莫忘了else
    }
    ll len; //len定义在for外面,因为最后for外面也用到了len
    for (len = 3; len <= n; len++)
    {
        bool ok = 1;
        for (ll i = 1; i < n; i += len) //+=len,i==n情况不需考虑
        {
            ll u = min(i + len - 1, n);
            ll to = min(u - 1, up[i]); //
            if (to == i || down[to] < u)
            {
                ok = 0;
                break;
            }
        }
        if (ok)
        {
            puts("Y");
            break;
        }
    }
    if (len == n + 1)
        puts("N");
    return 0;
}

C题【思维】

Crisis at the Wedding
算是思维题吧,注意代价是累加的,用前缀和表示

// #pragma GCC optimize(3, "Ofast", "inline")
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<long long, long long> pll;
const int N = 1e5 + 10;
const int M = 2e6 + 1000;
const int inf = 1e9;
const int mod = 1000000007;
const int pi = acos(-1);
#define IOS                  \
    ios::sync_with_stdio(0); \
    cin.tie(0);              \
    cout.tie(0);
ll n, sum, res;
ll a[N], s1[N], b[N], s2[N];
int main()
{
    // IOS;
    scanf("%lld", &n);
    for (int i = 1; i <= n; i++)
    {
        scanf("%lld", &a[i]);
        b[n - i + 1] = a[i];
        sum += a[i];
    }
    ll ave = sum / n, mi = inf;
    sum = 0;
    for (int i = 1; i <= n; i++)
    {
        a[i] -= ave;
        s1[i] = s1[i - 1] + a[i]; //1开始的,i到i+1的代价
        sum += s1[i];             //sum是从1开始的总代价
        mi = min(mi, s1[i]);
    }
    res = sum - mi * n;
    // printf("%lld**%lld**%lld\n", res,sum,mi);
    mi = inf, sum = 0;
    // memset(s, 0, sizeof(s));
    for (int i = 1; i <= n; i++)
    {
        b[i] -= ave;
        s2[i] = s2[i - 1] + b[i]; //1开始的,i到i+1的代价
        sum += s2[i];             //sum是从1开始的总代价
        mi = min(mi, s2[i]);
    }
    printf("%lld\n", min(res, sum - mi * n));
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

WTcrazy _

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值