模拟题,注意当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)的更多相关文章

  1. 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 ...

  2. 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 ...

  3. 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 ...

  4. PAT (Advanced Level) 1074. Reversing Linked List (25)

    简单题. #include<cstdio> #include<cstring> #include<cmath> #include<vector> #in ...

  5. PAT甲题题解-1074. Reversing Linked List (25)-求反向链表

    题意说的很清楚了,这种题的话,做的时候最好就是在纸上自己亲手模拟一下,清楚一下各个指针的情况, 这样写的时候就很清楚各个指针变量保存的是什么值. PS:一次AC哈哈,所以说自己动手在纸上画画还是很有好 ...

  6. 【PAT甲级】1074 Reversing Linked List (25 分)

    题意: 输入链表头结点的地址(五位的字符串)和两个正整数N和K(N<=100000,K<=N),接着输入N行数据,每行包括结点的地址,结点的数据和下一个结点的地址.输出每K个结点局部反转的 ...

  7. PAT甲级1074 Reversing Linked List (25分)

    [程序思路] 先根据地址按顺序读入节点,入栈,当栈里的元素个数等于k时全部出栈,并按出栈顺序保存,最后若栈不为空,则全部出栈并按出栈的稀饭顺序保存,最后输出各节点 注意:输入的节点中有可能存在无用节点 ...

  8. PAT 1074 Reversing Linked List[链表][一般]

    1074 Reversing Linked List (25)(25 分) Given a constant K and a singly linked list L, you are suppose ...

  9. PAT1074 Reversing Linked List (25)详细题解

    02-1. Reversing Linked List (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN, Yue ...

随机推荐

  1. zt:Linux查看程序端口占用情况

    http://www.cnblogs.com/benio/archive/2010/09/15/1826728.html yxr注: 由于安装eda工具libero,license要配端口,为确认端口 ...

  2. 《SSM框架搭建》二.mybatis3,spring4整合

    感谢学习文章来自http://www.cnblogs.com/xdp-gacl/p/4271627.html,spring3修改为spring4.还有提示驱动过期的问题,是由于使用了mysql-con ...

  3. 如何禁止 Mac OS X 在外接设备上生成 .DS_Store 文件?以及如何批量删除 .DS_Store 文件?

    如何禁止 Mac OS X 在外接设备上生成 .DS_Store 文件?以及如何批量删除 .DS_Store 文件?原文链接:http://www.java2class.net/bbs/viewthr ...

  4. 从excel文件中获取数据(2)

    本方法引用 Aspose.Cells.dll,ICSharpCode.SharpZipLib.dll ,NPOI.dll,NPOI.OOXML.dll,NPOI.OpenXml4Net.dll,NPO ...

  5. MVC过滤器详解

    MVC过滤器详解   APS.NET MVC中(以下简称"MVC")的每一个请求,都会分配给相应的控制器和对应的行为方法去处理,而在这些处理的前前后后如果想再加一些额外的逻辑处理. ...

  6. 解锁scott用户及设置密码

    关于Oracle 10g scott用户解锁的方法两则 解决方法一. 首先确认已经安装oracle 数据库和客户端 在客户端DOS下执行如下语句: 注意提示符号 c:\sqlplus /nolog s ...

  7. poj1050

    To the Max Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 39081   Accepted: 20639 Desc ...

  8. ( 转)UVM验证方法学之一验证平台

    在现代IC设计流程中,当设计人员根据设计规格说明书完成RTL代码之后,验证人员开始验证这些代码(通常称其为DUT,Design Under Test).验证工作主要保证从设计规格说明书到RTL转变的正 ...

  9. extjs ajax java简单精美验证码实现 有图

    前端:利用ExtJs的autoEl功能加载图片. var imgCheckValid = new Ext.create('Ext.Component',{ width: 70, //图片宽度 heig ...

  10. SOA架构介绍和理解

    SOA架构介绍和理解 SOA的正确方法论及目标模型,其实SOA在实现架构落地上,需要考虑到对服务的组合,不断的重用现有的服务,让企业应用可以逐步集成,快速实现业务的迭代. 通过SOA架构分层将服务按照 ...