Codeforces 401A Vanya and Card【贪心】水题
来源:互联网 发布:淘宝上目前最贵的东西 编辑:程序博客网 时间:2024/06/05 16:10
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 exceedx 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 foundn 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.
The first line contains two integers: n(1 ≤ n ≤ 1000) — the number of found cards andx (1 ≤ x ≤ 1000) — the maximum absolute value of the number on a card. The second line containsn space-separated integers — the numbers on found cards. It is guaranteed that the numbers do not exceedx in their absolute value.
Print a single number — the answer to the problem.
3 2-1 1 2
1
2 3-2 -2
2
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.
题目大意:
主人公一共有0,-1,1,-2,2,-3,3................-x,x这些牌,现在手上已经有了n张牌,主人公希望将这些已经有的n张牌的和使其变成0,问至少需要加入进来几张牌。
思路:
将n张牌求和之后,如果是负数,将其变成正数,然后从x开始扫,扫到1,如果当前剩余的值大于了i,那么对应就用大小为-i的牌来补数值。
Ac代码:
#include<stdio.h>#include<string.h>using namespace std;int main(){ int n,m; while(~scanf("%d%d",&n,&m)) { int sum=0; for(int i=0;i<n;i++) { int x; scanf("%d",&x); sum+=x; } if(sum<0)sum=-sum; int output=0; for(int j=m;j>=1;j--) { if(sum>=j) { output+=sum/j; sum%=j; } } printf("%d\n",output); }}
- Codeforces 401A Vanya and Card【贪心】水题
- Codeforces 401A Vanya and Cards(水题)
- codeforces 401A - Vanya and Cards
- codeforces 401A Vanya and Cards
- CodeForces 401A Vanya and Cards
- codeforces 401A Vanya and Cards
- Codeforces 492A Vanya and Cubes【水题】
- codeforces A. Vanya and Cards
- Codeforces 492C Vanya and Exams(贪心)
- Codeforces 492B Vanya and Lanterns【贪心】
- Codeforces 492C Vanya and Exams【贪心】
- CodeForces 492A Vanya and Cubes(水题)
- Codeforces Round #355 (Div. 2) A. Vanya and Fence(水题)
- codeforces 677A Vanya and Fence(水题)
- Codeforces #364(Div.2)A.Card【贪心】【水题】
- codeforces round #235 A. Vanya and Cards
- CodeForces 258A Vanya and Cards
- Codeforces 492A. Vanya and Cubes 水
- [NOIP2001][vijos1119]Car的旅行路线(dijkstra堆优化)
- 关于HTML页面之间的传值
- Ubuntu 的apt-get install的安装错误
- 三年日记-------------记录每天的工作与思考。
- os上安装jdk1.7
- Codeforces 401A Vanya and Card【贪心】水题
- linux signals
- kinect 2.0 SDK学习笔记(一)--获得原始数据
- MYSQL数据库——DML语句
- C++ vector 的相关基本操作
- 图片大小批量处理代码(MATLAB)
- Maxima符号计算系统简介
- Mac OS上配置Maven
- onvif规范 中文介绍