分析
- 注意
- 一个比较坑的点:题目给的点可能不在链表中,所以我们只需要把链表中的数据排序。这种坑真不像个人出的题目。
- 思路
- 按照每个节点具有两重属性,所以这里可以用双关键字排序。
- 第一个关键字:当前是第几个块。
- 第二个关键字:当前是这个块中的第几个元素。
- 详细讲解:
AC代码
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
const int N = 1e5+5;
struct node{
int add, data, next, tag1, tag2;
bool operator < (const node& a)const{
if(tag1 != a.tag1){
return tag1 > a.tag1;
}
return tag2 < a.tag2;
}
}E[N];
int main() {
int L, n, k;
cin>>L>>n >>k;
int add, data, next;
vector<node> res;
for(int i = 0; i < N; i++) {
E[i].tag1 = -1;
E[i].tag2 = -1;
}
for(int i = 0; i < n; i++) {
cin>>add>>data>>next;
E[add] = {add, data, next,-1,-1};
}
auto p = L;
int cnt = 0;
while(p != -1) {
E[p].tag1 = cnt/k;
E[p].tag2 = cnt%k;
cnt++;
p = E[p].next;
}
sort(E, E+N);
for(int i = 0; i < cnt; i++){
if(i != cnt - 1) printf("%05d %d %05d\n", E[i].add, E[i].data, E[i+1].add);
else printf("%05d %d -1\n", E[i].add, E[i].data);
}
return 0;
}