cf 1169 C Increasing by Modulo

Posted fanshhh

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了cf 1169 C Increasing by Modulo相关的知识,希望对你有一定的参考价值。

cf 1169 C. Increasing by Modulo

题意

给你一个n个数字的序列,有一个操作是选其中的一些数字来+1,最后使得序列每一个数取模m后是一个非严格单调递增的序列,问至少需要多少次操作?

题解

二分答案+一点点思维(代码易懂

#include <cstdio>

int a[300010], b[300010];
int main() 
    int n, m, ans = 0;
    scanf("%d %d", &n, &m);
    for(int i = 0; i < n; i++) scanf("%d", &a[i]);
    int l = 0, r = m;
    while(l < r) 
        int mid = (l + r) / 2, flag = 0;
        b[0] = a[0];
        if(a[0] + mid >= m) b[0] = 0;
        for(int i = 1; i < n; i++) 
            if(a[i] >= b[i-1]) 
                b[i] = a[i];
                if(a[i] + mid >= m && (a[i] + mid) % m >= b[i-1]) b[i] = b[i-1];
            
            else if(a[i] + mid >= b[i-1]) b[i] = b[i-1];
            else 
                flag = 1;
                break;
            
        
        if(flag) l = mid + 1;
        else ans = mid, r = mid;
    
    printf("%d\n", ans);
    return 0;

以上是关于cf 1169 C Increasing by Modulo的主要内容,如果未能解决你的问题,请参考以下文章

CF568E Longest Increasing Subsequence

Make It Increasing CF 1473 E

LeetCode --- 1636. Sort Array by Increasing Frequency 解题报告

LeetCode --- 1636. Sort Array by Increasing Frequency 解题报告

cf550C. Divisibility by Eight(结论)

contest 1169(div 2)