HDU - 5248 序列变换

序列变换

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2270    Accepted Submission(s): 896

 

Problem Description

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

我们定义从序列A到序列B变换的代价为cost(A,B)=max(|Ai−Bi|)(1≤i≤N) 。

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

注意,每个元素在变换前后都是整数。

 

 

Input

第一行为测试的组数T(1≤T≤10) .

对于每一组:
第一行为序列A的长度N(1≤N≤105) ,第二行包含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

2015年百度之星程序设计大赛 - 初赛(1)

 

 

Recommend

hujie   |   We have carefully selected several similar problems for you:  6447 6446 6445 6444 6443 

 

 

Statistic | Submit | Discuss | Note

题解:二分 + 贪心

#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;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值