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 ...
随机推荐
- xml---sax操作
<?xml version="1.0" encoding="UTF-8"?> <书架> <书> <书名>书名1& ...
- Can’t Activate Reporting Services Service in SharePoint
访问sharepoint的reporing service 的报表的时候莫名其妙的报错: The requested service, 'http://amatltapp02:32843/1dacf4 ...
- Chrome54安装最新版Flash版本办法
从 Chrome54 版本开始,flash默认已经不能使用了.打开机器上的C:\Users\Administrator\AppData\Local\Google\Chrome\User Data\Pe ...
- {Reship}{Code}{CV}
UIUC的Jia-Bin Huang同学收集了很多计算机视觉方面的代码,链接如下: https://netfiles.uiuc.edu/jbhuang1/www/resources/vision/in ...
- Hcm data loader for cancel work relationship
好好总结一下这个task.没有做好的东西:1.现有的资料和各种各样的工具没有很好的利用起来,造成了用了很多的时间去了解和自己学习.非常的不舒服的.下次要避免这样的浪费时间,学会在工作中学习.现在以及以 ...
- CSS实现DIV超长截断,并显示...
DIV显示内容有时会超长,并把页面撑的很难看, 以前的做法是在JS中,或者后台判断其长度,过长就截断, 但由于中英文数字展示的宽度并不一样,截断的长度也就只能取最小值, 展示的效果也不好.利用CSS提 ...
- 简单又高效的Access分页语句
转自:http://www.ljf.cn/archives/2281 Access实现分页其实也可以在查询语句里面做. 除了流行的top分页法之外,其实还有一种更简单,更容易理解,并且效率也不低的分页 ...
- copy(python中的引用,浅拷贝,深拷贝)
#直接赋值 list = [1,2,['a','b'],'python'] #现将a等于list a = list print a [1,2,['a','b'],'python'] list.appe ...
- Git版本控制与工作流
基本概念 Git是什么? Git是分布式版本控制系统,与SVN类似的集中化版本控制系统相比,集中化版本控制系统虽然能够令多个团队成员一起协作开发,但有时如果中央服务器宕机的话,谁也无法在宕机期间提交更 ...
- oracle+ibatis 批量插入-支持序列自增
首先请先看我前面一篇帖子了解oracle批量插入的sql:[oracle 批量插入-支持序列自增] 我用的ibatis2.0,sqlMap文件引入的标签如下: <!DOCTYPE sqlMap ...