PAT (Basic Level) 1055 集体照(模拟,好题)

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接: https://blog.csdn.net/qq_45458915/article/details/102728239

题目链接:点击查看

题目大意:给出N个人的姓名和身高,再给出一共需要站K排,现在需要按照规则站队,求最后的队列分布:

  1. 每排人数为 N/K(向下取整),多出来的人全部站在最后一排;
  2. 后排所有人的个子都不比前排任何人矮;
  3. 每排中最高者站中间(中间位置为 m/2+1,其中 m 为该排人数,除法向下取整);
  4. 每排其他人以中间人为轴,按身高非增序,先右后左交替入队站在中间人的两侧(例如5人身高为190、188、186、175、170,则队形为175、188、190、186、170。这里假设你面对拍照者,所以你的左边是中间人的右边);
  5. 若多人身高相同,则按名字的字典序降序排列。这里保证无重名。

题目分析:算是一个中等难度偏下的一个模拟题吧,主要是题目要求的思路不太好直接实现,需要间接实现好多东西,不过还是需要写起来一定要保证思路清晰,还有就是原题中的描述和答案是错的,就是上述的第五个规则,原题说的是按名字的字典序升序排序,在我写出来之后发现却不是这样的,部分正确,但改成降序后就直接A了

我觉得挺好的一道模拟题,可以锻炼锻炼代码的实现能力,没有太多不必要的坑,纯粹的按照规则按图索骥,但却又不是那么容易就能实现,感觉挺好的一个题,写个博客记录一下模拟出来的开心,以及马克一下好题

代码:

#include<iostream>
#include<cstdlib>
#include<string>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<climits>
#include<cmath>
#include<cctype>
#include<stack>
#include<queue>
#include<list>
#include<vector>
#include<set>
#include<map>
#include<sstream> 
#include<deque>
#include<unordered_map>
#define Pi acos(-1.0)
using namespace std;

typedef long long LL;

const int inf=0x3f3f3f3f;

const int N=1e4+100;

struct Node
{
	string name;
	int h;
	bool operator<(const Node& a)const
	{
		if(h!=a.h)
			return h<a.h;
		return name>a.name;
	}
}a[N];

string ans[15][N];

int main()
{
//  freopen("input.txt","r",stdin);
	int n,k;
	scanf("%d%d",&n,&k);
	for(int i=0;i<n;i++)
		cin>>a[i].name>>a[i].h;
	sort(a,a+n);
	int N=n/k;
	int m;
	for(int i=0;i<k;i++)
	{
		if(i!=k-1)
		{
			int st=i*N;
			int ed=i*N+N;
			int pos=N/2+1;
			ans[i][pos]=a[ed-1].name;
			for(int j=ed-2;j>=st;j-=2)
			{
				pos--;
				ans[i][pos]=a[j].name;
			}
			pos=N/2+1;
			for(int j=ed-3;j>=st;j-=2)
			{
				pos++;
				ans[i][pos]=a[j].name;
			}
		}
		else
		{
			int st=i*N;
			int ed=n;
			m=ed-st;
			int pos=(ed-st)/2+1;
			ans[i][pos]=a[ed-1].name;
			for(int j=ed-2;j>=st;j-=2)
			{
				pos--;
				ans[i][pos]=a[j].name;
			}
			pos=(ed-st)/2+1;
			for(int j=ed-3;j>=st;j-=2)
			{
				pos++;
				ans[i][pos]=a[j].name;
			}
		}
	}
	for(int i=k-1;i>=0;i--)
	{
		if(i==k-1)
			for(int j=1;j<=m;j++)
				cout<<ans[i][j]<<(j==m?'\n':' ');
		else
			for(int j=1;j<=N;j++)
				cout<<ans[i][j]<<(j==N?'\n':' ');
	}	
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    

    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_45458915/article/details/102728239
今日推荐