1165 Block Reversing

分数 25

作者 陈越

单位 浙江大学

Given a singly linked list L. Let us consider every K nodes as a block (if there are less than K nodes at the end of the list, the rest of the nodes are still considered as a block). Your job is to reverse all the blocks in L. For example, given L as 1→2→3→4→5→6→7→8 and K as 3, your output must be 7→8→4→5→6→1→2→3.

Input Specification:

Each input file contains one test case. For each case, the first line contains the address of the first node, a positive N (≤105) which is the total number of nodes, and a positive K (≤N) which is the size of a block. The address of a node is a 5-digit nonnegative integer, and NULL is represented by −1.

Then N lines follow, each describes a node in the format:

Address Data Next

where Address is the position of the node, Data is an integer, and Next is the position of the next node.

Output Specification:

For each case, output the resulting ordered linked list. Each node occupies a line, and is printed in the same format as in the input.

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

 

将链表用静态数组表示,将每个节点用一个结构体存储,结构体信息包括
 * 节点地址add,值dat,下一个结点的地址nex,还有一个flag值,此值为了
 * 让链表排序,有效节点的值从小到大排序,无效结点的值赋值为无穷大;
 * 此后给数组a一个排序,就能得到链表从小到大的排列顺序;

/*** 将链表用静态数组表示,将每个节点用一个结构体存储,结构体信息包括* 节点地址add,值dat,下一个结点的地址nex,还有一个flag值,此值为了* 让链表排序,有效节点的值从小到大排序,无效结点的值赋值为无穷大;* 此后给数组a一个排序,就能得到链表从小到大的排列顺序;
*/#include <iostream>
#include <algorithm>using namespace std;struct Node
{int add, dat, nex, flag;bool operator < (const Node & a) const{return flag < a.flag;}
};const int N = 1e5+10;
struct Node a[N];
int head, n, k;void Read()
{cin >> head >> n >> k;for(int i=0; i<n; ++i){int add, dat, nex;cin >> add >> dat >> nex;a[add] = {add, dat, nex};}
}void Deal()
{for(int i=0; i<N; ++i)a[i].flag = N;int cnt = 0;while(head != -1){a[head].flag = ++cnt; //有效结点的个数head = a[head].nex;}sort(a, a+N);int block = (cnt + k - 1) / k; //链表块的块数for(int i=block-1; i>=0; --i){//如果最后一块链表不足k个,则单独处理if(i == block - 1 && cnt % k){for(int j=i*k; j<cnt; ++j){if(i == 0 && j == cnt-1) //如果是第一块的最后一个结点printf("%05d %d -1\n", a[j].add, a[j].dat);else if(j == cnt-1) //如果是最后一块的最后一个节点printf("%05d %d %05d\n", a[j].add, a[j].dat, a[(i-1)*k].add);else printf("%05d %d %05d\n", a[j].add, a[j].dat, a[j+1].add);}}else{for(int j=i*k; j<(i+1)*k; ++j){if(i == 0 && j == (i+1)*k-1) //如果是第一块的最后一个结点printf("%05d %d -1\n", a[j].add, a[j].dat);else if(j == (i+1)*k-1) //如果是最后一个节点printf("%05d %d %05d\n", a[j].add, a[j].dat, a[(i-1)*k].add);else printf("%05d %d %05d\n", a[j].add, a[j].dat, a[j+1].add);}}}
}int main()
{Read();Deal();return 0;
}

PAT A1165 Block Reversing相关推荐

  1. PAT甲级1074 Reversing Linked List :[C++题解]反转链表,借用vector

    文章目录 题目分析 题目链接 题目分析 分析:数组模拟链表,这题反转操作在数组中进行,然后直接输出即可,甚至不用放回到链表. //遍历链表,该链表用数组模拟 //保存链表结点地址到数组中 for(in ...

  2. PTA 1164-1167 Good in C/Block Reversing/Summit/Cartesian Tree

    1164 Good in C 描述 When your interviewer asks you to write "Hello World" using C, can you d ...

  3. 19年冬季第二题 PAT甲级 1165 Block Reversing (25分) 跟1133类似的题目

    题目 Given a singly linked list L. Let us consider every K nodes as a block (if there are less than K ...

  4. PAT(甲级)2019年冬季考试 7-2 Block Reversing

    这题是做过的,B1025,我还总结过,果然早晚复相逢,只改了一点点,见1025 反转链表. 点睛之笔是结构体数组的哈希,地址既做下标,又有实际含义,妙啊. node[add].add = add; 当 ...

  5. PAT (Advanced Level) Practice 1165 Block Reversing

    题目 没有用链表,感觉用指针写太麻烦了,想起当初刚学数据结构被链表支配的恐惧.坑点是可能的有不在链表的数据,链表的长度不一定为n. #include <bits/stdc++.h> usi ...

  6. PTA Advanced 1165 Block Reversing C++

    目录 题目 Input Specification: Output Specification: Sample Input: Sample Output: 点睛 代码 题目 Given a singl ...

  7. 【PAT】A1074 Reversing Linked List ***

    Given a constant K and a singly linked list L, you are supposed to reverse the links of every K elem ...

  8. 2020年9月PAT甲级满分必备刷题技巧

    2020年7月的考试结束了,除了本次的考题更新,短期内不会更新. [7月题目的特点:首次线上考试,没出链表.树相关的模板题,第2到4题背景新颖,大大降低了抄袭历年代码的可能性,可以看作是线上考试的新趋 ...

  9. pat甲级考试报名费_PAT(甲级)2019年冬季考试 题解

    总结写在前头: 考了字符串.链表(伪链表).图.树.考点比较均衡. 本次考试难度还行,需要较扎实的数据结构底子,对于字符串处理的技巧有一定的要求. 说句题外话,字符串是个重点,主要因为很多人会忽视字符 ...

最新文章

  1. 从C语言的角度重构数据结构系列(五)-C语言的程序结构和基本语法
  2. 关于Java类加载双亲委派机制的思考(附面试题)
  3. Linux内存管理(最透彻的一篇)
  4. 2022年电商发展分析报告
  5. leetcode 实现 strStr()
  6. php将变量转成字符串类型
  7. Redhat6.5安装vnc服务远程桌面
  8. java sleep唤醒_[JavaEE]如何唤醒Sleep中的线程
  9. 流行编曲(6)副旋律&合声
  10. css代码中的ul和li是什么意思呢
  11. Python手机App数据抓取实战:抖音用户的抓取
  12. 工作感悟_of_RS
  13. 台式计算机无法连接网络,台式电脑无法连接无线网络怎么办
  14. 乐山—都江堰青城山精彩游记
  15. 高级运维工程师证书_一位IT运维工程师的CISSP认证历程,值得借鉴!
  16. java核心之类和对象
  17. WR703N烧写openwrt全过程
  18. loadrunner入门教程(6) --新建脚本
  19. Python实现炸金花游戏的示例代码
  20. node-sass改dart-sass and 一些七七八八,实现sass主题色修改

热门文章

  1. 英语----倒装句(下):部分倒装
  2. openedx学习笔记
  3. microbit部署问题及解决
  4. 软件测试周刊(第02期):测试不背锅指南、搞垮领导..
  5. 微信小程序导航:官方文档+精品教程+demo集合(5月9日更新)
  6. 怎么用matlab画双8曲线,MATLAB画双纵轴曲线。
  7. 为什么有的站点别人能访问但我无法访问
  8. 动效之文字滚动5个代码
  9. js 手机端网站底部悬浮html广告代码
  10. 用python打开浏览器的四种方法