社交网络中我们给每个人定义了一个“活跃度”,现希望根据这个指标把人群分为两大类,即外向型(outgoing,即活跃度高的)和内向型(introverted,即活跃度低的)。要求两类人群的规模尽可能接近,而他们的总活跃度差距尽可能拉开。
输入格式:
输入第一行给出一个正整数N(2≤N≤105)。随后一行给出N个正整数,分别是每个人的活跃度,其间以空格分隔。题目保证这些数字以及它们的和都不会超过231。
输出格式:
按下列格式输出:
Outgoing #: N1
Introverted #: N2
Diff = N3
其中N1
是外向型人的个数;N2
是内向型人的个数;N3
是两群人总活跃度之差的绝对值。
输入样例1:
10
23 8 10 99 46 2333 46 1 666 555
输出样例1:
Outgoing #: 5
Introverted #: 5
Diff = 3611
输入样例2:
13
110 79 218 69 3721 100 29 135 2 6 13 5188 85
输出样例2:
Outgoing #: 7
Introverted #: 6
Diff = 9359
#include<iostream>
#include<vector>
#include<algorithm>
#include<cmath>
#include<numeric>
using namespace std;
int a[100006];
int main() {
int n,i=0;
cin >> n;
vector <int> v,v1,v2;
while (n--) {
cin >> a[i];
v.push_back(a[i]);
i++;
}
int renshu = v.size();
sort(v.begin(), v.end());
int cnt_outgoing = ceil(i / 2.0);
int cnt_in = renshu - cnt_outgoing;
vector <int>::iterator it;
int cnt = 1;
for (it = v.begin();it != v.end();it++,cnt++) {
if (cnt <= cnt_in) v1.push_back(*it);
else v2.push_back(*it);
}
int sum1 = accumulate(v1.begin(), v1.end(),0);
int sum2 = accumulate(v2.begin(), v2.end(),0);
cout <<"Outgoing #: " << cnt_outgoing << endl <<"Introverted #: " << cnt_in << endl;
cout <<"Diff = " << sum2 - sum1;
return 0;
}
根据题意的这句话:要求两类人群的规模尽可能接近,而他们的总活跃度差距尽可能拉开。
分析两组输出案例后,发现N1就是ceil(人数/2.0)
12-18更 不需要vector ceil没必要
accumulate 第二参数是累加范围中最后一个元素的下一个位置的迭代器
#include<bits/stdc++.h>
using namespace std;
const int N = 100005;
int t[N],n;
#define x first
#define y second
typedef pair<int, int> PII;
int main()
{
cin >> n;
int a = (n + 1) / 2;
int b = n - a;
cout << "Outgoing #: " << a << endl << "Introverted #: " << b << endl;
for (int i = 0;i < n;i++)
cin >> t[i];
sort(t, t + n);
int sum1 = accumulate(t, t + n - a , 0);
sort(t, t + n, greater<int>());
int sum2 = accumulate(t, t + (n + 1) / 2 , 0);
cout << "Diff = " << sum2 - sum1;
system("pause");
}