PAT (Basic Level) 1055 group photo (analog, good title)

Disclaimer: This article is a blogger original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
This link: https://blog.csdn.net/qq_45458915/article/details/102728239

Topic links: Click here

Subject to the effect: N given individual's name and height, and then give a total need to stand K row, and now need to follow the rules stand, seeking the last queue distribution:

  1. The number of each row of N / K (rounded down), all the extra people standing in the last row;
  2. Back all child not shorter than anyone in the front row;
  3. Each intermediate station the highest row (middle position m / 2 + 1, where m for the number of rows, the division rounded down);
  4. In each row others intermediary axis, according to a non-increasing order of height, the first left and right sides alternately enqueue standing intermediary (e.g. 190,188,186,175,170 height of 5, the formation 175, . 188,190,186,170 this assumes you are facing the photographer, so your left is the right of intermediaries);
  5. If more than the same height, press the name of the dictionary are arranged in descending order. Here to ensure that no duplicate names.

Topic Analysis: considered a medium difficulty under a partial simulation title bar, the main subject of the request is not a good idea to achieve direct, indirect required to achieve a lot of things, but still need to make sure to write clear, there is the original title of the the description and the answer is wrong, that is, above the fifth rule, saying that the original title sequence in ascending order according to the name of the dictionary and found that is not the case after I wrote it, is partially correct, but directly after descending into a a

I feel very good simulation of a problem, you can exercise the ability to implement the code of exercise, not too many unnecessary pit, purely in accordance with the rules of what you want, and yet not so easy to achieve, I feel very good a question, write blog simulated happy about the record, and Mark look good title

Code:

#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;
}

 

Guess you like

Origin blog.csdn.net/qq_45458915/article/details/102728239