当前位置:网站首页>4277. 区块反转
4277. 区块反转
2022-07-27 08:37:00 【追寻远方的人】
给定一个单链表 L,我们将每 K 个结点看成一个区块(链表最后若不足 K 个结点,也看成一个区块),请编写程序将 L 中所有区块的链接反转。
例如:给定 L 为 1→2→3→4→5→6→7→8,K 为 3,则输出应该为 7→8→4→5→6→1→2→3。
补充
本题中可能包含不在单链表中的节点,这些节点无需考虑。
输入格式
第 1 行给出第 1 个结点的地址、结点总个数正整数 N、以及正整数 K,即区块的大小。结点的地址是 5 位非负整数(可能包含前导 0),NULL 地址用 −1−1 表示。
接下来有 N 行,每行格式为:
Address Data Next
其中 Address 是结点地址,Data 是该结点保存的整数数据,Next 是下一结点的地址。
输出格式
对每个测试用例,顺序输出反转后的链表,其上每个结点占一行,格式与输入相同。
数据范围
1≤K≤N≤105
输入样例:
00100 8 3
71120 7 88666
00000 4 99999
00100 1 12309
68237 6 71120
33218 3 00000
99999 5 68237
88666 8 -1
12309 2 33218
输出样例:
71120 7 88666
88666 8 00000
00000 4 99999
99999 5 68237
68237 6 00100
00100 1 12309
12309 2 33218
33218 3 -1
代码:
#include <bits/stdc++.h>
using namespace std;
const int N = 100010;
int n, k;
int h, e[N], ne[N];
int main()
{
scanf("%d%d%d", &h, &n, &k);
for (int i = 0; i < n; i++)
{
int address, data, next;
scanf("%d%d%d", &address, &data, &next);
e[address] = data, ne[address] = next;
}
vector<int> q;
for (int i = h; i != -1; i = ne[i])
q.push_back(i);
int size = q.size() % k; // 这一步必须思考清晰,取余则是多余数据
// size / k 则是你有几个block
reverse(q.begin(), q.end());
bool flag = true;
for (int i = 0; i < q.size();)
{
if (flag)
reverse(q.begin() + i, q.begin() + i + size), i += size, flag = false;
else
{
reverse(q.begin() + i, q.begin() + i + k);
i += k;
}
}
for (int i = 0; i < q.size(); i++)
{
printf("%05d %d ", q[i], e[q[i]]);
if (i + 1 == q.size())
puts("-1");
else
printf("%05d\n", q[i + 1]);
}
return 0;
}
边栏推荐
- CMD command and NPM command
- Day6 --- Sqlalchemy advanced
- redis的string类型及bitmap
- Map structure
- Attack and defense World Lottery
- IBM3650M4实体机安装VCenter7.0
- Background coupon management
- sql_ Mode strict mode (ansi/traditional/strict_trans_tables)
- VS Code中#include报错(新建的头文件)
- After downloading URL loader and specifying the size of the image with limit, the image will not be displayed
猜你喜欢

pytorch_demo1

How to permanently set source

借生态力量,openGauss突破性能瓶颈

好吃难吃饱七分为宜;好喝难喝醉三分为佳

Interviewer: what is scaffolding? Why do you need scaffolding? What are the commonly used scaffolds?

SSTI template injection

User management - restrictions

Realization of background channel group management function

How to upload qiniu cloud

Explain cache consistency and memory barrier
随机推荐
regular expression
Interviewer: what is scaffolding? Why do you need scaffolding? What are the commonly used scaffolds?
[geek challenge 2019] finalsql 1
【渗透测试工具分享】【dnslog服务器搭建指导】
【uni-app高级实战】手把手带你学习一个纯实战复杂项目的开发1/100
Installation and use of Supervisor
[netding cup 2020 rosefinch group]nmap 1 two solutions
1178 questions of Olympiad in informatics -- ranking of grades
After installing mysql, docker entered the container and found that he could not log in to MySQL
Realization of specification management and specification option management functions
Day6 --- Sqlalchemy advanced
好吃难吃饱七分为宜;好喝难喝醉三分为佳
The following license SolidWorks Standard cannot be obtained, and the use license file cannot be found. (-1,359,2)。
Luogu Taotao picks apples
Function realization of order system
Block, there is a gap between the block elements in the row
How to uninstall -- Qianxin secure terminal management system
Flask's operations on model classes
Have a good laugh
Binglog backup data