首页 > 其他 > 详细

C. Increasing by Modulo

时间:2019-05-27 14:12:44      阅读:152      评论:0      收藏:0      [点我收藏+]

链接: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;
}

  

C. Increasing by Modulo

原文:https://www.cnblogs.com/YDDDD/p/10930217.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!