当前位置:网站首页>[HDU] 5248 sequence transformation (greedy + dichotomy) [recommended collection]

[HDU] 5248 sequence transformation (greedy + dichotomy) [recommended collection]

2022-07-07 19:09:00 Full stack programmer webmaster

Hello everyone , I meet you again , I'm the king of the whole stack .

The length of binary enumeration can be changed

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int  INF = 3000000;
const int maxn = 100005;
int n,arr[maxn],arr2[maxn];
bool solve(int x){
    memcpy(arr2,arr,sizeof(arr));
    for(int i = 1; i <= n; i++){
        if(arr2[i] > arr2[i - 1]){
            arr2[i] = max(arr2[i - 1] + 1,arr2[i] - x);
        }
        else{
            if(arr2[i] + x <= arr2[i - 1]) return false;
            arr2[i] = arr2[i - 1] + 1;
        }
    }
    return true;
}
int main(){
    int T,Case = 1;
    scanf("%d",&T);
    arr[0] = -INF;
    while(T--){
        scanf("%d",&n);
        for(int i = 1; i <= n; i++)
            scanf("%d",&arr[i]);
        int l = 0,r = INF;
        int ans = n;
        while(l <= r){
            int mid = (l + r) >> 1;
            if(solve(mid)){
                ans = mid;
                r = mid - 1;
            }
            else
                l = mid + 1;
        }
        printf("Case #%d:\n",Case++);
        printf("%d\n",ans);
    }
    return 0;
}

Publisher : Full stack programmer stack length , Reprint please indicate the source :https://javaforall.cn/116609.html Link to the original text :https://javaforall.cn

原网站

版权声明
本文为[Full stack programmer webmaster]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/188/202207071653296941.html