关键
- 双端队列的使用
- 数值和字符串的相互映射(有重复值)
代码
#include <iostream>
#include <cstdio>
#include <string>
#include <set>
#include <deque>
#include <algorithm>
using namespace std;
string array[300][2000];
int a[10001];
string h[10001];
string h2[10001];
int main(){
int n,k;
int count;
string name;
int height;
multiset<int> st;
deque<string> dq;
int tmp3;
int tmp4=0;
cin>>n>>k;
count=n/k;
for(int i=0;i<n;i++){
cin>>name>>height;
tmp3=0;
while(array[height][tmp3]!=""){
tmp3++;
}
array[height][tmp3]=name;
for(int j=tmp3;j>=0;j--){
h[j]=array[height][j];
}
sort(h,h+tmp3+1);
for(int j=tmp3;j>=0;j--){
array[height][j]=h[j];
}
st.insert(height);
}
int tmp=0;
for(multiset<int>::iterator it=st.begin();it!=st.end();it++){
a[tmp]=*it;
while(array[a[tmp]][tmp4]!=""){
tmp4++;
}
h2[tmp]=array[a[tmp]][tmp4-1];
array[a[tmp]][tmp4-1]="";
tmp4=0;
tmp++;
}
int tmp2;
for(int i=0;i<k-1;i++){
tmp2=0;
for(int j=count-1;j>=0;j--){
if(tmp2%2==0){
dq.push_back(h2[i*count+j]);
}
else{
dq.push_front(h2[i*count+j]);
}
tmp2++;
}
for(int j=0;j<count;j++){
h2[i*count+j]=dq.front();
dq.pop_front();
}
}
tmp2=0;
for(int i=n-1;i>=count*(k-1);i--){
if(tmp2%2==0){
dq.push_back(h2[i]);
}
else{
dq.push_front(h2[i]);
}
tmp2++;
}
for(int i=count*(k-1);i<n;i++){
h2[i]=dq.front();
dq.pop_front();
}
for(int i=count*(k-1);i<=n-1;i++){
if(i==count*(k-1)){
cout<<h2[i];
}
else{
cout<<" "<<h2[i];
}
}
cout<<endl;
for(int i=k-2;i>=0;i--){
for(int j=0;j<=count-1;j++){
if(j==0){
cout<<h2[i*count+j];
}
else{
cout<<" "<<h2[i*count+j];
}
}
if(i!=0){
cout<<endl;
}
}
return 0;
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· C#/.NET/.NET Core技术前沿周刊 | 第 29 期(2025年3.1-3.9)
· 从HTTP原因短语缺失研究HTTP/2和HTTP/3的设计差异