L2-017 人以群分(贪心)

题目连接

https://pintia.cn/problem-sets/994805046380707840/problems/994805061056577536

思路

贪心地去想,我们如果将这些人先按照从小到大排序,再分成两部分的话,那么一定是极端地两部分,又由于两堆人数要经可能接近,于是当 n n n 为偶数的时候那么直接拆分成相同的人数的两堆即,否则我们应该让outgoing那一堆的人数多一点,便于我们提高差距

代码

#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define mod 1000000007
#define endl "\n"
#define PII pair<int,int>
#define INF 0x3f3f3f3f

int n;

int main()
{
    
    
	ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
	cin>>n;
	vector<int> V(n);
	for(int i = 0;i < n; ++i) cin>>V[i];
	sort(V.begin(),V.end());
	int l = n/2,r = n - l;
	cout<<"Outgoing #: "<<r<<endl;
	cout<<"Introverted #: "<<l<<endl;
	int ans = 0;
	for(int i = 0;i < n; ++i) {
    
    
		if(i < l) ans -= V[i];
		else ans += V[i];
	}
	cout<<"Diff = "<<ans<<endl;
	return 0;
}

猜你喜欢

转载自blog.csdn.net/m0_46201544/article/details/123963483