2021年度牛客训练联盟 F.Pulling Their Weight

8 篇文章 0 订阅
7 篇文章 0 订阅

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;
}


数组的遍历+前缀和+后缀和

画了张图但现在在上课,之后修改上传。
但其实代码已经很好懂了吧?

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值