当前位置:网站首页>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;
}
边栏推荐
- Flink1.15源码阅读flink-clients客户端执行流程(阅读较枯燥)
- regular expression
- After installing mysql, docker entered the container and found that he could not log in to MySQL
- Openresty + keepalived 实现负载均衡 + IPV6 验证
- Block, there is a gap between the block elements in the row
- Day3 -- flag state holding, exception handling and request hook
- Implementation of registration function
- List delete collection elements
- Day4 --- flask blueprint and rest ful
- 情人节,我用字符画出了一个对象!
猜你喜欢

Day4 --- flask blueprint and rest ful

On Valentine's day, I drew an object with characters!

开怀一笑

What are the differences or similarities between "demand fulfillment to settlement" and "purchase to payment"?

Alibaba cloud international receipt message introduction and configuration process

Using ecological power, opengauss breaks through the performance bottleneck

P7 Day1 get to know the flask framework

Have a good laugh

Chapter 2 foreground data display

How to permanently set source
随机推荐
Login to homepage function implementation
Massive data Xiao Feng: jointly build and govern opengauss root community and share a thriving new ecosystem
Interviewer: what is scaffolding? Why do you need scaffolding? What are the commonly used scaffolds?
MCDF top level verification scheme
Alibaba cloud international receipt message introduction and configuration process
Realization of specification management and specification option management functions
Weekly learning summary
Hundreds of people participated. What are these people talking about in the opengauss open source community?
openGauss之TryMe初体验
“寻源到结算“与“采购到付款“两者有什么不同或相似之处?
Bandwidth and currency
Zhongang Mining: the new energy industry is developing rapidly, and fluorine chemical products have a strong momentum
Redis configuration file download
Set set
第2章 前台数据展现
General view, DRF view review
Iterators and generators
JS advanced knowledge - function
Containerd failed to pull private database image (kubelet)
One book 1201 Fibonacci sequence