A1109 Group Photo
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
思路:使用链表左右插入即可。
#include"bits/stdc++.h"
using namespace std;
struct Node {
string s;
int v;
};
int main() {
// freopen("input.txt","r",stdin);
int n,k; cin >> n >> k;
vector<Node> nodes(n);
for(int i=0; i<n; i++) {
cin >> nodes[i].s >> nodes[i].v;
}
int num_last = n - (k-1)*(n/k);
sort(nodes.begin(),nodes.end(),[](Node &t1,Node &t2) {
if(t1.v == t2.v)
return t1.s < t2.s;
return t1.v > t2.v;
});
int index = 0;
while(true) {
int num = index == 0 ? num_last : n/k;
if(index+num-1 >= n)
break;
list<int> temp;
for(int i=0; i<num; i++) {
if(temp.size()%2)
temp.push_front(index+i);
else
temp.push_back(index+i);
}
bool blank = false;
for(int t:temp) {
if(blank)
printf(" ");
else
blank = true;
cout << nodes[t].s;
}
cout << endl;
index += num;
}
}