(~ ̄▽ ̄)~*

找规律

//初始化为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#贪心的更多相关文章

  1. 【题解】Cut the Sequence(贪心区间覆盖)

    [题解]Cut the Sequence(贪心区间覆盖) POJ - 3017 题意: 给定一大堆线段,问用这些线段覆盖一个连续区间1-x的最小使用线段的数量. 题解 考虑一个这样的贪心: 先按照左端 ...

  2. HDU5014Number Sequence(贪心)

    HDU5014Number Sequence(贪心) 题目链接 题目大意: 给出n,然后给出一个数字串,长度为n + 1, 范围在[0, n - 1].然后要求你找出另外一个序列B,满足上述的要求,而 ...

  3. [POJ 3581]Sequence

    [POJ 3581]Sequence 标签: 后缀数组 题目链接 题意 给你一串序列\(A_i\),保证对于$ \forall i \in [2,n],都有A_1 >A_i$. 现在需要把这个序 ...

  4. hdu4915 Parenthese sequence 贪心O(n)解法(new)

    hdu4915 Parenthese sequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K ...

  5. POJ 1456(贪心)

    #include <string.h> #include <iostream> #include <queue> #include <stdio.h> ...

  6. POJ 2442 Sequence

    Pro. 1 给定k个有序表,取其中前n小的数字.组成一个新表,求该表? 算法: 由于  a1[1] < a1[2] < a1[3] ... <a1[n] a2[1] < a2 ...

  7. poj -3614 Sunscreen(贪心 + 优先队列)

    http://poj.org/problem?id=3614 有c头奶牛在沙滩上晒太阳,每头奶牛能忍受的阳光强度有一个最大值(max_spf) 和最小值(min_spf),奶牛有L种防晒霜,每种可以固 ...

  8. POJ 3614 Sunscreen 贪心

    题目链接: http://poj.org/problem?id=3614 Sunscreen Time Limit: 1000MSMemory Limit: 65536K 问题描述 to avoid ...

  9. Poj 2478-Farey Sequence 欧拉函数,素数,线性筛

    Farey Sequence Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 14291   Accepted: 5647 D ...

随机推荐

  1. codevs1002搭桥(prim)

    题目描述: 这是道题题意有点迷(或者是我语文不好),但其实实际上求的就是图中连通块的个数,然后在连通块与连通块之间连边建图跑最小生成树.但是--这个图可能是不连通的--求桥的数量和总长 于是我立刻想到 ...

  2. Java中修饰符

    下面这张图应该大家都见过,根据图表来记忆最好理解 范围 private friendly(默认) protected public 当前类 √ √ √ √ 当前包中的类   √ √ √ 当前包中的类, ...

  3. getResourceAsStream和getResource的用法

    用JAVA获取文件,听似简单,但对于很多像我这样的新人来说,还是掌握颇浅,用起来感觉颇深,大家最经常用的,就是用JAVA的File类,如要取得 D:/test.txt文件,就会这样用File file ...

  4. mysql与oracle在groupby语句上的细节差异

    前言 之所以去纠那么细节的问题,是因为之前有过一个这样的场景: 有个同学,给了一条数据库的语句给我,问,为啥这样子的语句在oracle语句下执行不了. select * from xx where x ...

  5. HDU 5868 Different Circle Permutation

    公式,矩阵快速幂,欧拉函数,乘法逆元. $an{s_n} = \frac{1}{n}\sum\limits_{d|n} {\left[ {phi(\frac{n}{d})×\left( {fib(d ...

  6. 深入了解css3新特性

    深入了解css3新特性:http://www.ibm.com/developerworks/cn/web/1202_zhouxiang_css3/

  7. <T> List<T>前面<T>的意思

    先看例子: import java.util.*; class Fruit { public String toString() { return "Fruit"; } } cla ...

  8. 7.MyBatis延时加载

    1.创建javaWeb项目MyBatis_Lazy并在WebRoot下的WEB-INF下的lib下添加如下jar文件 cglib-nodep-2.1_3.jar log4j-1.2.17.jar my ...

  9. Memcache(1)

    一.缓存套路 原文地址:http://coolshell.cn/articles/17416.html Scaling Memcached at Facebook 好些人在写更新缓存数据代码时,先删除 ...

  10. mysql批量插入之提高插入效率

    INSERT INTO insert_table (datetime, uid, content, type) VALUES ('0', 'userid_0', 'content_0', 0); IN ...