NYOJ--1105--sweets

2024-08-27 03:18
文章标签 nyoj 1105 sweets

本文主要是介绍NYOJ--1105--sweets,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

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.
样例输入
2
1 5
4
7 15 9 5
样例输出
1
3

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

思路:简单贪心,其实求其平均值,用平均值减去每个包里的数目,注意判断正负,最后所得结果除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");elseprintf("%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;
}        





这篇关于NYOJ--1105--sweets的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1110475

相关文章

nyoj 288 士兵杀敌(五)

一道插线问线离线版的题  复杂度O(n); 代码如下: #include<stdio.h>#include<string.h>const int M = 1000003;const int mod=10003;int num[M];int main(){int n,c,q;scanf("%d%d%d",&n,&c,&q);while(c--){int a,b,x;scan

nyoj 1037 Postscript of Tian Ji racing

一道卡贪心的题。 也算一道改编题。 此题的解法推荐为二分图的最大匹配。 首先将输入数据转换一下,然后将满足题意的一组牌建立条边,最终边的覆盖数即为 LN 最后可得的分数。 然后求出最大匹配即可。 代码如下: #include<stdio.h>#include<string.h>char card[30][5];char s[5];int map[30][30];

nyoj 1002 Trucking

同样一道改编题。 只要把题意理解了好。 简单的二分加最短路。 只要二分高度, 然后求最短路,输出满足题意的即可。 代码如下: (最短路用spfa 时间效率高) #include <iostream>#include <cstdio>#include <cstring>#include <ctime>#include <queue>using namespace st

nyoj 1072 我想回家

一道相当题目描述相当扯的题。 这道题目的描述最后说的是求出到达最后一个点的最短距离,所以输入数据最后输入的城堡的坐标是没用的。 就是先求出两点之间的距离,若不大于村落间距离,并且不大于最后的距离限制 l ,则在两点间建边。 最后任意方法求出最短路即可。 #include <iostream>#include<stdio.h>#include<vector>#include<

nyoj 1038 纸牌游戏

poj 的一道改编题,说是翻译题更恰当,因为只是小幅度改动。 一道模拟题,代码掌控能力比较好,思维逻辑清晰的话就能AC。 代码如下: #include<stdio.h>#include<string.h>#include<algorithm>using namespace std;struct node{char c[5];int rk;char da[5];int nu

nyoj 685 查找字符串

当初一开始没做出来。 后来,学习过一段时间之后,在返回来做这道题,忽然发现,map类容器可以做。 PS:需要注意的是:此题如果用c++的输入输出的话,会超时。 O(time):gets()<  scanf() < cin。   附上代码: #include<stdio.h>#include<map>#include<string>#include<string.h>usin

nyoj 695 Judging Filling Problems

一道强大的模拟题。。。 只要学会<string>类的运用即可。。。 注意: 1、细节的处理。 2、问题的分情况讨论。。 附上代码: 有好对缀余的地方,希望大神前来更新。 #include<stdio.h>#include<string.h>#include<string>#include<iostream>using namespace std;int num[1000

NYOJ 37 回文字符串(记忆化搜索)

OJ题目 : 戳这里~~ 描述 所谓回文字符串,就是一个字符串,从左到右读和从右到左读是完全一样的,比如"aba"。当然,我们给你的问题不会再简单到判断一个字符串是不是回文字符串。现在要求你,给你一个字符串,可在任意位置添加字符,最少再添加几个字符,可以使这个字符串成为回文字符串。 输入 第一行给出整数N(0<N<100) 接下来的N行,每行一个字符串,每个字符串长度不超过1000.

NYOJ 763 Vawio Sequence

OJ题目 : 戳这里~ 描述 Vawio Sequence is very funny,it is a sequence of integers. It has some interesting properties. ·   Vawio is of odd length i.e. L = 2*n + 1. ·  The first (n+1) integers of  Vawio s

NYOJ 10 skiing

OJ题目 : 猛戳~~ 描述 Michael喜欢滑雪百这并不奇怪, 因为滑雪的确很刺激。可是为了获得速度,滑的区域必须向下倾斜,而且当你滑到坡底,你不得不再次走上坡或者等待升降机来载你。Michael想知道载一个区域中最长底滑坡。区域由一个二维数组给出。数组的每个数字代表点的高度。下面是一个例子  1 2 3 4 5 16 17 18 19 6 15 24 25 20 7 14