CCCC L2-022. 重排链表
题解:直接list模拟,头尾两个指针,分别将头尾元素push到另一个list里面,输处输入方式同上一篇
坑:第一发卡了第二个样例,第二发卡了第4个,莫名其妙,所以把两个代码合起来,然后强行ac了。
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <queue>
#include <vector>
#include <cmath>
#include <cstring>
#include <string>
#include <map>
#include<stack>
#include<set>
#include<string.h>
#include<list>
#define pb push_back
#define mp make_pair
#define _for(i, a, b) for (int i = (a); i<(b); ++i)
#define _rep(i, a, b) for (int i = (a); i <= (b); ++i) using namespace std;
const int N = + ;
//double num[N], price[N], ave[N];
int nxt[N], val[N];
int nxt1[N];
map<int, int> p;
list<pair<int, int> >l,ll;
int main() {
int head, n;
cin >> head >> n;
_for(i, , n) {
int x;
cin >> x;
cin >> val[x] >> nxt[x]; } for (int p = head; p != -; p = nxt[p]) {
l.push_back(mp(p, val[p]));
} list<pair<int, int> >::iterator it1,it2;
it2 = l.end(); it2--;
if (n % == ) {
for (it1 = l.begin();;) {//
ll.push_back(*it2); n--; if (n == )break;
it2--; //if (it1 == it2)break;
ll.push_back(*it1); n--; if (n == )break;
it1++;// if (it1 == it2)break; }
}
else {
for (it1 = l.begin(); it1 != it2;) {
ll.push_back(*it2);
ll.push_back(*it1); it1++; if (it1 == it2)break;
it2--;
}
}
for (it1 = ll.begin(); it1 != ll.end(); ) {
printf("%05d %d ", it1->first, it1->second);
//cout << it->first << ' ' << it->second << ' ';
if (++it1 != ll.end())printf("%05d\n", it1->first);//cout<< it->first << endl;
else cout << - << endl;
} system("pause"); }
/*
00100 3 23854 2 1
1 3 -1
00100 1 23854
*/
CCCC L2-022. 重排链表的更多相关文章
- L2-022. 重排链表
L2-022. 重排链表 时间限制 500 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 陈越 给定一个单链表 L1→L2→...→Ln-1→Ln,请 ...
- pat甲级 团体天梯赛 L2-022. 重排链表
L2-022. 重排链表 时间限制 500 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 陈越 给定一个单链表 L1→L2→...→Ln-1→Ln,请 ...
- GPLT天梯赛 L2-022. 重排链表
L2-022. 重排链表 时间限制 500 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 陈越 给定一个单链表 L1→L2→...→Ln-1→Ln,请 ...
- Leetcode 143.重排链表
重排链表 给定一个单链表 L:L0→L1→…→Ln-1→Ln ,将其重新排列后变为: L0→Ln→L1→Ln-1→L2→Ln-2→… 你不能只是单纯的改变节点内部的值,而是需要实际的进行节点交换. 示 ...
- Java实现 LeetCode 143 重排链表
143. 重排链表 给定一个单链表 L:L0→L1→-→Ln-1→Ln , 将其重新排列后变为: L0→Ln→L1→Ln-1→L2→Ln-2→- 你不能只是单纯的改变节点内部的值,而是需要实际的进行节 ...
- 【Warrior刷题笔记】143.重排链表 【线性化 || 双指针+翻转链表+链表合并】详细注释
题目一 力扣143.重排链表 来源:力扣(LeetCode) 链接:https://leetcode-cn.com/problems/reorder-list/ 1.描述 给定一个单链表L的头节点he ...
- L2-022 重排链表 (25 分)
L2-022 重排链表 (25 分) 给定一个单链表 L1→L2→⋯→Ln−1→Ln,请编写程序将链表重新排列为 Ln→L1→Ln−1→L2→⋯.例 ...
- LeetCode 重排链表 OPPO笔试
重排链表 几个关键点: 1. 双指针(快慢指针找中点)(用于反转后一部分) 2. 反转后一部分 (reverse函数) 3. 合并链表 合并的时候在笔试的时候想了一种比我之前想的简单的方法 从slow ...
- 天梯赛 L2-022. (数组模拟链表) 重排链表
题目链接 题目描述 给定一个单链表 L1→L2→...→Ln-1→Ln,请编写程序将链表重新排列为 Ln→L1→Ln-1→L2→....例如:给定L为1→2→3→4→5→6,则输出应该为6→1→5→2 ...
随机推荐
- sublime + emmet(Zen Coding)
今天接触sublime这个编辑器,一下子就喜欢上它了,以前我一直使用NOTEPAD++,果断换上sublime玩玩,呵呵 编辑功能啥的没话,作为前端开发的话,和emmet(原名叫Zen Coding) ...
- centos7安装python-3.5
sudo yum install gcc wget https://www.python.org/ftp/python/3.5.0/Python-3.5.0.tgz sudo cp Python-.t ...
- my97date 时间范围限制
需求:根据开始时间,动态限制结束时间 实现: <!DOCTYPE html> <html lang="en"> <head> <meta ...
- Nginx 访问控制
根据 IP 限制访问: location /admin/ { allow 192.168.1.1; allow 192.168.1.2; deny all; } 根据正则限制访问: location ...
- MVC的简单初步学习(2)
今天似乎一切是正常的,我们的课依旧在进行着,但是恍惚脑海中并没有那样的平静,不知道在想些什么?而且今天是学习MVC的初步开始,我应该认真地学习才是正确的啊.但是我并不糊涂,今天是周一,也就是刚开始上课 ...
- Xcode 插件集:xTextHandler
本文转载至 http://www.tuicool.com/articles/zIFvQn7 基于 Xcode Source Editor Extension 做了一个插件集,叫做 xTextHandl ...
- codeforces水题100道 第十四题 Codeforces Round #321 (Div. 2) A. Kefa and First Steps (brute force)
题目链接:http://www.codeforces.com/problemset/problem/580/A题意:求最长连续非降子序列的长度.C++代码: #include <iostream ...
- 【PHP】 毫秒级时间戳和日期格式转换
在并发量搞得情况下.需要开启毫秒级运算 mysql 支持: `create_time` datetime() DEFAULT NULL COMMENT '创建时间', 效果 PHP 代码实现: &l ...
- java基础---->java的新特性(一)
通过简单的实例来感觉一下java7和java8的新特性.当那条唯捷径省略了朝拜者,我便在一滴花露中瞬间彻悟. java7代码实例 一.java7中switch中可以字符串 @Test public v ...
- springbatch---->springbatch的使用(六)
前面的例子step都是线性的流程,这里我们提供一个非线性的流程,也就是根据不同的状态做不同的流程step处理.万一有天悔恨变得太现实太世故太麻木,说不定能从回忆中重拾随兴飞翔. step非线性的流程 ...