Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

50题留念,但这个题却不是自己想出来的。。大牛就是厉害,赞一个

Posted by 1083595345 at 2015-07-27 09:46:30 on Problem 2313
解法就是那个求中位数,也不是很懂意思,只能说写的出来,这个题有的不是最优解
但这样想就是错的,毕竟出题的人是想表达一种思维上的逻辑吧

贴代码
#include <cstdio>
#include <algorithm>
#include <cmath>

using namespace std;

const int maxn=1000;

int a[maxn],b[maxn];

int mid(int a,int b,int c)
{
    int min=a<b?(b<c?a:(a<c?a:c)):(b<c?b:c),max=a<b?(b<c?c:b):(b<c?(a<c?c:a):a);
    return a+b+c-min-max;
}

int main()
{
    int n,ans=0;
    scanf("%d",&n);
    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(b[i-1],a[i],b[i+1]);
    for(int i=0;i<n;i++) ans+=abs(a[i]-b[i]);
    for(int i=1;i<n;i++) ans+=abs(b[i]-b[i-1]);
    printf("%d\n",ans);

    return 0;
}

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator