7-2 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 (≤) which is the total number of nodes, and a positive K (≤) which is the size of a block. The address of a node is a 5-digit nonnegative integer, and NULL is represented by −.

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

题意:

链表的转置。给定N和K,每K个转一转,最后还要整个再转一转。

题解:

这道题挺熟悉的,和1074 Reversing Linked List (25分)差不多嘛。

当时依稀记得,用vector超方便,但是忘了怎么用

reverse(valid.begin()+i*k,valid.begin()+i*k+k);

然后就十分痛苦地用不了现成的reverse,只好把开两个vector当成数组用,然后非常艰难地写完了。。。。

AC代码:

#include<bits/stdc++.h>
using namespace std;
struct node{
int d;
int id;
int nx;
}a[],b[];
vector<node>v;
int main(){
int n,k;
int root;
cin>>root>>n>>k;
for(int i=;i<=n;i++){
int x,y,z;
cin>>x>>z>>y;
a[x].d=z;
a[x].nx=y;
a[x].id=x;
}
node nroot=a[root];
v.push_back(nroot);
int num=;
while(nroot.nx!=-){//先按照链表的顺序存好放到vector中
nroot=a[nroot.nx];
v.push_back(nroot);
num++;
}
int cs=num/k;//cs表示要转几次
for(int i=;i<cs;i++){
for(int j=;j<k;j++){
a[i*k+j]=v.at(i*k+k-j-);//每k个转一转,本来可以 reverse(v.begin()+i*k,v()+i*k+k);唉。。。
}
}
int pp=cs*k-;
if(cs*k<num){//剩下的不到K个也要转
for(int i=v.size()-;i>=cs*k;i--) a[++pp]=v.at(i);
}
for(int i=;i<num;i++){//再整个链表转一下,本来可以reverse(v.begin(),v.end())的,唉。。。
b[i]=a[num-i-];
}
for(int i=;i<num;i++){//修改b数组里每个结构体的nx值
if(i!=num-) b[i].nx=b[i+].id;
else b[i].nx=-;
}
for(int i=;i<num-;i++){//输出
printf("%05d %d %05d\n",b[i].id,b[i].d,b[i].nx);
}
printf("%05d %d -1",b[num-].id,b[num-].d);
return ;
}

PAT-2019年冬季考试-甲级 7-2 Block Reversing (25分) (链表转置)的更多相关文章

  1. PAT-2019年冬季考试-甲级 7-1 Good in C (20分)

    7-1 Good in C (20分)   When your interviewer asks you to write "Hello World" using C, can y ...

  2. PAT (Basic Level) Practice (中文)1070 结绳 (25 分) 凌宸1642

    PAT (Basic Level) Practice (中文)1070 结绳 (25 分) 凌宸1642 题目描述 给定一段一段的绳子,你需要把它们串成一条绳.每次串连的时候,是把两段绳子对折,再如下 ...

  3. PAT (Advanced Level) Practice 1006 Sign In and Sign Out (25 分) 凌宸1642

    PAT (Advanced Level) Practice 1006 Sign In and Sign Out (25 分) 凌宸1642 题目描述: At the beginning of ever ...

  4. PAT (Basic Level) Practice (中文)1065 单身狗 (25 分) 凌宸1642

    PAT (Basic Level) Practice (中文)1065 单身狗 (25 分) 凌宸1642 题目描述: "单身狗"是中文对于单身人士的一种爱称.本题请你从上万人的大 ...

  5. PAT (Basic Level) Practice (中文) 1050 螺旋矩阵 (25 分) 凌宸1642

    PAT (Basic Level) Practice (中文) 1050 螺旋矩阵 (25 分) 目录 PAT (Basic Level) Practice (中文) 1050 螺旋矩阵 (25 分) ...

  6. PTA甲级1094 The Largest Generation (25分)

    PTA甲级1094 The Largest Generation (25分) A family hierarchy is usually presented by a pedigree tree wh ...

  7. PAT-2019年冬季考试-甲级 7-3 Summit (25分) (邻接矩阵存储,直接暴力)

    7-3 Summit (25分)   A summit (峰会) is a meeting of heads of state or government. Arranging the rest ar ...

  8. PAT 甲级 1040 Longest Symmetric String (25 分)(字符串最长对称字串,遍历)

    1040 Longest Symmetric String (25 分)   Given a string, you are supposed to output the length of the ...

  9. PAT-2019年冬季考试-甲级 7-4 Cartesian Tree (30分)(最小堆的中序遍历求层序遍历,递归建树bfs层序)

    7-4 Cartesian Tree (30分)   A Cartesian tree is a binary tree constructed from a sequence of distinct ...

随机推荐

  1. 项目Beta冲刺(团队)--6/7

    课程名称:软件工程1916|W(福州大学) 作业要求:项目Beta冲刺 团队名称:葫芦娃队 作业目标:进行新一轮的项目冲刺,尽力完成并完善项目 团队博客 队员学号 队员昵称 博客地址 04160242 ...

  2. 服务器端渲染 VS 浏览器端渲染

    这里的渲染,就是指生成html文档的过程,和浏览器渲染html没有关系. 浏览器端渲染,指的是用js去生成html,前端做路由.举例:React, Vue等等前端框架.适合单页面应用程序. 服务器端渲 ...

  3. git 学习笔记 —— 切换和恢复提交版本( git reset/reflog/tag 命令)

    记录一下关于 git 不同提交版本间切换的操作以及如何恢复至切换之前的版本. 切换到之前提交的版本 —— git reset --hard 笔者在使用 git 时,首先接触到了一个"黑魔法& ...

  4. Nginx——配置文件服务下载

    前言 只是临时搭建的一个下载服务,所以就直接用nginx来咯 步骤 解析域名 将域名解析到要部署应用对应的服务器,就是个解析操作,没啥好讲的 创建目录 # mkdir /data/install/ 配 ...

  5. python预课03 三元表达式示例,函数定义示例,七段彩码管绘制示例

    三元表达式 s = '不下雨' if s == '下雨': print('带伞') if s == '不下雨': print('不带伞') #等效与以下语句 print('带伞' if s == '下 ...

  6. [nodemon] app crashed - waiting for file changes before starting...

    慕课网前端工程师晋升课程 Vue全家桶+SSR+Koa2全栈开发美团网 视频里用 npx create-nuxt-app mt-app创建项目后,不能使用import ...from....跟着老师修 ...

  7. Javascript搞笑图,哈哈哈哈

  8. luogu_2831: 愤怒的小鸟

    洛谷2831:愤怒的小鸟(状压\(dp\)) 题意: 在二维平面上给定\(n\)个点\((1\leq n\leq18)\). 其中每个点用\((x_i,y_i)\)表示\((0<x_i,y_i& ...

  9. minio gataway 模式快速提供s3 兼容的文件服务

    实际很多场景我们已经有了遗留系统的文件存储方式(ftp,或者共享目录),但是这个方式可能不是很好,对于web 不是很友好 实际上minio 也提供了gateway 的模式,可以方便快速的将遗留系统的存 ...

  10. LOJ3058. 「HNOI2019」白兔之舞 [DP,MTT]

    LOJ 前置知识:任意长度NTT 普通NTT只能做\(2^k\)的循环卷积,尝试扩展成长度为\(n\)的循环卷积,保证模意义下\(\omega_n\)存在. 不管怎样还是要算点值.推式子: \[ \b ...