Hdu 5248
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的更多相关文章
- hdu 5248 序列变换(二分枚举)
Problem Description 给定序列A={A1,A2,...,An}, 要求改变序列A中的某些元素,形成一个严格单调的序列B(严格单调的定义为:Bi<Bi+,≤i<N). 我们 ...
- hdu 5248 贪心
题意:
- poj和hdu部分基础算法分类及难度排序
最近想从头开始刷点基础些的题,正好有个网站有关于各大oj的题目分类(http://www.pythontip.com/acm/problemCategory),所以写了点脚本把hdu和poj的一些题目 ...
- HDOJ 2111. Saving HDU 贪心 结构体排序
Saving HDU Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total ...
- 【HDU 3037】Saving Beans Lucas定理模板
http://acm.hdu.edu.cn/showproblem.php?pid=3037 Lucas定理模板. 现在才写,noip滚粗前兆QAQ #include<cstdio> #i ...
- hdu 4859 海岸线 Bestcoder Round 1
http://acm.hdu.edu.cn/showproblem.php?pid=4859 题目大意: 在一个矩形周围都是海,这个矩形中有陆地,深海和浅海.浅海是可以填成陆地的. 求最多有多少条方格 ...
- HDU 4569 Special equations(取模)
Special equations Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u S ...
- HDU 4006The kth great number(K大数 +小顶堆)
The kth great number Time Limit:1000MS Memory Limit:65768KB 64bit IO Format:%I64d & %I64 ...
- HDU 1796How many integers can you find(容斥原理)
How many integers can you find Time Limit:5000MS Memory Limit:32768KB 64bit IO Format:%I64d ...
随机推荐
- JArray
[{ "A001033": "", ", ", ", ", ", ", ", " ...
- 怎样获取从服务器返回的xml或html文档对象
使用 xhr.responseXML; 通过这个属性正常获取XML或HTML文档对象有两个前置条件: 1. Content-Type头信息的值等于: text/xml 或 application/x ...
- Python中异常打印——面向程序猿
import logging # logging.disable(logging.CRITICAL) logging.basicConfig(filename="loggingBug.txt ...
- 3、详解 ESLint 规则 转自https://blog.csdn.net/bbsyi/article/details/88816637
什么是 ESLint ? ESLint 是在 ECMAScript/JavaScript 代码中识别和报告模式匹配的工具,它的目标是保证代码的一致性和避免错误.在许多方面,它和 JSLint.JSHi ...
- shiro 权限过滤器 -------(1)
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAABBEAAAJRCAIAAACcEbhqAAAgAElEQVR4nO3dv67sVtkHYEefhIKUIC ...
- Django rest-framework框架-序列化
序列化: 第一版: class RolesView(APIView): def get(self,request,*args,**kwargs): roles = models.Role.object ...
- oracle索引查询
/*<br>* *查看目标表中已添加的索引 * */ --在数据库中查找表名 select * from user_tables where table_name like 'table ...
- DICOM文件修改方法
/// <summary> /// 读取dicom文件 /// </summary> /// <param name="srcdirectory"&g ...
- 【Struts2】防止表单重复提交
一.概述 二.Struts2中解决方案 三.实现步骤 一.概述 regist.jsp----->RegistServlet 表单重复提交 危害: 刷票. 重复注册.带来服务器访问压力(拒绝服务) ...
- Flask+gevent-websocket模块实现websocket
后端代码: from flask import Flask,request from geventwebsocket.handler import WebSocketHandler from geve ...