序列变换Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Problem Description 给定序列A={A1,A2,...,An} , 要求改变序列A中的某些元素,形成一个严格单调的序列B(严格单调的定义为:Bi<Bi+1,1≤i<N )。
Input 第一行为测试的组数T(1≤T≤10) .
Output 对于每一个测试样例,输出两行:
Sample Input
2 2 1 10 3 2 5 4
Sample Output
Case #1: 0 Case #2: 1
Source
Recommend hujie | We have carefully selected several similar problems for you: 6447 6446 6445 6444 6443
|
题解:二分 + 贪心
#include<set>
#include<map>
#include<list>
#include<queue>
#include<stack>
#include<time.h>
#include<math.h>
#include<vector>
#include<bitset>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<iostream>
#include<algorithm>
#define eps (1e-8)
#define MAX 0x3f3f3f3f
#define u_max 1844674407370955161
#define l_max 9223372036854775807
#define i_max 2147483647
#define re register
#define pushup() tree[rt]=max(tree[rt<<1],tree[rt<<1|1])
#define nth(k,n) nth_element(a,a+k,a+n); // 将 第K大的放在k位
#define ko() for(int i=2;i<=n;i++) s=(s+k)%i // 约瑟夫
#define ok() v.erase(unique(v.begin(),v.end()),v.end()) // 排序,离散化
using namespace std;
inline int read(){
char c = getchar(); int x = 0, f = 1;
while(c < '0' || c > '9') {if(c == '-') f = -1; c = getchar();}
while(c >= '0' & c <= '9') x = x * 10 + c - '0', c = getchar();
return x * f;
}
typedef long long ll;
const double pi = atan(1.)*4.;
const int M=5e3+5;
const int N=1e6+5;
int a[N],b[N],n;
bool fun(int mid){
for(int i=0;i<n;i++)
b[i]=a[i];
b[0]-=mid;
for(int i=1;i<n;i++){
if(b[i]>b[i-1])
b[i]=max(b[i]-mid,b[i-1]+1);
else{
if(b[i]+mid<=b[i-1]) return false;
b[i]=min(b[i]+mid,b[i-1]+1);
}
if(b[i]<=b[i-1])
return false;
}
return true;
}
int main(){
int t,pos=1;
scanf("%d",&t);
while(t--){
scanf("%d",&n);
int leap=0;
for(int i=0;i<n;i++){
scanf("%d",&a[i]);
if(i==0) continue;
else{
if(a[i]<=a[i-1])
leap=1;
}
}
if(!leap){
printf("Case #%d:\n",pos++);
printf("0\n");
continue;
}
int l=1,r=N;
while(r-1>l){
int mid=l+r>>1;
if(fun(mid)) r=mid;
else l=mid;
}
printf("Case #%d:\n",pos++);
printf("%d\n",fun(l)?l:r);
}
return 0;
}