2251. 【2018.1.29普及组模拟】小麦高度

2251. 【2018.1.29普及组模拟】小麦高度

题目描述
Smart和Sarah在每年的圣诞节都要种植小麦。众所周知,小麦的生长速度不同,经过一段时间后,种植的小麦会参差不齐。他们决定玩以下的游戏来解决这个问题:
1、当轮到Smart的时候,他会选择最短的那个小麦,然后延长高度到第二短的小麦的高度。
2、当轮到Sarah的时候,他会选择最长的那个小麦,然后剪短高度到第二长的小麦的高度。
游戏如果能继续的话,小麦中必须有三种不同的高度,如果有人不能再继续游戏,那么他就失败了。
给定所有小麦的高度,假设从Smart开始游戏,写一个程序决出游戏中谁是胜者,并输出游戏结束时最高的和最低的小麦的高度。

输入
第一行输入一个整数N,表示小麦的数量。
第二行输入N个用个空格隔开的整数,表示每个小麦的高度,小麦高度都不超过100000。

输出
输出的第一行为"Smart"表示Smart赢得游戏,为"Sarah"表示Sarah赢得游戏。
输出第二行的两个数分别表示游戏结束时最低和最高的小麦长度。

样例输入
样例输入1

3
3 4 3

样例输入2

5
3 1 2 4 1

样例输入3

7
2 1 3 3 5 4 1

样例输出
样例输出1

Sarah
3 4

样例输出2

Smart
2 3

样例输出3

Sarah
2 3

数据范围限制
50%的数据:1≤N≤500;
80%的数据:1≤N≤3000;
100%的数据:1≤N≤100000。

#include<cstdio>
#include<iostream>
#include<cmath>
#include<cstring>
#include<string>
#include<algorithm>
#include<vector>
#include<queue>
#define fre(x) freopen(#x".in","r",stdin),freopen(#x".out","w",stdout);
using namespace std;
const int MAX=2147483647;
const int N=1e6;
int n,a,f[100010],k,s[100010],tot,head,tail; 
int main()
{
	fre(wheat);
	scanf("%d",&n);
	for(int i=1;i<=n;i++)
	{
		scanf("%d",&a); 
		if(!f[a]) k++;
		f[a]++;
	}
	for(int i=1;i<=100000;i++) 
		if(f[i]) s[++tot]=i;
	head=1,tail=tot;
	if(k<3) printf("Sarah\n");
	while(k>=3)
	{
		if(k>3)
		{
			if(f[s[head]]>f[s[tail]]) 
			{
				f[s[head]]-=f[s[tail]];
				f[s[head+1]]+=f[s[tail]];
				f[s[tail-1]]+=f[s[tail]];
				tail--,k--;			
			}	
			else if(f[s[head]]<f[s[tail]])
			{
				f[s[tail]]-=f[s[head]];
				f[s[head+1]]+=f[s[head]];
				f[s[tail-1]]+=f[s[head]];
				head++,k--;
			}	
			else if(k>4&&f[s[head]]==f[s[tail]])
			{
				f[s[head+1]]+=f[s[head]];
				f[s[tail-1]]+=f[s[head]];
				head++,tail--,k-=2;
			}    
			else if(k==4&&f[s[head]]==f[s[tail]])
				head++,tail--,k-=2,printf("Sarah\n");
		}
		else
		{
			if(f[s[head]]>f[s[tail]]) 
				tail--,k--,printf("Sarah\n");
			else head++,k--,printf("Smart\n");	
		}
	}
	printf("%d %d",s[head],s[tail]);
	return 0;
}
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值