uva11300--Algorithm Competition Introductory classic Training Guide

Source: Internet
Author: User
Tags cmath

(XL) Lt1-3

This problem is still a test of mathematical level, since all exchanges are limited to the exchange between two adjacent, so it is easy to n-1 a group of equations, note that this is a ring, so the nth equation can be deduced so that it cannot be counted as an effective equation group, even if all the values cannot be solved, but we note that As long as the minimum value is obtained, then the problem of finding the extremum can be expressed by x1 A variable, that is, the elimination of the element is completed. This translates to the "single-variable extremum problem".

And then there is the obvious coordinate to beg ... And then the obvious median number is the solution.

However, when programming is disgusting, I always thought my formula may be wrong, but the problem is tot did not initialize, disgusting ah disgusting ah ... So be sure to initialize must initialize!!!

Attached code:

#include <iostream>#include<cstdio>#include<algorithm>//#include <cmath>using namespacestd;Const intmaxn=1000000+ -;Long LongA[MAXN],C[MAXN];intMain () {intN;  while(SCANF ("%d", &n) = =1){        Long Longtot=0;//Initialize, otherwise a fixed random value is generated ... Hey, Daddy.         for(intI=1; i<=n;i++) {scanf ("%lld",&A[i]); Tot+=A[i]; }        Long Longave=tot/N; c[0]=0;  for(intI=1; i<n;i++) {C[i]=c[i-1]+ave-a[i+1];//cout<< "C" <<i<< ":" <<c[i]<<endl;} sort (C,c+N); Long Longmid=c[n/2]; Long Longans=0;  for(intI=0; i<n;i++) Ans+=abs (mid-C[i]); printf ("%lld\n", ans); }    return 0;}

I found such a problem when I was reading a book. What is the difference between fabs and abs? What library do you need? It seems that ABS does not require additional libraries.

Libraries are different, but in fact, as long as the loading of Cmath library, they can be used, they are the difference between fabs is floating point type, ABS is integral type (note, take the absolute value after rounding).

uva11300--Algorithm Competition Introductory classic Training Guide

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.