hdu5248-序列变换

题意:

给你一个序列A,要求改变序列A中的某些元素的顺序,形成一个新的数列B,并保证数列B严格单调递增,求出最小代价。

代价计算公式 $ cost(a,b)=max(|A_i - B_i|) $ 。

解法:

和跳石头那道题类似,通过二分答案不断缩小范围,再每次贪心的取最小值即可。

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

Hdu 5248的更多相关文章

  1. hdu 5248 序列变换(二分枚举)

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

  2. hdu 5248 贪心

    题意:

  3. poj和hdu部分基础算法分类及难度排序

    最近想从头开始刷点基础些的题,正好有个网站有关于各大oj的题目分类(http://www.pythontip.com/acm/problemCategory),所以写了点脚本把hdu和poj的一些题目 ...

  4. HDOJ 2111. Saving HDU 贪心 结构体排序

    Saving HDU Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total ...

  5. 【HDU 3037】Saving Beans Lucas定理模板

    http://acm.hdu.edu.cn/showproblem.php?pid=3037 Lucas定理模板. 现在才写,noip滚粗前兆QAQ #include<cstdio> #i ...

  6. hdu 4859 海岸线 Bestcoder Round 1

    http://acm.hdu.edu.cn/showproblem.php?pid=4859 题目大意: 在一个矩形周围都是海,这个矩形中有陆地,深海和浅海.浅海是可以填成陆地的. 求最多有多少条方格 ...

  7. HDU 4569 Special equations(取模)

    Special equations Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u S ...

  8. HDU 4006The kth great number(K大数 +小顶堆)

    The kth great number Time Limit:1000MS     Memory Limit:65768KB     64bit IO Format:%I64d & %I64 ...

  9. HDU 1796How many integers can you find(容斥原理)

    How many integers can you find Time Limit:5000MS     Memory Limit:32768KB     64bit IO Format:%I64d ...

随机推荐

  1. System.Data.EntityException: The underlying provider failed on Open.

    场景:IIS默认站点建立程序,使用Windows集成身份验证方式,连接SQLServer数据库也是采用集成身份验证.我报“System.Data.EntityException: The underl ...

  2. 【ES6 】ES6 解构赋值--函数参数解构赋值

    函数的参数也可以使用解构赋值. function add([x, y]){ return x + y; } add([1, 2]); 上面代码中,函数add的参数表面上是一个数组,但在传入参数的那一刻 ...

  3. SSE指令集加速之 I420转BGR24

    void yuv420_to_rgb24_sse3(uint8_t *yp, uint8_t *up, uint8_t *vp, int sy, int suv, int width, int hei ...

  4. Linux常用命令(自用)

    1 抓包 tcpdump port 5060 and host 192.168.1.180 tcpdump -i ethx -w 1.pcap -s 0 2. 查看硬盘使用情况 df ./ 3.查看进 ...

  5. luogu P3773 [CTSC2017]吉夫特

    luogu 这里的组合数显然要用\(\text{lucas}\)定理来求,所以考虑\(\text{lucas}\)定理的本质,即把\(n,m\)分别拆分成\(p\)进制串\(\{a\}\{b\}\), ...

  6. JavaScript Basics_Fundamentals Part 2_A simple calendar

    下方的日历框架是从 Active learning: A simple calendar 上整过来的. 主要任务是用 if...else 语句来让日历本显示出每月相对应的天数,相关代码已经给出,我们只 ...

  7. linux后台启动项目命令

    在用xshell启动一个项目后,关闭了xshell后,项目又停止了 nohup python admin.py runserver & nohup ........  &   中间包含 ...

  8. Java虚拟机(JVM)知多少

    本文大量参考:https://www.cnblogs.com/lfs2640666960/p/9297176.html 概述 JVM是JRE的一部分.它是一个虚构出来的计算机,是通过在实际的计算机上仿 ...

  9. Delphi 类成员的可见性

  10. 三:MVC之Lambda表达式

    Lambda表达式 Lambda表达式是一个匿名方法,即没有方法名的方法. C#中的Lambda表达式使用Lambda运算符“=>”,该运算符读为“goes to”. 语法: 形参列表=> ...