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 ...
随机推荐
- JavaScript的Array.prototype.filter()详解
摘抄与:https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Array/filter 概述 ...
- MIPS指令集
MIPS 指令集(共31条) 助记符 指令格式 示例 示例含义 操作及其解释 Bit # 31..26 25..21 20..16 15..11 10..6 5..0 R-type op rs rt ...
- MATLAB符号极限、导数及级数求和
作者:长沙理工大学 交通运输工程学院 王航臣 1.函数的极限 函数:limit 功能:求取函数的极限 语法: limit(f) limit(f,x,a) limit(f,x,a,'right') li ...
- memcache学习和分析
Memcached 特点• 具有非常快的处理速度• 缺乏认证以及安全管制,应将其放置在防火墙之后• 重启后数据全部丢失• 可以给数据设置有效期• 适合使用大量低CPU的机器搭建集群• 各节点之间各自独 ...
- 网站URL重写(Java UrlRewrite 的使用)
现在大部分的网站和商城都会使用到URL重写,接触到这个,也是因为正在做的电子商务商城.URL重写,是将原有的URL采用另一种规则来显示,使得用户方便访问同时也屏蔽一些信息. 在此说下它的好处,在开发过 ...
- Jenkins slave image
Add a new shell script configure_slave.sh as following: #!/bin/bash dnf -openjdk git wget openssh-se ...
- ASP.NET网站怎么发布 Web项目程序怎么发布部署(暂时收藏)
Web程序如何发布部署呢.网站项目做好了,需要发布出来,提交给客户,装上服务器.那怎么在ASP.NET开发环境中将网站程序发布出来呢 ^_^ 工具/原料 Visual Studio 2010 ( ...
- MediaPlayer的错误列表速查(android)
public static final int MEDIA_ERROR_IO Added in API level 17 File or network related operation error ...
- nsstring遍历汉子
NSString *mytimestr=@"好人一生平安"; size_t length = [mytimestr length]; ; i < length; i++) { ...
- mysql5.5修改字符编码
因为mysql版本问题,网上各种修改编码的方法都不行,最后找到下面这条,终于解决! [mysqld]下添加: character-set-server=utf8 collation-server=ut ...