链接:https://codeforces.com/contest/1169/problem/C

题意:

Toad Zitz has an array of integers, each integer is between 00 and m−1m−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 1≤i1<i2<…<ik≤n1≤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;
}

  

最新文章

  1. P 1080 Human Gene Functions
  2. JavaScript使用DeviceOne开发实战(五)仿ZAKER应用
  3. 17B
  4. 用触发器来实现Oracle的自增长列
  5. Artem and Array
  6. PHP 运算符 详解
  7. 为什么 as sysdba着陆方法oracle数据库,为什么刚刚输入username和password我们都可以登录?
  8. php学习之目录
  9. golang 用tar打包文件或文件夹
  10. [Luogu 1730]最小密度路径
  11. RomUtil【Android判断手机ROM,用于判断手机机型】
  12. Android BottomNavigationBar导航栏
  13. war包安装jenkins
  14. Python 进度条原理
  15. margin的垂直方向塌陷
  16. WiFi-ESP8266入门http(3-2)网页认证上网-post请求
  17. Oracle数据库联机重定义讲解及错误处理
  18. iview组件 eslint校验出错 Parsing error: x-invalid-end-tag
  19. TCP滑动窗口与回退N针协议
  20. #leetcode刷题之路24-两两交换链表中的节点

热门文章

  1. BZOJ 1634 [Usaco2007 Jan]Protecting the Flowers 护花:贪心【局部分析法】
  2. jvm file.encoding 属性引起的storm/hbase乱码
  3. python web server gateway interface (wsgi ) notes
  4. 数据库小记:根据指定名称查询数据库表名及根据指定名称查询数据库所有表中的字段名称(支持mysql/postgre)
  5. css3渐变gradient
  6. CF 504 E —— Misha and LCP on Tree —— 树剖+后缀数组
  7. 洛谷P1474货币系统——背包方案计数
  8. 关于java中equals与==的区别的小实验
  9. SynEdit(Delphi XE7)的安装和基本使用
  10. vb常用命名空间