hdu 5248 序列变换

中文题意,略(但弱鸡的我还是把题目读错了,一直到读题解的时候才发现,弱鸡总会读错题目,我以为跑一下lis就行了,太天真)

但现在还是没有想到为什么会用二分(虽然大家都是这么说的,而且这是二分专题,emmm)

#include <set>
#include <map>
#include <queue>
#include <stack>
#include <math.h>
#include <vector>
#include <string>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <iostream>
#include <algorithm>
#define zero(a) fabs(a)<eps
#define max( x, y )  ( ((x) > (y)) ? (x) : (y) )
#define min( x, y )  ( ((x) < (y)) ? (x) : (y) )
#define lowbit(x) (x&(-x))
#define debug(a) cerr<<#a<<"=="<<a<<endl
typedef long long ll;
const double pi=acos(-1.0);
const double eps=1e-8;
const int inf=0x3f3f3f3f;
const ll linf=0x3f3f3f3f3f3f3f3f;
const int maxn = 1e5;
using namespace std;

int a[maxn],b[maxn],n;

int check(int x)
{
//    bool ok=true;
    b[0]=a[0]-x;
    for(int i=1;i<n;i++){
        if(a[i]>b[i-1]){
            b[i]=max(b[i-1]+1,a[i]-x);
        }
        else{
            b[i]=min(b[i-1]+1,a[i]+x);
        }
        if(b[i]<=b[i-1])return false;
    }
    return true;
}

int main()
{
    int t;
    scanf("%d",&t);
    int cas=1;
    while(t--)
    {
        scanf("%d",&n);
        memset(a,0,sizeof(a));
        for(int i=0;i<n;i++){
            scanf("%d",&a[i]);
        }

        int l=0,r=inf;
        int mid,as;
        while(l<=r){
            mid=(l+r)>>1;
//            printf("test
");
            if(check(mid)){
                r=mid-1;
                as=mid;
            }
            else{
                l=mid+1;
            }
        }
        printf("Case #%d:
",cas++);
        printf("%d
",as);
    }
    return 0;
}
/*
2
2
1 10
3
2 5 4
*/
原文地址:https://www.cnblogs.com/lalalatianlalu/p/7819987.html