PAT甲级1109
来源:互联网 发布:虎牙象棋勇哥是软件 编辑:程序博客网 时间:2024/06/11 19:14
1109. Group Photo (25)
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 (<=10000), 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 3Tom 188Mike 170Eva 168Tim 160Joe 190Ann 168Bob 175Nick 186Amy 160John 159Sample Output:
Bob Tom Joe NickAnn Mike EvaTim Amy John
#include<iostream>#include<vector>#include<cstdio>#include<cstring>#include<string>#include<algorithm>#include<queue>#include<iomanip>using namespace std;struct stu{string name;int height;bool operator<(stu s){if (height != s.height)return height > s.height;elsereturn name < s.name;}//重载小于号};int main(){int N, K;cin >> N >> K;stu t;deque<stu> v;//双端队列int N1 = N;while (N--){cin >> t.name >> t.height;v.push_back(t);}sort(v.begin(), v.end()); N = N1;int p = N / K;int lastrow = N - p*(K - 1);deque<stu> vv;for (int i = 0; i < v.size()||vv.size(); i++){if (i < lastrow)//设定lastrow对最后一排进行特殊处理{if (i % 2 == 0)vv.push_back(v[i]);elsevv.push_front(v[i]);//利用双端队列的特性,可以往前插}else{if (i == lastrow||vv.size()==p)//当一排排完时{for (int j = 0; j < vv.size(); j++){cout << vv[j].name;if (j != vv.size() - 1)cout << " ";elsecout << endl;}vv.clear();//一定要记得清空}if(i<v.size())if (vv.size() % 2 == 0)//交替插入vv.push_back(v[i]);elsevv.push_front(v[i]);}}return 0;}
- *浙大甲级PAT 1109
- PAT甲级1109
- PAT 甲级
- PAT甲级 A1025.PAT RANKING
- PAT 甲级 1025 PAT Ranking
- PAT(甲级)1003
- PAT(甲级)1004
- PAT(甲级)1005
- PAT(甲级)1006
- PAT(甲级)1007
- PAT(甲级)1008
- PAT(甲级)1009
- PAT(甲级)1010
- PAT(甲级)1011
- PAT(甲级)1012
- PAT(甲级)1013
- PAT(甲级)1014
- PAT(甲级)1015
- [mmc subsystem] mmc core(第二章)——数据结构和宏定义说明
- Codeforces Round #397(Div. 1 + Div. 2 combined)D. Artsem and Saunders【思维+乱搞】
- 终端使用别名记住登录ssh的ip登录
- JavaWeb起航篇:web通信原理
- RabbitMQ之队列优先级
- PAT甲级1109
- Log4J
- 石子合并 (区间dp)
- Access/Modify/Change时间
- Android_通知Notificaition
- [mmc subsystem] mmc core(第六章)——mmc core主模块
- 数据结构实验之栈:行编辑器
- org.hibernate.MappingException: must specify an identifier type:
- 武器与战术:对聚变时代太空舰队的构想