A. Vanya and Cards
time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output
题目如下
Vanya loves playing. He even has a special set of cards to play with. Each card has a single integer. The number on the card can be positive, negative and can even be equal to zero. The only limit is, the number on each card doesn’t exceed x in the absolute value.
Natasha doesn’t like when Vanya spends a long time playing, so she hid all of his cards. Vanya became sad and started looking for the cards but he only found n of them. Vanya loves the balance, so he wants the sum of all numbers on found cards equal to zero. On the other hand, he got very tired of looking for cards. Help the boy and say what is the minimum number of cards does he need to find to make the sum equal to zero?
You can assume that initially Vanya had infinitely many cards with each integer number from - x to x.
Input
The first line contains two integers: n (1 ≤ n ≤ 1000) — the number of found cards and x (1 ≤ x ≤ 1000) — the maximum absolute value of the number on a card. The second line contains n space-separated integers — the numbers on found cards. It is guaranteed that the numbers do not exceed x in their absolute value.
Output
Print a single number — the answer to the problem.
Examples
input
3 2
-1 1 2
output
1
input
2 3
-2 -2
output
2
Note
In the first sample, Vanya needs to find a single card with number -2.
In the second sample, Vanya needs to find two cards with number 2. He can’t find a single card with the required number as the numbers on the lost cards do not exceed 3 in their absolute value.
解题思路
根据本文题目所描述,需要使用 r e s res res 个绝对值不超过 k k k 的牌使得抵消原本给出的牌的总和,那么就延伸出了两种情况:
1.给出牌总和为正数
使用最少的牌数那么只需要 s u m / x sum/x sum/x(正好足够)或者 s u m / x + 1 sum/x+1 sum/x+1 张牌(用 x x x 尽可能中和 s u m sum sum,如果 s u m sum%x!=0 sum那么剩下的绝对值一定小于 x x x 所以只需要再加一张牌即可)
2.给出牌总和为负数
与上述同理,只需要让x取相反数-x(使用-x)即可
具体代码
// Problem: A. Vanya and Cards
// Contest: Codeforces - Codeforces Round 235 (Div. 2)
// URL: https://codeforces.com/problemset/problem/401/A
// Memory Limit: 256 MB
// Time Limit: 1000 ms
//
// Powered by CP Editor (https://cpeditor.org)
#include<bits/stdc++.h>
using namespace std;
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
int n,x;
cin>>n>>x;
long long cnt=0;
while(n--){
int t;
cin>>t;
cnt+=t;
}
if(cnt<0)x=-x;
if(cnt%x)cout<<cnt/x+1;
else cout<<cnt/x;
return 0;
}