当前位置:网站首页>1110 区块反转
1110 区块反转
2022-06-10 03:08:00 【NEFU AB-IN】
Powered by:NEFU AB-IN
1110 区块反转
题意
给定一个单链表 L,我们将每 K 个结点看成一个区块(链表最后若不足 K 个结点,也看成一个区块),请编写程序将 L 中所有区块的链接反转。例如:给定 L 为 1→2→3→4→5→6→7→8,K 为 3,则输出应该为 7→8→4→5→6→1→2→3。
思路
建立单链表后遍历,将每组的头节点放入vector,并逆序,最后输出即可
代码
/* * @Author: NEFU AB-IN * @Date: 2022-06-04 11:45:48 * @FilePath: \ACM\GPLT\1110.cpp * @LastEditTime: 2022-06-04 12:57:32 */ #include <bits/stdc++.h> using namespace std; #define SZ(X) ((int)(X).size()) #define IOS \ ios::sync_with_stdio(false); \ cin.tie(0); \ cout.tie(0); #define DEBUG(X) cout << #X << ": " << X << endl; typedef pair<int, int> PII; const int N = 1e6 + 10; int h, e[N], ne[N]; int n, k; signed main() { IOS; cin >> h >> n >> k; for (int i = 1; i <= n; ++i) { int addr, data, nxt; cin >> addr >> data >> nxt; e[addr] = data; ne[addr] = nxt; } vector<int> lst; int cnt = 0; for (int i = h; ~i; i = ne[i]) { if (cnt == 0) { lst.push_back(i); } cnt += 1; if (cnt == k) cnt = 0; } vector<int> ans; reverse(lst.begin(), lst.end()); for (auto addr : lst) { for (int i = addr, cnt = 0; ~i && cnt < k; i = ne[i], cnt++) { ans.push_back(i); } } for (int i = 0; i < SZ(ans); ++i) { if (i != SZ(ans) - 1) printf("%05d %d %05d\n", ans[i], e[ans[i]], ans[i + 1]); else printf("%05d %d -1\n", ans[i], e[ans[i]]); } return 0; }
边栏推荐
- Lua's modules and packages
- Tidb experience sharing 01
- 18行列式及其性质
- Determinant and its properties
- leetcode 19. 删除链表的倒数第 N 个结点
- Data Lake solutions of various manufacturers
- 修改谷歌浏览器Google Chrome缓存位置
- Wang Xing, Zhang Yong, Xu Lei, who can win the local e-commerce?
- How to distinguish the attributes of the object itself from those on the prototype
- Matplotlib visual data analysis chart
猜你喜欢

零拷贝原理详解

Yum Usage Summary

Protobuf basic introduction to installation and use

Jupyter notebook configuring virtual environments

leetcode:305. 岛屿的数量

86. (leaflet house) leaflet military plotting - collection of linear arrows

C # extension method (this in the method parameter)

FPGA 可以双目以及单目运算

Arduino and processing serial port communication (match function)

qiankun 内实现应用通讯的三种方式(props、initGlobalState、socket.io)
随机推荐
Matplotlib visual data analysis chart
Tidb experience sharing 01
Cancel printing useless information in tensorflow, such as tensorflow:autograph could not transform < *> and will run it as is, loading CUDA information, etc
Disgruntled Judge(扩欧+枚举)
在不同领域内自相关函数/自协方差函数含义辨析
leetcode:305. 岛屿的数量
Anaconda modify file save path
ESP32 内部函数/变量无法跳转到定义
Educational Codeforces Round 129 (Rated for Div. 2)(A-D)
P1082 [NOIP2012 提高组] 同余方程
Tensorflow. Mobilenet for getting started with JS
Detailed explanation of redis iterative query and its use: scan command, sscan command, hscan command and zscan command
V8 global.gc() 的实现
Sword finger offer 09 Implementing queues with two stacks
How to hack user code gracefully
洛谷 P1902 刺杀大使(二分 | BFS)
switch case语法
TS 23.122
对《C语言深度解剖》(第2版)这本书的一个阅读笔记
Idea start multiple services with the same set of code