思路:

首先得做个转化,如果某个解法最终分别对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. Mesos的quorum配置引发的问题

    Mesos安装完毕后,发现agent无法和master关联(通过WebUI的agent页面无法看到agent信息),查看日志显示: Elected as the leading master! sta ...

  2. bzoj1072Perm——状压DP

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1072 数字串最多只有10位,所以考虑用状压: 压缩的状态是哪些数字被用过,这样可以从一种状态 ...

  3. 洛谷P1144——最短路计数

    题目:https://www.luogu.org/problemnew/show/P1144 spfa跑最短路的同时记录cnt数组表示到达方案数. 代码如下: #include<iostream ...

  4. Spring boot 学习六 spring 继承 mybatis (基于注解)

    MyBatis提供了多个注解如:@InsertProvider,@UpdateProvider,@DeleteProvider和@SelectProvider,这些都是建立动态语言和让MyBatis执 ...

  5. deleteMany is not a function

    问题: 同事使用了deleteMany方法用于删除数据,但是全公司只有我一个人报错deleteMany is not a function. 很自然,输出了model.deleteMany,得到的结果 ...

  6. day1 java基础

    常见的dos命令 盘符: 进入指定的盘符下. dir : 列出当前目录下的文件以及文件夹 md : 创建目录 rd : 删除目录    注意:rd不能删除非空的文件夹,而且只能用于删除文件夹. cd ...

  7. 2015年第六届蓝桥杯国赛试题(JavaA组)

    1.结果填空 (满分15分)2.结果填空 (满分35分)3.代码填空 (满分31分)4.程序设计(满分41分)5.程序设计(满分75分)6.程序设计(满分103分) 1.标题:胡同门牌号 小明家住在一 ...

  8. 初步了解XMLHttpRequest对象、http请求的封装

    构造器 var xhr = new XMLHttpRequest() 设置超时时间 xhr.ontimeout= 设置超时时间为 1s 设置超时时间(单位:ms) 0为永不超时 HTTP 请求的状态 ...

  9. 封装了一个电商放大镜移入放大的功能,适用于VUE

    代码地址:https://github.com/zhongqiulan/jqimgzoom 由于vue只支持ie9以上版本,所以这个插件也是一样的 效果图: 第一步,在goodsinfo文件中引入cs ...

  10. 桥接设计模式(Bridge)

    Bridge??? Bridge的意思是"桥梁".就像在现实世界中,桥梁的功能是将河流的两侧连接起来一样,Bridge模式的作用也是将两样东西连接起来,它们分别是类的功能层次结构和 ...