CF1169C. Increasing by Modulo(二分)

简介: CF1169C. Increasing by Modulo(二分)

linkkkkk

题意:

给出n , k和数组a,每次都可以选出若干个元素让他们的值变成k( a i + 1 ) m o d

问最少需要几次操作才能使得数组非递减

思路:

操作次数是有单调性的,比如当x次可以成功的话,x + 1次也可以成功,最后一次可以调整最大或最小元素。考虑二分答案。

对于c h e c k,贪心的考虑,如果b i = = b i − 1,跳过;如果b i > b i − 1,已经满足要求了,为了有利于后面的操作,尽可能的让b i = b i − 1,所需要的操作次数为b [ i − 1 ] + k − b [ i ];如果b i < b i − 1

相等就可以了,所需要的操作次数为b [ i − 1 ] − b [ i ],如果b [ i − 1 ] − b [ i ] < m i d

代码:

// Problem: C. Increasing by Modulo
// Contest: Codeforces - Codeforces Round #562 (Div. 2)
// URL: https://codeforces.com/problemset/problem/1169/C
// Memory Limit: 256 MB
// Time Limit: 2500 ms
// 
// Powered by CP Editor (https://cpeditor.org)
#include<bits/stdc++.h>
using namespace std;
int n,k,a[300007],b[300007];
bool check(int mid){
  for(int i=1;i<=n;i++) b[i]=a[i];
  for(int i=1;i<=n;i++){
    if(b[i]<b[i-1]){
      if(b[i-1]-b[i]>mid) return 0;
      b[i]=b[i-1];
    }
    else{
      if(b[i-1]+k-b[i]<=mid) b[i]=b[i-1];
    }
  }
  return 1;
}
int main(){
  cin>>n>>k;
  for(int i=1;i<=n;i++) cin>>a[i];
  int l=0,r=k,ans;
  while(l<=r){
    int mid=(l+r)/2;
    if(check(mid)) ans=mid,r=mid-1;
    else l=mid+1;
  }
  cout<<ans<<endl;
  return 0;
}


目录
相关文章
hdu 1019 Least Common Multiple
hdu 1019 Least Common Multiple
30 0
Light oj 1080 - Binary Simulation(树状数组区间更新点查询)
有一字符串只包含0和1,然后又m组操作,I L R是将从L到R的字符进行翻转操作0变为1、1变为0,Q x表示询问第x的字符。
41 0
codeforces 344B - Simple Molecules
题意就是给出3个原子的化学价,然后组成一个分子,要保证这个分子是稳定的,如果你还记得高中化学知识的话这个很容易理解,然后让你求出1-2 2-3 1-3 号原子之间有几条键, 这里我分别用ta tb tc 表示, 用数学的方法表示出来的话就是a = tc + tb; b = ta+tc; c = ta + tb;可能有多种情况,只要输出一种即可。
41 0
|
vr&ar
CF482B. Interesting Array(线段树)
CF482B. Interesting Array(线段树)
63 1
《Towards A Fault-Tolerant Speaker Verification System A Regularization Approach To Reduce The Condition Number》电子版地址
Towards A Fault-Tolerant Speaker Verification System: A Regularization Approach To Reduce The Condition Number
85 0
《Towards A Fault-Tolerant Speaker Verification System A Regularization Approach To Reduce The Condition Number》电子版地址
CF1454 E. Number of Simple Paths (基环树 拓扑排序)
CF1454 E. Number of Simple Paths (基环树 拓扑排序)
92 0
|
人工智能
Tree with Maximum Cost---CF1092F 树上DP
You are given a tree consisting exactly of n vertices. Tree is a connected undirected graph with n−1 edges. Each vertex v of this tree has a value av assigned to it. Let dist(x,y) be the distance between the vertices x and y.
139 0
Tree with Maximum Cost---CF1092F 树上DP
|
存储 算法
PAT (Advanced Level) Practice - 1151 LCA in a Binary Tree(30 分)
PAT (Advanced Level) Practice - 1151 LCA in a Binary Tree(30 分)
127 0
HDOJ 2028 Lowest Common Multiple Plus(n个数的最小公倍数)
HDOJ 2028 Lowest Common Multiple Plus(n个数的最小公倍数)
126 0
下一篇
无影云桌面