zoukankan      html  css  js  c++  java
  • HDU5248——二分查找——序列变换

    Problem Description
    给定序列A={A1,A2,...,An}, 要求改变序列A中的某些元素,形成一个严格单调的序列B(严格单调的定义为:Bi<Bi+1,1i<N)。

    我们定义从序列A到序列B变换的代价为cost(A,B)=max(|AiBi|)(1iN)

    请求出满足条件的最小代价。

    注意,每个元素在变换前后都是整数。
     
    Input
    第一行为测试的组数T(1T10).

    对于每一组:
    第一行为序列A的长度N(1N105),第二行包含N个数,A1,A2,...,An.
    序列A中的每个元素的值是正整数且不超过106
     
    Output
    对于每一个测试样例,输出两行:

    第一行输出:"Case #i:"。i代表第 i 组测试数据。

    第二行输出一个正整数,代表满足条件的最小代价。
     
    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:  5257 5256 5255 5254 5253 
    对于给定的范围,我们可以二分查找x看是否a序列加减x后能否满足递增
    有三种情况
    1.如果a[i]+x < temp 即在这个范围里面是肯定可以的,temp就更新成下面需要判断的序列对
    2.如果a[i] - x >= temp a[i]即使小到最小仍旧比后面一个数大,说明不可行
    3.其他情况为了保证严格递增 temp--
    二分左闭右开
    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    #include<cmath>
    using namespace std;
    int n;
    int a[1000005];
    bool judge(int  x){
        int  temp = a[n] + x;
        for(int i = n - 1; i >= 1; i--){
            if(a[i] + x  < temp){
                temp = a[i] + x;
            }
            else if(a[i] - x >= temp)
                return false;
            else temp--;
        }
        return true;
    }
    int main()
    {
        int t;
        scanf("%d",&t);
        for(int cas = 1; cas <= t; cas++){
            scanf("%d",&n);
            for(int i = 1; i <= n ;i ++)
                scanf("%d",&a[i]);
        int l = 0,r = 1000000;
            while(l < r){
                int  mid = (l + r ) >> 1;
                if(judge(mid)){
                    r = mid;
                }
                else l = mid + 1;
            }
            printf("Case #%d:
    %d
    ",cas,l);
        }
        return 0;
    }
    

      

  • 相关阅读:
    模式识别 第一章 概论
    高等代数9 欧几里得空间
    离散数学6 初等数论
    高等代数6 线性空间
    高等代数5 二次型
    GMOJ 6870. 【2020.11.17提高组模拟】ckw的树 (tree)
    1
    Virtual Tree 学习笔记
    [2020.11.14提高组模拟] 鬼渊传说(village)
    企业购置新车,各项费用会计入账以及案例分析
  • 原文地址:https://www.cnblogs.com/zero-begin/p/4547729.html
Copyright © 2011-2022 走看看