NYOJ--1105--sweets

网友投稿 484 2022-11-12

NYOJ--1105--sweets

NYOJ--1105--sweets

LANGyang系列 ~~             sweets

链接:​​click here​​

1000 ms  |  内存限制: 65535

1

buys N packets of sweets for his students.However, the number of sweets in each packet is not same. For the sake of fairness, the president decides to move some sweets between the packet to make them all equal.

As he has two hands, he must always carry exactly two sweets: one in each hand. Thus, he can only make one type of an action: pick up two sweets from one of the packets and carry both of them to some other packets. Of course,he is not allowed to remove a packet completely. Therefore, he cannot pick up sweets from a packet that currently contains fewer than 3 sweets.

Now, the president want to know the minimum number of actions he has to perform in order to make all packets equal.

There are multiple test cases.

For each test case, the first line contains an integer N, represent there are N packets of sweets. The second line contains N integers Ai, it indicates the number of sweets in ith packet. (1≤N,Ai≤100)

输出 For each case, print an integer which represents the minimum number of actions the president has to perform in order to make all packets equal. If it is impossible to make all packets equal using the allowed type of actions, print “-1” instead.

样例输入

2 1 5 4 7 15 9 5

样例输出

1 3

题意:分甜品,每次只能从包里拿出两个甜品,及左手和右手分别拿一个,现求为使得最后每个包里的甜品都一样的分的最小步骤。

思路:简单贪心,其实求其平均值,用平均值减去每个包里的数目,注意判断正负,最后所得结果除2就行。

PS:注意语句continue 的用法:返回到程序输入的地方,中止后面语句循环,继续下次循环。

代码:

#include#include#include#include#include#include#include#include#includeusing namespace std;#define Max(a,b) a>b?a:b#define min(a,b) a>b?b:a#define mem(a,b) memset(a,b,sizeof(a))int divs[4][2]= {{1,0},{-1,0},{0,1},{0,-1}};int main(){ int n,m,i,j,a[105]; //freopen("1.txt","r",stdin); //freopen("2.txt","w",stdout); while(~scanf("%d",&n)) { int s=0,s1=0,ff=0; for(i=0; iave) { if((a[i]-ave)%2!=0) { ff=-1; break; } else s1+=(a[i]-ave)/2; } else if(a[i]

学长的牛逼代码--以前只见输入有外挂,这次第一次见输入和输出一起加外挂,时间直接跑到648ms,简直无敌,不过输入加外挂确实对输入数据很大的情况下能缩时间

输出的话,如果是字符串的话其实还挺麻烦,

#include #include int Scan() { //输入外挂 int res = 0, flag = 0; char ch; if((ch = getchar()) == '-') flag = 1; else if(ch >= '0' && ch <= '9') res = ch - '0'; while((ch = getchar()) >= '0' && ch <= '9') res = res * 10 + (ch - '0'); return flag ? -res : res;}void Out(int a) { //输出外挂 if(a < 0) { putchar('-'); a = -a; } if(a >= 10) Out(a / 10); putchar(a % 10 + '0');}int main() { int cnt[155]; int n, age; while(~scanf("%d", &n)) { memset(cnt, 0, sizeof(cnt)); for(int i = 0; i < n; i++) { age = Scan(); cnt[age]++; } for(int i = 0; i <= 150; i++) { if(cnt[i]) for(int j = 0; j < cnt[i]; j++) Out(i), putchar(' '); } printf("\n"); } return 0;}

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:Maven创建项目过慢的4种解决办法
下一篇:NYOJ--1112--求次数
相关文章

 发表评论

暂时没有评论,来抢沙发吧~