思路:

首先得做个转化,如果某个解法最终分别对a[i](i = 1, 2, ..., n)做了b[i](i = 1, 2, ..., n)次加1再取余的运算,那么可以等价地构造出x次(x = max(b[i]))题目定义的操作,这样就可以直接对x二分了。

实现:

 #include <bits/stdc++.h>
using namespace std;
const int N = ;
int a[N], n, m;
bool check(int x)
{
int cur = ;
for (int i = ; i <= n; i++)
{
if (a[i] + x < cur) return false;
else if (a[i] > cur)
{
if (a[i] + x < m || (a[i] + x) % m < cur)
cur = a[i];
}
}
return true;
}
int main()
{
while (cin >> n >> m)
{
for (int i = ; i <= n; i++) cin >> a[i];
int l = , r = m, ans = m;
while (l <= r)
{
int mi = l + r >> ;
if (check(mi))
{
ans = mi;
r = mi - ;
}
else l = mi + ;
}
cout << ans << endl;
}
return ;
}

CF1168A Increasing by Modulo的更多相关文章

  1. C. Increasing by Modulo

    给定n个模m的数字 可以选择k个数字进行操作,操作时对该数字进行+1模m 求解最少多少次操作可以使得该数列变成单调不下降序列 实际上就是二分操作数目,其中操作数目肯定不会超过m 然后我们将左右边界变成 ...

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

    链接:https://codeforces.com/contest/1169/problem/C 题意: Toad Zitz has an array of integers, each intege ...

  3. Codeforces 1168A Increasing by Modulo

    题目链接:http://codeforces.com/problemset/problem/1168/A 题意:给一个数组,数组中元素范围为0~n,每次你可以选择若干元素进行(ai+1)%m的操作,问 ...

  4. LightOJ 1085(树状数组+离散化+DP,线段树)

    All Possible Increasing Subsequences Time Limit:3000MS     Memory Limit:65536KB     64bit IO Format: ...

  5. RE:ゼロから始める文化課生活

    觉得有必要在NOI之前开一篇学习内容记录. 至于为什么要取这个标题呢?也许并没有什么特殊的借口吧. 5.23 在LOJ上搬了三道原题给大家考了考,然后大家都在考试就我一个人在划水. SSerxhs 和 ...

  6. [Done] Codeforces Round #562 (Div. 2) 题解

    A - Circle Metro 模拟几百步就可以了. B - Pairs 爆搜一下,时间复杂度大概是 $O(4 * n)$ Code: 56306723 C - Increasing by Modu ...

  7. [LeetCode] Increasing Triplet Subsequence 递增的三元子序列

    Given an unsorted array return whether an increasing subsequence of length 3 exists or not in the ar ...

  8. [LeetCode] Longest Increasing Path in a Matrix 矩阵中的最长递增路径

    Given an integer matrix, find the length of the longest increasing path. From each cell, you can eit ...

  9. [LeetCode] Longest Increasing Subsequence 最长递增子序列

    Given an unsorted array of integers, find the length of longest increasing subsequence. For example, ...

随机推荐

  1. [转]从onload和DOMContentLoaded谈起

    这篇文章是对这一两年内几篇dom ready文章的汇总(文章的最后会标注参考文章),因为浏览器进化的关系,可能他们现在的行为与本文所谈到的一些行为不相符.我也并没有一一去验证,所以本文仅供参考,在具体 ...

  2. BZOJ4571:[SCOI2016]美味

    浅谈主席树:https://www.cnblogs.com/AKMer/p/9956734.html 题目传送门:https://www.lydsy.com/JudgeOnline/problem.p ...

  3. POJ1860(ford判环)

    Currency Exchange Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 24243   Accepted: 881 ...

  4. python中re.sub的使用 --解读一段代码

    json='{ title: "条纹针织衫+蓬蓬裙套装", picSrc: "http://d05.res.meilishuo.net/pic/_o/15/f5/f2e2 ...

  5. 生成分布式随机ID

    经测试,最快的一种 public class Generator { // should be between 40 (34 years) and 42 (139 years) ; // should ...

  6. 在VS中对WCF服务进行更新,但是配置文件没有更新解决办法

    取消下面的勾选框

  7. QTreeWidget笔记

    1.QTreeWidget继承自QTreeView. 2.头文件:QTreeWidget 3.简单使用: #include "mainwindow.h" #include < ...

  8. 条款29:为“异常安全”而努力是值得的

    当异常被抛出时,带有异常安全性的函数: 1.不泄露任何资源 2.不允许数据败坏   异常安全函数提供以下三个保证之一: 1.基本承诺:如果异常被抛出,程序内的任何事物仍然保持在有效的状态下.没有任何对 ...

  9. 《Java多线程编程核心技术》读后感(十五)

    线程的状态 线程对象在不同的运行时期有不同的状态,状态信息就存在与State枚举类中. 验证New,Runnable,Terminated new:线程实例化后还从未执行start()方法时的状态 r ...

  10. 2014 acm鞍山现场赛总结

    好像大家都习惯打完比赛写总结,我也来水一发好了.. 记一下流水账,那么多第一次献给了acm,不记一下就白去那么远的地方了.. 首先比赛前网上买了机票跟火车票了.比赛前一天早上6点钟起来收拾东西6点半坐 ...