当前位置:网站首页>区块反转(暑假每日一题 7)
区块反转(暑假每日一题 7)
2022-07-28 11:48:00 【sweetheart7-7】
给定一个单链表 L L L,我们将每 K K K 个结点看成一个区块(链表最后若不足 K K K 个结点,也看成一个区块),请编写程序将 L 中所有区块的链接反转。
例如:给定 L L L 为 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 为 3 3 3,则输出应该为 7 → 8 → 4 → 5 → 6 → 1 → 2 → 3 7→8→4→5→6→1→2→3 7→8→4→5→6→1→2→3。
补充
本题中可能包含不在单链表中的节点,这些节点无需考虑。
输入格式
第 1 1 1 行给出第 1 1 1 个结点的地址、结点总个数正整数 N N N、以及正整数 K K K,即区块的大小。结点的地址是 5 5 5 位非负整数(可能包含前导 0 0 0),NULL 地址用 −1 表示。
接下来有 N N N 行,每行格式为:
Address Data Next
其中 Address 是结点地址,Data 是该结点保存的整数数据,Next 是下一结点的地址。
输出格式
对每个测试用例,顺序输出反转后的链表,其上每个结点占一行,格式与输入相同。
数据范围
1 ≤ K ≤ N ≤ 1 0 5 1≤K≤N≤10^5 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<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;
}
边栏推荐
- Distributed timer
- MarkDown简明语法手册
- Arduino Pro Mini atmega328p connect and light the first LED (at the same time, record the problem of burning failure stk500_recv)
- Use json.stringify() to format data
- C structure use
- Custom paging tag 02 of JSP custom tag
- Developing NES game (cc65) 03 and VRAM buffer with C language
- 用arduino开发ESP8266 搭建开发环境
- Introduction to resttemplate
- Merge table rows - three levels of for loop traversal data
猜你喜欢

MarkDown简明语法手册

用arduino开发ESP8266 搭建开发环境

Open source huizhichuang future | 2022 open atom global open source summit openatom openeuler sub forum was successfully held
![[half understood] zero value copy](/img/5b/18082c1ea93f2e3bbf4920d73163fd.png)
[half understood] zero value copy

Developing NES games with C language (cc65) 09, scrolling

05 pyechars 基本图表(示例代码+效果图)

Is it overtime to be on duty? Take up legal weapons to protect your legitimate rights and interests. It's time to rectify the working environment

微创电生理通过注册:年营收1.9亿 微创批量生产上市企业

DIY system home page, your personalized needs PRO system to meet!

VS1003 debugging routine
随机推荐
Custom paging tag 02 of JSP custom tag
Implementation method of mouse hover, click and double click in ue4/5
Developing NES game (cc65) 03 and VRAM buffer with C language
Open source huizhichuang future | 2022 open atom global open source summit openatom openeuler sub forum was successfully held
Using dependent packages to directly implement paging and SQL statements
[dark horse morning post] LETV 400 employees have no 996 and no internal papers; Witness history! 1 euro =1 US dollar; Stop immediately when these two interfaces appear on wechat; The crackdown on cou
微创电生理通过注册:年营收1.9亿 微创批量生产上市企业
stm32 回环结构接收串口数据并处理
Foam exploded three times, why did Luo Yonghao put all his eggs in one basket to do ar?
1331. 数组序号转换 : 简单模拟题
Use json.stringify() to format data
让arduino支持nuvotom新唐
FutureWarning: Indexing with multiple keys (implicitly converted to a tuple of keys) will be depreca
奥浦迈生物通过注册:半年营收1.47亿 国寿成达与达晨是股东
New Oriental's single quarter revenue was 524million US dollars, a year-on-year decrease of 56.8%, and 925 learning centers were reduced
Markdown concise grammar manual
Industry, University, research and application jointly build an open source talent ecosystem | the 2022 open atom global open source summit education sub forum was successfully held
Most of the interfaces of Tiktok are already available, and more interfaces are still open. Please look forward to it
AVL tree (balanced search tree)
与元素类型 “item” 相关联的 “name” 属性值不能包含'&lt;” 字符解决办法