模拟题,注意当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. NodeJs解析web一例

    var http = require('http'); var fs = require('fs'); var url = require('url'); http.createServer(func ...

  2. Java 实现多线程的两种方式

    1:继承Therad类2:实现Runnable 接口 1.继承Thread类实现多线程继承Thread类的方法尽管被我列为一种多线程实现方式,但Thread本质上也是实现了Runnable接口的一个实 ...

  3. REST及RESTful的实现

    什么是REST? REST (REpresentation State Transfer) 描述了一个架构样式的网络系统,比如 web 应用程序.它首次出现在 2000 年 Roy Fielding ...

  4. information_schema系列八(事物,锁)

    今天我们主要看一下MySQL information_schema里面的关于innodb的锁和事物的两三个系统表: 看一下锁对应的sql: select * from innodb_lock_wait ...

  5. c#接口容易被忽视的问题

    今天在看"并发集合"的时候,接口IProducerConsumerCollection<T> 有一个方法是TryAdd(),表示"试图"去添加,然后 ...

  6. 锋利的js之验证身份证号

    我们在做互联网网站时,注册个人资料时,经常要用到身份证号,我们需要对身份证进验证,不然别人随便输个号码就通过,让你感觉这个网站做得很shit. 身份证号是有规则的. 结构和形式 1.号码的结构  公民 ...

  7. Sql Server隔离级别(1)

    数据库是一个并发操作的环境,就像多线程一样,这样在高并发的情况下回出现一些问题. 假设我们有一张表Account,表结构和数据如下所示 AccountName Balance jo 100 fo 20 ...

  8. java中的静态变量---static

    用static修饰符修饰的数据成员,并不是属于那个对象的,而是属于类.静态数据成员在定义的时候已经分配了内存,而此时对象还没有建立好. 当然它也是被保存到类的方法中的,而不是对象内存中,大家都是可以访 ...

  9. 走进异步编程的世界 - 开始接触 async/await

    [C#] 走进异步编程的世界 - 开始接触 async/await   走进异步编程的世界 - 开始接触 async/await 序 这是学习异步编程的入门篇. 涉及 C# 5.0 引入的 async ...

  10. 说说JSON和JSONP,也许你会豁然开朗(转)

    前言 由于Sencha Touch 2这种开发模式的特性,基本决定了它原生的数据交互行为几乎只能通过AJAX来实现. 当然了,通过调用强大的PhoneGap插件然后打包,你可以实现100%的Socke ...