無限の光を放つのは人間の心であり、無限の闇を生み出すのも人間の心であり、光と闇が絡み合って戦うこの世界は、懐かしくて無力な世界です。
定数Kと単一リンクリストLが与えられた場合、L上のすべてのK要素のリンクを逆にする必要があります。たとえば、Lが1→2→3→4→5→6であるとすると、K = 3の場合、 3→2→1→6→5→4を出力する必要があります。K = 4の場合、4→3→2→1→5→6を出力する必要があります。
入力仕様:
各入力ファイルには1つのテストケースが含まれています。いずれの場合も、最初の行には最初のノードのアドレス、ノードの総数である正のN(≤105)、およびサブリストの長さである正のK(≤N)が含まれています逆転する。ノードのアドレスは5桁の負でない整数で、NULLは-1で表されます。
次に、N行が続き、それぞれが次の形式でノードを記述します。
Address Data Next
ここ Address
で、はノードの位置、 Data
は整数、 Next
は次のノードの位置です。
出力仕様:
ケースごとに、結果の順序付けされたリンクリストを出力します。各ノードは1行を占め、入力と同じ形式で出力されます。
入力例:
00100 6 4
00000 4 99999
00100 1 12309
68237 6 -1
33218 3 00000
99999 5 68237
12309 2 33218
出力例:
00000 4 33218
33218 3 12309
12309 2 00100
00100 1 99999
99999 5 68237
68237 6 -1
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <string>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <vector>
#include <ctime>
#include <cctype>
#include <bitset>
#include <utility>
#include <sstream>
#include <complex>
#include <iomanip>
#include<climits>//INT_MAX
//#include<bits/stdc++.h>
#define PP pair<ll,int>
#define inf 0x3f3f3f3f
#define llinf 0x3f3f3f3f3f3f3f3fll
#define dinf 1000000000000.0
#define PI 3.1415926
typedef long long ll;
using namespace std;
int const mod=1e9+7;
const int maxn=1e6+10;
struct node{
int add;
int data;
int nx;
}a[maxn];
vector<node>V,jg;
int head,n,k;
int main()
{
for(int i=-0;i<maxn;i++)
a[i].add=i;
scanf("%d%d%d",&head,&n,&k);
for(int i=0;i<n;i++)
{
int dz;
scanf("%d",&dz);
scanf("%d%d",&a[dz].data,&a[dz].nx);
}
int p=head;
while(p!=-1)
{
V.push_back(a[p]);
p=a[p].nx;
}
int ls=V.size()/k;
for(int i=0;i<ls;i++)
reverse(V.begin()+i*k,V.begin()+i*k+k);
for(int i=0;i<V.size();i++)
{
if(i!=V.size()-1)
printf("%05d %d %05d\n",V[i].add,V[i].data,V[i+1].add);
else
printf("%05d %d -1\n",V[i].add,V[i].data);
}
return 0;
}