当前位置:网站首页>Block reversal (summer vacation daily question 7)
Block reversal (summer vacation daily question 7)
2022-07-28 12:47:00 【sweetheart7-7】
Given a single chain table L L L, We will every K K K A node is regarded as a block ( If the linked list is insufficient at the end K K K Nodes , Also as a block ), Please write a program to L The links of all blocks in are reversed .
for example : Given L L L by 1 → 2 → 3 → 4 → 5 → 6 → 7 → 8 1→2→3→4→5→6→7→8 1→2→3→4→5→6→7→8, K K K by 3 3 3, Then the output should be 7 → 8 → 4 → 5 → 6 → 1 → 2 → 3 7→8→4→5→6→1→2→3 7→8→4→5→6→1→2→3.
Add
This question may contain nodes that are not in the single linked list , These nodes need not be considered .
Input format
The first 1 1 1 Line gives 1 1 1 The address of the node 、 The total number of nodes is a positive integer N N N、 And positive integers K K K, That is, the size of the block . The address of the node is 5 5 5 Bit nonnegative integer ( May contain leading 0 0 0),NULL Address use −1 Express .
Next there is N N N That's ok , The format of each line is :
Address Data Next
among Address Is the node address ,Data Is the integer data saved in this node ,Next Is the address of the next node .
Output format
For each test case , Output the inverted linked list in sequence , Each node on it occupies a row , The format is the same as the input .
Data range
1 ≤ K ≤ N ≤ 1 0 5 1≤K≤N≤10^5 1≤K≤N≤105
sample input :
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
sample output :
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<iostream>
#include<vector>
using namespace std;
const int N = 100010;
int n, k;
int e[N], ne[N];
int main(){
int h = -1;
scanf("%d%d%d", &h, &n, &k);
int addr, data, neaddr;
for(int i = 0; i < n; i++){
scanf("%d%d%d", &addr, &data, &neaddr);
e[addr] = data;
ne[addr] = neaddr;
}
vector<int> v, res;
for(int i = h; i != -1; i = ne[i])
v.push_back(i);
int t = v.size() / k;
if(v.size() % k != 0) t++;
for(int i = t - 1; i >= 0; i--)
for(int j = i * k; j < (i+1) * k && j < v.size(); j++)
res.push_back(v[j]);
for(int i = 0; i < res.size(); i++){
printf("%05d %d ", res[i], e[res[i]]);
if(i == res.size() - 1) puts("-1");
else printf("%05d\n", res[i + 1]);
}
return 0;
}
边栏推荐
- Using JSON in C language projects
- Leetcode: array
- leetcode 376. Wiggle Subsequence
- 苏黎世联邦理工学院 | 具有可变形注意Transformer 的基于参考的图像超分辨率(ECCV2022))
- 大模型哪家强?OpenBMB发布BMList给你答案!
- Kafaka丢消息吗
- HMS core audio editing service supports 7 kinds of audio effects to help one-stop audio processing
- leetcode:数组
- STM32 loopback structure receives and processes serial port data
- GMT安装与使用
猜你喜欢
随机推荐
Newly released, the domestic ide developed by Alibaba is completely open source
03 pyechars 直角坐标系图表(示例代码+效果图)
Redis实现分布式锁
XIII Actual combat - the role of common dependence
[nuxt 3] (XII) project directory structure 3
AI制药的数据之困,分子建模能解吗?
Leetcode 42. rainwater connection
Under what circumstances can the company dismiss employees
MSP430 开发中遇到的坑(待续)
[apue] 文件中的空洞
输入字符串,内有数字和非字符数组,例如A123x456将其中连续的数字作为一个整数,依次存放到一个数组中,如123放到a[0],456放到a[1],并输出a这些数
一台电脑上 多个项目公用一个 公私钥对拉取gerrit服务器代码
sqli-labs(less-8)
恋爱男女十禁
快速读入
C# 泛型是什么、泛型缓存、泛型约束
Developing NES games with C language (cc65) 04. Complete background
Hc-05 Bluetooth module debugging slave mode and master mode experience
SuperMap arsurvey license module division
Linear classifier (ccf20200901)






![[cute new problem solving] climb stairs](/img/a2/fd2f21c446562ba9f0359988d97efe.png)

