NYOJ--1105--sweets

来源:互联网 发布:java工厂方法模式 编辑:程序博客网 时间:2024/06/03 16:33

LANGyang系列 ~~             sweets

链接:click here

时间限制:1000 ms  |  内存限制:65535 KB
难度:1
描述
The president of LanXiang 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.
样例输入
21 547 15 9 5
样例输出
13

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

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

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

代码:

#include<cstdio>#include<cmath>#include<ctime>#include<queue>#include<stack>#include<cstring>#include<cstdlib>#include<iostream>#include<algorithm>using 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; i<n; i++)        {            scanf("%d",&a[i]);            s+=a[i];        }        if(s%n)        ff=-1;         int ave=s/n;        for(i=0; i<n; i++)        {            if(a[i]>ave)            {                if((a[i]-ave)%2!=0)                {                    ff=-1;                    break;                }                else s1+=(a[i]-ave)/2;            }            else if(a[i]<ave)            {                if((ave-a[i])%2)                {                    ff=-1;                    break;                }            }        }        if(ff)            printf("-1\n");        else            printf("%d\n",s1);    }}

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

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

 #include <cstdio>#include <cstring>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;}        





0 0