Hdu 5248

hdu5248- series conversion

Meaning of the questions:

To give you a sequence A, it requires changing the order of certain elements in the sequence A, form B a new number of columns, number of rows and B to ensure a strictly monotonic increasing, the minimum cost is obtained.
Cost calculation formula $ cost (a, b) = max (| A_i - B_i |) $.

solution:

Similar stone and hop that question, the answer is shrinking by half of the range, then take the minimum to every greedy.

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>

using namespace std;

#define LL long long
#define N 100010

int ans,cnt = 1;
int a[N],n,T,tmp;

inline bool check(int x) {
    int pre = a[1] - x;
    for(int i = 2 ; i <= n ; i++) {
        if(a[i] + x <= pre) return 0;
        pre = max(pre + 1, a[i] - x);
    }
    return 1;
}

int main() {
    scanf("%d",&T);
    while(T--) {
        scanf("%d",&n);
        tmp = 0;
        for(int i = 1 ; i <= n ; i++) {
            scanf("%d",&a[i]);
            if(tmp < a[i]) tmp = a[i];
        }
        int l = 0, r = 0x3f3f3f, ans;
        while(l <= r) {
            int mid = (l + r) >> 1;
            if(check(mid)) {
                r = mid - 1;
                ans = mid;
            } else l = mid + 1;
        }
        printf("Case #%d:\n%d\n",cnt++,ans);
    }
    //system("pause");
    return 0;
}

Guess you like

Origin www.cnblogs.com/Repulser/p/11401298.html