zoukankan      html  css  js  c++  java
  • 1109 Group Photo (25 分)

    1. 题目

    Formation is very important when taking a group photo. Given the rules of forming K rows with N people as the following:

    • The number of people in each row must be N/K (round down to the nearest integer), with all the extra people (if any) standing in the last row;
    • All the people in the rear row must be no shorter than anyone standing in the front rows;
    • In each row, the tallest one stands at the central position (which is defined to be the position (m/2+1), where m is the total number of people in that row, and the division result must be rounded down to the nearest integer);
    • In each row, other people must enter the row in non-increasing order of their heights, alternately taking their positions first to the right and then to the left of the tallest one (For example, given five people with their heights 190, 188, 186, 175, and 170, the final formation would be 175, 188, 190, 186, and 170. Here we assume that you are facing the group so your left-hand side is the right-hand side of the one at the central position.);
    • When there are many people having the same height, they must be ordered in alphabetical (increasing) order of their names, and it is guaranteed that there is no duplication of names.

    Now given the information of a group of people, you are supposed to write a program to output their formation.

    Input Specification:

    Each input file contains one test case. For each test case, the first line contains two positive integers N ((≤10^{4})), the total number of people, and K (≤10), the total number of rows. Then N lines follow, each gives the name of a person (no more than 8 English letters without space) and his/her height (an integer in [30, 300]).

    Output Specification:

    For each case, print the formation -- that is, print the names of people in K lines. The names must be separated by exactly one space, but there must be no extra space at the end of each line. Note: since you are facing the group, people in the rear rows must be printed above the people in the front rows.

    Sample Input:

    10 3
    Tom 188
    Mike 170
    Eva 168
    Tim 160
    Joe 190
    Ann 168
    Bob 175
    Nick 186
    Amy 160
    John 159
    

    Sample Output:

    Bob Tom Joe Nick
    Ann Mike Eva
    Tim Amy John
    

    2. 题意

    合影时,需要将N个人排成K行进行拍照,队形规则:

    • 每行的人数必须为 N/K(向下取整),所有多余的人(如果有)都放到最后一行;
    • 位于后排的所有人都不得矮于位于前排的任何人;
    • 在每一行中,最高的人站在该行的中心位置(定义为位置(m/2+1),位置从1开始编号,其中 m 是该行的总人数,除法结果向下取整);
    • 在每一行中,其他人必须按照其身高非递增顺序依次排入该行,交替地将他们的位置先安排到最高人的右侧,然后再安排到最高人的左侧(例如,假设五个人的身高为 190、188,186、175、170,最终阵型将是 175、188、190、186、170。在这里,我们假设你面对着合影人员,因此你的左手边位置其实是最高人的右手边位置。);
    • 当许多人的身高相同时,必须按姓名的字典序升序进行排序,保证所有人的姓名不重复。

    题目给定一群人的信息,要求将他们队形排好。

    3. 思路——字符串+sort

    1. 首先将所有人的身高和姓名成对存入一个数组中。
    2. 对数组进行排序,排序规则:身高从高到低排序,身高相同的话根据字典序排序。
    3. 计算每排站多少人,以及最后一排站多少人。以摄影师视角,从最后一排开始排队,先选最高的站在中间位置,然后依次选次高的站在最高的左边,再选下一个站最高的右边,接着选下一个站最左边... 依次进行下去,每排都用这个方式来排队即可。排好一排后就直接输出出来。

    4. 代码

    #include <iostream>
    #include <vector>
    #include <algorithm>
    #include <cstdlib>
    
    using namespace std;
    typedef pair<int, string> PID;
    
    int n, k;
    vector<PID> people;
    
    int cmp(PID p1, PID p2)
    {
    	if (p1.first == p2.first) return p1.second < p2.second;
    	else return p1.first > p2.first;
    }
    
    int main()
    {
    	cin >> n >> k;
    	string name;
    	int height;
    	for (int i = 0; i < n; ++i)
    	{
    		cin >> name >> height;
    		people.push_back(make_pair(height, name));
    	}
    	// 学生按从高到低排序,身高一样的的按照名字字典序排序 
    	sort(people.begin(), people.end(), cmp); 
    	// 每排学生数量 
    	int rowNum = n / k;
    	// 最后一排学生数量 
    	int lastRowNum = n - (k - 1) * rowNum;
    	
    	int index = 0;
    	string res = "";
    	// 从排好序的人群中,依次选取人组成一排
    	// 先选最高站在中间位置,再选次高的在最高的右手边(拍照的人看到的是在最高的左边),紧接着再选下一个在最高的左手边(拍照的人看到的是在最高的右边) 
    	for (int i = 0; i < lastRowNum; ++i)
    	{
    		name = people[index++].second;
    		if (i == 0) res = name;
    		else if (i % 2 == 1) res = name + " " + res;
    		else res += " " + name; 
    	}
    	cout << res << endl;
    	for (int i = 0; i < k - 1; ++i)
    	{
    		res = "";
    		// 同上从排好序的人群中选人排成一排即可 
    		for (int j = 0; j < rowNum; ++j)
    		{
    			name = people[index++].second;
    			if (j == 0) res = name;
    			else if (j % 2 == 1) res = name + " " + res;
    			else res += " " + name;
    		}
    		cout << res << endl;
    	}
    	return 0;
    }
     
    
  • 相关阅读:
    关于MAC下重置MYSQL密码
    MAC下配置PHPStorm环境
    Java中从控制台输入数据的几种常用方法
    IDEA 指定入口class
    Python中的除法
    Python 学习笔记
    SQLiteDatabase中query、insert、update、delete方法参数说明
    listview与sqlite数据绑定
    java中HashMap详解
    只要有信心,人永远不会挫败
  • 原文地址:https://www.cnblogs.com/vanishzeng/p/15489887.html
Copyright © 2011-2022 走看看