1074. Reversing Linked List (25)
模拟题,注意当k == 1 与 k == n时情况
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <map> using namespace std; const int N = 100005; struct Node
{
int pre;
int value;
int lat;
}node[N]; int order[N];
int size;
map<int, int> pre2idx; void solve1(int fir, int n)
{
map<int, int>::iterator it = pre2idx.find(fir);
order[size++] = it->second; while (node[it->second].lat != -1)
{
it = pre2idx.find(node[it->second].lat);
order[size++] = it->second;
}
} void solve2(int k, int n)
{
int bound = n / k * k; int idx = k;
while (idx <= bound)
{
int boundt = idx;
int boundb = idx - k + 1;
while (boundt >= boundb)
{
int next = 0;
if (boundt > boundb) next = boundt - 1;
else
{
if (idx < bound)
next = idx + k;
else if (idx == bound)
{
if (bound < n)
next = idx + 1;
else if (bound == n)
{
printf("%05d %d -1\n", node[order[boundt]].pre, node[order[boundt]].value);
break;
}
}
}
printf("%05d %d %05d\n", node[order[boundt]].pre, node[order[boundt]].value, node[order[next]].pre);
boundt--;
}
idx += k;
}
bound++;
while (bound <= n)
{
if (node[order[bound]].lat != -1)
printf("%05d %d %05d\n", node[order[bound]].pre, node[order[bound]].value, node[order[bound]].lat);
else printf("%05d %d -1\n", node[order[bound]].pre, node[order[bound]].value);
bound++;
} } int main()
{
int fir, n, k; while (scanf("%d%d%d", &fir, &n, &k) != EOF)
{
pre2idx.clear(); size = 1;
for (int i = 0; i < n; i++)
{
scanf("%d%d%d", &node[i].pre, &node[i].value, &node[i].lat);
pre2idx.insert(make_pair(node[i].pre, i));
} solve1(fir, n); solve2(k , size - 1); }
return 0;
} /*
00100 6 6
00000 4 99999
00100 1 -1
68237 6 -1
33218 3 00000
99999 5 68237
12309 2 33218
*/
1074. Reversing Linked List (25)的更多相关文章
- PAT 甲级 1074 Reversing Linked List (25 分)(链表部分逆置,结合使用双端队列和栈,其实使用vector更简单呐)
1074 Reversing Linked List (25 分) Given a constant K and a singly linked list L, you are supposed ...
- PAT 1074. Reversing Linked List (25)
Given a constant K and a singly linked list L, you are supposed to reverse the links of every K elem ...
- PAT Advanced 1074 Reversing Linked List (25) [链表]
题目 Given a constant K and a singly linked list L, you are supposed to reverse the links of every K e ...
- PAT (Advanced Level) 1074. Reversing Linked List (25)
简单题. #include<cstdio> #include<cstring> #include<cmath> #include<vector> #in ...
- PAT甲题题解-1074. Reversing Linked List (25)-求反向链表
题意说的很清楚了,这种题的话,做的时候最好就是在纸上自己亲手模拟一下,清楚一下各个指针的情况, 这样写的时候就很清楚各个指针变量保存的是什么值. PS:一次AC哈哈,所以说自己动手在纸上画画还是很有好 ...
- 【PAT甲级】1074 Reversing Linked List (25 分)
题意: 输入链表头结点的地址(五位的字符串)和两个正整数N和K(N<=100000,K<=N),接着输入N行数据,每行包括结点的地址,结点的数据和下一个结点的地址.输出每K个结点局部反转的 ...
- PAT甲级1074 Reversing Linked List (25分)
[程序思路] 先根据地址按顺序读入节点,入栈,当栈里的元素个数等于k时全部出栈,并按出栈顺序保存,最后若栈不为空,则全部出栈并按出栈的稀饭顺序保存,最后输出各节点 注意:输入的节点中有可能存在无用节点 ...
- PAT 1074 Reversing Linked List[链表][一般]
1074 Reversing Linked List (25)(25 分) Given a constant K and a singly linked list L, you are suppose ...
- PAT1074 Reversing Linked List (25)详细题解
02-1. Reversing Linked List (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN, Yue ...
随机推荐
- Kafka报错-as it has seen zxid 0x83808 our last zxid is 0x0 client must try another server
as it has seen zxid 0x83808 our last zxid is 0x0 client must try another server 停止zookeeper,删除datadi ...
- HTTP权威协议笔记-5.Web服务器
5.1 Web服务器工作内容 建立连接--接受一个客户端的连接,或者将其拒绝 接受请求--从网络中读取一条HTTP报文 处理请求--对请求报文进行解释,并采取行动 访问资源--访问报文中指定的资源 构 ...
- HBase学习
记录HBase的学习过程.之后会陆续添加内容. 阅读hbase的博客,理解hbase是什么.推荐博文: 1,HBase原理,基础架构,基础概念 2,HBase超详细介绍 --------------- ...
- linux 学习1
这里主要是听讲的一些东西的总结, thanks to lamp brother ____安装虚拟机 vmware 8.0.4, 因为高版本占用空间大,没必要 cent OS 6.3 linux 版本 ...
- [转]配置sonar、jenkins进行持续审查
本文以CentOS操作系统为例介绍Sonar的安装配置,以及如何与Jenkins进行集成,通过pmd-cpd.checkstyle.findbugs等工具对代码进行持续审查. 一.安装配置sonar ...
- PHP中的特殊符号
<?php 注解符号: // 单行注解 /* */ 多行注解 引号的使用 ' ' 单引号,没有任何意义,不经任何处理直接拿过来; " "双引号,php动态处理然后输出,一般用 ...
- libsvm参数学习和核函数使用(转载)
一.参数说明 English libsvm_options: -s svm_type : set type of SVM (default 0) 0 -- C-SVC 1 -- nu-S ...
- FastDFS介绍
相关术语 1)跟踪服务器tracker server 2)存储服务器 storage server 3)元数据 meta data --- 附件上传的说明 4)客户端 client---对程序员暴露 ...
- 查linux端口连接情况用命令netstat
查linux端口连接情况用命令netstat netstat -apn |grep cdnbest 或netstat –apn | grep 3320
- MySQL中auto_increment的基本特性
创建数据表时,经常会出现auto_increment这个词,下面就来了解一下它吧. MySQL的中AUTO_INCREMENT类型的属性用于为一个表中记录自动生成ID功能,可在一定程度上代替Oracl ...