第四个测试点错误,第五个运行超时,代码应该没有错啊,难道是空间使用太多了?
#include <iostream>
using namespace std;
int main(){
int first, n, k, head, num, next;
cin >> first >> n >> k;
int a1[100000]={0}, a2[100000]={0}, a3[n]={0}, a5[n] = {0};
for(int i = 0; i < n; i++){
cin >> head >> num >> next;
a1[head] = num;
a2[head] = next;
}
int i, p = 0, q = 0;
while(first != -1){
for(i = 0; i < 100005; i++){
if(i == first){
a3[p++] = i; break; // 记录原本排序的head
}
}
first = a2[i]; // first 等于 next;
}
for(int i = 0; i < n; i++) // 对head 根据题目要求重新排序
if(a1[a3[i]] < 0) a5[q++] = a3[i];
for(int i = 0; i < n; i++)
if(a1[a3[i]] >= 0 && a1[a3[i]] < k) a5[q++] = a3[i];
for(int i = 0; i < n; i++)
if(a1[a3[i]] == k) a5[q++] = a3[i];
for(int i = 0; i < n; i++)
if(a1[a3[i]] > k) a5[q++] = a3[i];
for(int i = 0; i < n-1; i++)
printf("%05d %d %05d\n", a5[i], a1[a5[i]], a5[i+1]);
printf("%05d %d %d\n", a5[n-1], a1[a5[n-1]], -1);
return 0;
}
柳神代码
https://blog.csdn.net/liuchuo/article/details/78037319
#include <iostream>
#include <vector>
using namespace std;
struct node{
int data, next;
}list[100000];
vector<int> v[3]; //注意是中括号;vector数组
int main(){
int start, n, k, a;
cin >> start >> n >> k;
for(int i = 0; i < n; i++){
cin >> a;
cin >> list[a].data >> list[a].next;
}
int p = start;
while(p != -1){
int data = list[p].data;
if(data < 0)
v[0].push_back(p);
else if(data >= 0 && data <= k)
v[1].push_back(p);
else
v[2].push_back(p);
p = list[p].next;
}
int flag = 0;
for(int i = 0; i < 3; i++){
for(int j = 0; j < v[i].size(); j++){
if(flag == 0){
printf("%05d %d", v[i][j], list[v[i][j]].data);
flag = 1;
}else{
printf(" %05d\n%05d %d", v[i][j], v[i][j], list[v[i][j]].data);
}
}
}
printf(" -1");
return 0;
}