思路:

首先得做个转化,如果某个解法最终分别对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. bootstrap模版兼容IE浏览器代码嵌入

    1.  bootstrap模板为使IE6.7.8版本(IE9以下版本)浏览器兼容html5新增的标签,引入下面代码文件即可. <script src="https://oss.maxc ...

  2. Day06:迭代器,生成器,生成表达式,面向过程编程,包及常用模块

    今日内容:1.迭代器(****)2.生成器(***)3.生成器表达式(*****)4.面向过程编程(*****)5.包的使用(***)6.常用模块    logging (*****)    re ( ...

  3. string行读入&&文件输入

    普通读入的时候会以空格作为分隔符 直接用cin>>s读入,此时可以直接处理文件尾的情况 text代码: #include <iostream>#include <cstd ...

  4. php封装pdo操作数据的工具类

    <?php header("Content-Type:text/html;charset=utf-8"); class PdoMysql{ public static $co ...

  5. WPF后台修改内容界面不显示问题

    通知3部曲:1.Model继承并实现 INotifyPropertyChanged 接口:2.数据集合使用ObservableCollection<T>集合:3.View使用Binding ...

  6. [51nod] 1432 独木桥 贪心

    n个人,已知每个人体重.独木舟承重固定,每只独木舟最多坐两个人,可以坐一个人或者两个人.显然要求总重量不超过独木舟承重,假设每个人体重也不超过独木舟承重,问最少需要几只独木舟? Input 第一行包含 ...

  7. C# File和fileinfo类

    两个类功能差不多,File是静态方法实现的,Fileinfo通过实例方法实现的: 文件操作例子: using System; using System.Collections.Generic; usi ...

  8. Educational Codeforces Round 65 (Rated for Div. 2) B. Lost Numbers

    链接:https://codeforces.com/contest/1167/problem/B 题意: This is an interactive problem. Remember to flu ...

  9. 牛客小白月赛13 G(双向搜索)

    AC通道 两边同步搜,一步里面A走一次B走两次,遇到对方走过的地方就得到了答案. #include <bits/stdc++.h> using namespace std; const i ...

  10. 一篇关于完全动态凸包的paper(侵删)

    先放原文,挖个坑,到时候再来说人话ε=(´ο`*))) 作者:Franco P. Preparata 出处:Computational geometry An introduction The tec ...