NC218905. PullingTheirWeight
描述
输入描述
Input describes a list of animals' weights. The first line contains an integer
m (), 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 ounces are too big to pull the sleigh so
no given weight will exceed this maximum.
输出描述
Output the smallest integer target weight t, as described above. It's
guaranteed that it is possible to find such an integer.
示例1
输入:
4 3 6 1 2
输出:
4
示例2
输入:
4 11 8 3 10
输出:
10
示例3
输入:
2 99 99
输出:
99
Python3(3.9) 解法, 执行用时: 186ms, 内存消耗: 7288K, 提交时间: 2021-03-07 14:21:51
n = int(input()) li = [] for i in range(n): li.append(int(input())) li.sort() l = 0 r = len(li) - 1 l_s = 0 r_s = 0 while l < r: l_s += li[l] r_s += li[r] while l_s < r_s and l < r: l += 1 l_s += li[l] l += 1 r -= 1 if li[l] == li[r]: print(li[l]) else: print(li[l-1] + 1)
C++(clang++11) 解法, 执行用时: 31ms, 内存消耗: 504K, 提交时间: 2021-03-07 21:28:07
#include<bits/stdc++.h> using namespace std; int a[100005],sum,ans,sum1; int main(){ int n; cin>>n; for(int i=1;i<=n;i++){ int x; cin>>x; a[x]++; sum+=x; } for(int i=1;i<=20000;i++){ sum1+=a[i-1]*(i-1); if(sum1*2==(sum-a[i]*i)) {cout<<i<<endl;return 0;} } }