pta people are divided into groups (25 points)

7-5 people divided into groups (25 points)

In social networks, we define an "activity" for everyone. Now we hope to divide the population into two categories based on this indicator, namely outgoing (highly active) and introverted (low active) of). It is required that the size of the two groups of people is as close as possible, and the gap between their total activity is as wide as possible.

Input format:

Enter the first line to give a positive integer N (2≤N≤10​5​​). The next line gives N positive integers, which are the activity of each person, separated by spaces. The title guarantees that these numbers and their sum will not exceed 2​31​​.

Output format:

Output in the following format:

Outgoing #: N1
Introverted #: N2
Diff = N3

 

Among them N1is the number of extroverts; N2is the number of introverts; N3is the absolute value of the difference between the total activity of the two groups.

Input example 1:

10
23 8 10 99 46 2333 46 1 666 555

 

Output sample 1:

Outgoing #: 5
Introverted #: 5
Diff = 3611

 

Input example 2:

13
110 79 218 69 3721 100 29 135 2 6 13 5188 85

 

Output sample 2:

Outgoing #: 7
Introverted #: 6
Diff = 9359

 

Examination questions: The scale is required to be as close as possible --->even number, half of the outward inward; odd number, one more outward than inward (the active value should be as large as possible); 

#include<bits/stdc++.h>
using namespace std;

int main()
{
	int n;
	cin>>n;
	int a[100100];
	for(int i=1;i<=n;++i)
	{
		cin>>a[i];
	}
	sort(a+1,a+n+1);
	int sum,m,sum1=0,sum2=0;
     m=n/2;//‘/’直接向下取整 
	
	for(int i=1;i<=m;++i)
	{
		sum1+=a[i];
	}//前m项和 
	for(int i=m+1;i<=n;++i)
	{
		sum2+=a[i];
	}//剩下项之和 
	sum=sum2-sum1;//求差 
	

	
		printf("Outgoing #: %d\n",n-m);
		printf("Introverted #: %d\n",m);
		printf("Diff = %d\n",abs(sum));//输出活跃程度绝差的对值 
	
	

	return 0;
	
 } 

 

Guess you like

Origin blog.csdn.net/weixin_43863618/article/details/105641962