CCCC L2-022. 重排链表

时间:2021-08-18 10:01:12

题解:直接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
*/