"Mock test" cent candy

Source: Internet
Author: User
"Mock test" cent candy DescriptionThe kindergarten kids received a big parcel of M candy, and now they're giving it to n kids. Each child gives an expected number of sweets, and if it does not meet his expectations a[i, the child will be angry. Every bad candy, the child's angry index will increase. The degree to which he was angry was equal to the square of the number of sweets he had less. For example, Mirko wanted 32 sweets, but only 29. He has 3 fewer, so his anger index is 9. Unfortunately, the number of sweets is not enough to meet the expectations of all children. So we should take the optimal allocation method, so that the last child's angry index and the smallest.
InputLine 1th: 2 integers m,n
2nd.. N+1 Line: The first i+1 line indicates the value of the first child a[i] OutputLine 1th: An integer that resembles the smallest total angry exponent Sample Input10 44523 Sample Output4 HintData range
1≤n≤100,000,1≤m≤2*10^9,m < Sum{a[i]}
Results no more than 2^64-1
SolutionWithout looking at the data, there is a little network flow feeling ...
The requirement A[i] is decomposed into a[i]^2-(a[i]-1) ^2, a[i]-1 ^2-(a[i]-2) ^2 ... This is done with the total dissatisfaction (that is, not a single sugar) minus the maximum flow after the edge is built.
/********************************** Split Line **********************************/
In front of the method in this huge data can only hehe, according to the data we get complexity to be in O (Nlogn) around to produce results.
But the part we've been messing with is not useless. We now consider a serious question: if there is only one sugar, you give it to whom. It's obviously a[i] the biggest one (it's going to be sugar-eating), so we can reduce 2*a[i]-1 's dissatisfaction. So the higher the current expectation, the greater the degree of dissatisfaction that a candy can reduce.
Thus, we find that greed may be the final solution to this problem.
Consider the a[i] from big to small, assigning candy to the current largest a[1], making a[1] as big as a[2 [now a[1] and a[2] the maximum. Operate in the same way until m cannot be allocated, assigning m evenly to the largest a[1]~a[k].
Finally, the remainder of the a[i] squared up to get the answer. CODE
#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;
Inline long Long read () {
	char c;long long rec=0,f=1;
	while ((C=getchar ()) < ' 0 ' | | C> ' 9 ') if (c== '-') f=-1;
	while (c>= ' 0 ' &&c<= ' 9 ') rec=rec*10+c-' 0 ', C=getchar ();
	return rec*f;
}
Long long n,m;
Long long a[100005];
inline BOOL CMP (long long A,long long b) {return a>b;}
int main () {
	m=read (); N=read (
	); for (int i=1;i<=n;i++) a[i]=read ();
	Sort (a+1,a+1+n,cmp);
	for (int i=1;i<=n;i++) {
		if (m>=i* (a[i]-a[i+1)) m-=i* (a[i]-a[i+1]);
		else {for
			(long Long j=1;j<=i;j++) a[j]=a[i];
			For (long long j=1;j<=i;j++) a[j]-=m/i;m%=i;
			For (long long j=1;j<=m;j++) a[j]--;
			break;
		}
	}
	Long long ans=0;
	for (int i=1;i<=n;i++) ans+=a[i]*a[i];
	cout<<ans;
	return 0;
}
Roughly so

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.