Online Judge | Problem Set | Authors | Online Contests | User | ||||||
---|---|---|---|---|---|---|---|---|---|---|
Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest |
50题留念,但这个题却不是自己想出来的。。大牛就是厉害,赞一个解法就是那个求中位数,也不是很懂意思,只能说写的出来,这个题有的不是最优解 但这样想就是错的,毕竟出题的人是想表达一种思维上的逻辑吧 贴代码 #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: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator