1110 区块反转 (25 分)

分析

  • 注意
    • 一个比较坑的点:题目给的点可能不在链表中,所以我们只需要把链表中的数据排序。这种坑真不像个人出的题目。
  • 思路
    • 按照每个节点具有两重属性,所以这里可以用双关键字排序。
    • 第一个关键字:当前是第几个块。
    • 第二个关键字:当前是这个块中的第几个元素。
  • 详细讲解:
    • 见B站视频:我不是匠人

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;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值