topic:
analysis:
First, understand the requirements, the subject is required: for every k elements reversed, that is, if k <, then n / 2 times the need to reverse.
Secondly, the subject may appear in multiple chains, chains can only output required
The following code in the application of reverse algorithm library functions, for reversing element, before the code is as follows:
Code:
#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
using namespace std;
const int MAXN = 1e5+5;
int ad,n,k,ind;
int num[MAXN];
struct node
{
int val,nex;
}no[MAXN];
int main()
{
int add,val,nex;
scanf("%d%d%d",&ad,&n,&k);
for(int i=1;i<=n;++i)
{
scanf("%d%d%d",&add,&val,&nex);
no[add].val = val;
no[add].nex = nex;
}
int st = ad;
while(st!=-1)
{
num[ind++] = st;
st = no[st].nex;
}
int indx = 0;
while(indx+k<=ind) //这里应该是小于等于 因为 indx+k不参与reverse
{
reverse(num+indx,num+indx+k);
indx = indx + k;
}
for(int i=0;i<ind-1;++i)
printf("%05d %d %05d\n",num[i],no[num[i]].val,num[i+1]);
printf("%05d %d -1\n",num[ind-1],no[num[ind-1]].val);
return 0;
}