"Codeforces 626E" Simple skewness

Source: Internet
Author: User

Test instructions

Gives a set of n numbers, a subset of the maximum (the mean-median), the number of elements of the output subset, and the individual elements (in any order).

Analysis

Because it is a subset, it is not necessarily a sequential sequence. Then we have several conclusions.

1. The maximum degree of skewness must be ≥0

Because of an element, the skewness is 0.

2. The maximum skewness subset must have an odd number of elements.

Certificate

If the number of elements is even 2*k when the maximum skewness, we prove that it removes an element a[k+1] will not be worse.

The sequence of the subsets is a[i]. Remove A[k+1] The average of other numbers is AV

New average-old average =av-(av+a[k+1])/2= (av-a[k+1])/2

New median-old median =a[k]-(a[k]+a[k+1])/2= (a[k]-a[k+1])/2

And the old average-the oldest median = (Av+a[k+1])/2-(a[k]+a[k+1])/2= (Av-a[k])/2≥0 (otherwise impossible skewness maximum)

So there is a mean increment-median increment = (av-a[k])/2≥0

So the new skewness will certainly not be worse.

3. The average is incremented and then decremented.

Because it is an odd number, so the enumeration of each number to do the median, if the left and right extension length of J, then to make a greater degree of skewness, we must be left in the left side of the maximum and the number of the largest. So the remaining numbers are getting smaller, the average is growing less, and the current average is getting bigger, and the average is starting to decrease after a certain peak.

So you can use the dichotomy method to take the midpoint and a point next to the midpoint to determine whether the current mean is increasing or decreasing, increasing the right to find, reduce the left to find.

Note: floating-point numbers are not safe to determine size, so they are converted to multiplication. There is another way that I don't quite understand it.

Code
#include <cstdio> #include <algorithm> #define N 200005#define ll long longusing namespace Std;ll a[n],s[n]; int n,ansi=1,ansp;double ans;//The maximum skewness ≥0, so the initial value is only the first element with a skewness of 0. ll sum (int i,int j) {return s[n]-s[n-j]+ s[i]-s[i-j-1];}    int main () {scanf ("%d", &n);    for (int i=1; i<=n; i++) {scanf ("%i64d", &a[i]);    } sort (a+1,a+1+n);    for (int i=1; i<=n; i++) {s[i]=s[i-1]+a[i];        } for (int i=2; i<n; i++) {int l=1,r=min (i-1,n-i), m;        ll S1,s2;            while (L<r) {m=l+ (r-l)/2;            S1=sum (i,m) * (2*m+3);            S2=sum (i,m+1) * (2*m+1);            if (S1&GT;S2) {r=m;                } else {l=m+1;                if (S1==S2) {break;            }}} if (1.0*sum (i,l)/(2*l+1)-a[i]>ans) {ans=1.0*sum (i,l)/(2*l+1)-a[i];            Ansi=i;       Ansp=l; }} printf ("%d\n%i64d", Ansp*2+1,a[ansi]);    for (int i=1; i<=ansp; i++) {printf ("%i64d%i64d", a[ansi-i],a[n-i+1]); } return 0;}

"Codeforces 626E" simple skewness

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.