POJ 2313 Sequence#贪心
找规律
//初始化为B[i]=A[i]
//然后由V=|A[1]-B[1]|+|A[2]-B[2|+|A[3]-B[3]|
// +|B[1]-B[2]|+|B[2]-B[3]|
//可以知道,如果要V尽可能小,那么对于B[2]的取值,
//则要求B[2]到A[2] B[1] B[3]的距离尽可能小,
//在数轴上可以看出,只要B[2]=中位数(A[2],B[1],B[3])即可
//即贪心规则如下:B[i]=Mid(A[i],B[i-1],B[i+1])(i=1……n-1)
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std; int a[105],b[105]; int Mid(int a,int b,int c)
{
int num[4];
num[0]=a; num[1]=b; num[2]=c;
sort(num,num+3);
return num[1];
} int main()
{
int n;
while(~scanf("%d",&n))
{
int res=0;
for(int i=0;i<n;i++)
{
scanf("%d",&a[i]);
b[i]=a[i];
}
for(int i=1;i<n-1;i++)
b[i]=Mid(a[i],b[i-1],b[i+1]);
for(int i=0;i<n;i++)
res+=abs(a[i]-b[i]);
for(int i=1;i<n;i++)
res+=abs(b[i-1]-b[i]);
printf("%d\n",res);
}
return 0;
}
POJ 2313 Sequence#贪心的更多相关文章
- 【题解】Cut the Sequence(贪心区间覆盖)
[题解]Cut the Sequence(贪心区间覆盖) POJ - 3017 题意: 给定一大堆线段,问用这些线段覆盖一个连续区间1-x的最小使用线段的数量. 题解 考虑一个这样的贪心: 先按照左端 ...
- HDU5014Number Sequence(贪心)
HDU5014Number Sequence(贪心) 题目链接 题目大意: 给出n,然后给出一个数字串,长度为n + 1, 范围在[0, n - 1].然后要求你找出另外一个序列B,满足上述的要求,而 ...
- [POJ 3581]Sequence
[POJ 3581]Sequence 标签: 后缀数组 题目链接 题意 给你一串序列\(A_i\),保证对于$ \forall i \in [2,n],都有A_1 >A_i$. 现在需要把这个序 ...
- hdu4915 Parenthese sequence 贪心O(n)解法(new)
hdu4915 Parenthese sequence Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K ...
- POJ 1456(贪心)
#include <string.h> #include <iostream> #include <queue> #include <stdio.h> ...
- POJ 2442 Sequence
Pro. 1 给定k个有序表,取其中前n小的数字.组成一个新表,求该表? 算法: 由于 a1[1] < a1[2] < a1[3] ... <a1[n] a2[1] < a2 ...
- poj -3614 Sunscreen(贪心 + 优先队列)
http://poj.org/problem?id=3614 有c头奶牛在沙滩上晒太阳,每头奶牛能忍受的阳光强度有一个最大值(max_spf) 和最小值(min_spf),奶牛有L种防晒霜,每种可以固 ...
- POJ 3614 Sunscreen 贪心
题目链接: http://poj.org/problem?id=3614 Sunscreen Time Limit: 1000MSMemory Limit: 65536K 问题描述 to avoid ...
- Poj 2478-Farey Sequence 欧拉函数,素数,线性筛
Farey Sequence Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 14291 Accepted: 5647 D ...
随机推荐
- 借助OpenOffice实现office转pdf(Java)的.exe小程序
原料:OpenOffice4.1.2(之所以选OpenOffice是因为可以跨平台,下载后直接安装),jodconverter-core-3.0-beta-4-dist.zip(可以搜博客园),Exe ...
- hdu 5996 dingyeye loves stone(博弈)
题目链接:hdu 5996 dingyeye loves stone 题意: 给你一棵树,树的每一个节点有a[i]个石子,每个人可以将这个节点的石子移向它的父亲,如果没有合法操作,那么就算输,现在给你 ...
- mysql权限设置
想要设定权限,必须通过root用户登录: >> grant all privileges on *.* to root@"%" identified by " ...
- OpenCV备忘
都是转来的内容的,算是整理一下 OpenCV备忘 深度和通道的理解 CV_8UC1 是指一个8位无符号整型单通道矩阵, CV_32FC2是指一个32位浮点型双通道矩阵 CV_8UC1 CV_8SC1 ...
- Ubuntu 14.04—无法修正错误,因为您要求某些软件包保持现状,就是它们破坏了软件包间的依赖关系 解决办法
在Ubuntu中使用sudo apt-get install安装是有时候会出现: 无法修正错误,因为您要求某些软件包保持现状,就是它们破坏了软件包间的依赖关系 解决办法 这样的错误,这是因为更新源 ...
- 通过Windows常见性能计数器分析服务器性能瓶颈
转载 http://www.cnblogs./junzhongxu/archive/2011/02/15/1954889.html 监测对象 Ø System(系统) l %Total Process ...
- HDU 5880 Family View
$AC$自动机. 用$AC$自动机匹配一次,开一个$flag$记录一下以$i$位置为结尾的最长要打$*$多少个字符,然后倒着扫描一次就可以输出了. #pragma comment(linker, &q ...
- 【angular】angular如何让传递变量参数+ng-change的使用
HTML: <div class="form-group"> <label class="col-sm-2 control-label"> ...
- Chapter 17_2 备忘录函数
一项通用的编程技术:用空间换时间. 例如有一种做法就可以提高一些函数的运行速度,记录下函数计算的结果,当再次调用该函数时,便可以复用之前的结果. 比如,一个普通服务器,在它收到请求中包含Lua代码,会 ...
- 搭建git代码服务器
在代码管理中,通常需要使用版本管理工具,git就是一个不错的选择,这里简单罗列一下git服务器的搭建过程. 1. 安装git工具包 2. 初始化git库:在代码服务器上,通常只需要创建一个不含有工作目 ...