Codeforces Round #562 (Div. 2) C. Increasing by Modulo

博客围绕Codeforces上的一道题目展开,题目给定一个整数数组,可进行特定操作使数组元素加1并取模,目标是用最少操作次数让数组非递减。解题思路采用二分法,判断当前步数能否满足条件,代码转载自指定博客。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

链接:https://codeforces.com/contest/1169/problem/C

题意:

Toad Zitz has an array of integers, each integer is between 00 and m1m−1 inclusive. The integers are a1,a2,,ana1,a2,…,an.

In one operation Zitz can choose an integer kk and kk indices i1,i2,,iki1,i2,…,ik such that 1i1<i2<<ikn1≤i1<i2<…<ik≤n. He should then change aijaij to ((aij+1)modm)((aij+1)modm) for each chosen integer ijij. The integer mm is fixed for all operations and indices.

Here xmodyxmody denotes the remainder of the division of xx by yy.

Zitz wants to make his array non-decreasing with the minimum number of such operations. Find this minimum number of operations.

 思路:

二分,考虑当前步数能否满足条件即可。判断时候,如果ai+1 < ai 则判断能否使ai加到ai+1,如果不行则当前步数不行,如果ai+1 > ai。同样尽量使ai+1 = ai,如果不行并不结束。

代码:

#include <bits/stdc++.h>

using namespace std;

typedef long long LL;
const int MAXN = 3e5 + 10;
const int MOD = 1e9 + 7;
int n, m, k, t;
int a[MAXN];

bool Check(int cnt)
{
    int temp = 0;
    for (int i = 1;i <= n;i++)
    {
        if (a[i] < temp)
        {
            int ops = temp-a[i];
            if (ops > cnt)
                return false;
        }
        else if (a[i] > temp)
        {
            int ops = m-a[i]+temp;
            if (ops > cnt)
                temp = a[i];
        }
    }
    return true;
}

int main()
{
    cin >> n >> m;
    for (int i = 1;i <= n;i++)
        cin >> a[i];
    int l = 0, r = m;
    int res = m;
    while (l < r)
    {
        int mid = (l+r)/2;
        if (Check(mid))
        {
            res = min(res, mid);
            r = mid;
        }
        else
            l = mid+1;
    }
    cout << res << endl;

    return 0;
}

  

转载于:https://www.cnblogs.com/YDDDD/p/10930217.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值