F.Pulling Their Weight
题面
To save money, Santa Claus has started hiring other animals besides reindeer to
pull his sleigh via short term ‘gig’ contracts. As a result, the actual
animals that show up to pull his sleigh for any given trip can vary greatly in
size.
Last week he had 2 buffalo, 37 voles and a schnauzer. Unfortunately, both
buffalo were hitched on the left side and the entire sleigh flipped over in
mid-flight due to the weight imbalance.
To prevent such accidents in the future, Santa needs to divide the animals for
a given trip into two groups such that the sum of the weights of all animals in
one group equals the sum of the weights of all animals in the other. To make
the hitching process efficient, Santa is seeking an integer target weight t
such that all animals that are lighter than t go in one group and those
heavier than t go into the other. If there are multiple such t, he wants
the smallest one. There’s one small wrinkle: what should be done if there some
animals have weight exactly equal to t? Santa solves the problem this way: if
there are an even number of such animals, he divides them equally among the two
groups (thus distributing the weight evenly). But if there are an odd number of
such animals, then one of those animals is sent to work with the elves to make
toys (it is not put in either group), and the remaining (now an even number)
are divided evenly among the two groups.
输入描述:
Input describes a list of animals’ weights. The first line contains an integer
m (2 \le m \le 10^52≤m≤10
5
), indicating the number of animals. The next m lines
each have a positive integer. These are the weights of the animals (in ounces).
Animals weighing more than 20,00020000 ounces are too big to pull the sleigh so
no given weight will exceed this maximum.
代码:
#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <cmath>
#include <map>
#include <vector>
#include <algorithm>
#include <set>
typedef long long ll;
using namespace std;
ll data[100005], head[100005], tail[100005], m;
//head存前缀和,tail存后缀和
ll getHead(ll index) {
if (index < 0)return 0;
else return head[index];
}
ll getTail(ll index) {
if (index >= m)return 0;
else return tail[index];
}
int main() {
ll i, tmin, tmax, j, t = 0, tnext;
cin>>m;
for (i = 0; i < m; i++) {
cin>> data[i];
}
//输入
sort(data, data + m);
//排序处理
for (i = 0; i < m; i++) {
if (i == 0)
head[i] = data[i];
else
head[i] = data[i] + head[i - 1];
}
//前缀和
for (i = m - 1; i >= 0; i--) {
if (i == m - 1)
tail[i] = data[i];
else
tail[i] = data[i] + tail[i + 1];
}
//后缀和
for (i = 0; i < m; i++) {
tmin = i;
for (j = i + 1; j < m; j++) {
if (data[j] != data[i])break;
}
tmax = j - 1;
if (getHead(tmin - 1) == getTail(tmax + 1)) {
t = data[tmin];
break;
}
if (tmax != m - 1) {
tnext = data[tmax + 1];
if (tnext > data[tmax] + 1) {
if (getHead(tmax) == getTail(tmax + 1)) {
t = data[tmax] + 1;
break;
}
}
}
i = tmax;
}
cout<<t<<endl;
return 0;
}
数组的遍历+前缀和+后缀和
画了张图但现在在上课,之后修改上传。
但其实代码已经很好懂了吧?