PAT 1109
双向队列很简单,就多一个前加后加和前删后删的区别
直接上代码:
#include <cstdio>
#include <algorithm>
#include <queue>
#include <iostream>
using namespace std;
int N,K,cnt;
struct person{
string name;
int height;
};
bool cmp(person a,person b){
if(a.height != b.height)
return a.height > b.height;
else
return a.name < b.name;
}
//13:43 14:28
int main(){
cin >> N >> K;
cnt=0;//用于统计当前输出到第几个元素
//获得学生数据数组stu
person stu[N];
for(int i=0;i<N;i++)
cin >> stu[i].name >> stu[i].height;
sort(stu,stu+N,cmp);
//声明一个矩阵数组存放摆放的位置
int arr = N/K+N-N/K*K;
//开始将名字填入矩阵
for(int i=0;i<K;i++){
int size;//代表本次要处理的元素
deque<string> tmp;//用于处理排序的双向队列
bool flag = true;//判断前加还是后加
if(i==0) size = arr;
else size = N/K;
for(int j=0;j<size;j++){
if(!flag)//前面
tmp.push_front(stu[cnt++].name);
else {//后面
tmp.push_back(stu[cnt++].name);
}
flag = !flag;
}
while(!tmp.empty()){
cout << tmp.front();
if(tmp.size()!=1) cout << " ";
tmp.pop_front();
}
cout << endl;
}
return 0;
}