1025.反転チェーンテーブル(25)PAT乙級&&1074.Reversing Linked List(25)PAT甲級


甲級トランスミッションゲート乙級トランスミッションゲート
#include
#include


using namespace std;

#define MAX_N 100100

struct Node{
    int address;
    int next;
    int data;
    int order;
}node[MAX_N];

bool cmp(struct Node a,struct Node b){
    return a.orderint main(){
    for(int i=0;iint begin;
    int n;
    int k;
    scanf("%d%d%d",&begin,&n,&k);
    int address;
    for(int i=0;i"%d",&address);
        scanf("%d%d",&node[address].data,&node[address].next);
        node[address].address=address;
    }
    int count=0;
    for(int p=begin;p!=-1;p=node[p].next){
        node[p].order=count++;
    }
    sort(node,node+MAX_N,cmp);
    n=count;
    for(int i=0;ifor(int j=(i+1)*k-1;j>i*k;j--){
            printf("%05d %d %05d
"
,node[j].address,node[j].data,node[j-1].address); } printf("%05d %d ",node[i*k].address,node[i*k].data); if(i1){ printf("%05d
"
,node[(i+2)*k-1].address); } else{ if(n%k==0){ printf("-1
"
); } else{ printf("%05d
"
,node[(i+1)*k].address); for(int l=n/k*k;lprintf("%05d %d ",node[l].address,node[l].data); if(l!=n-1) printf("%05d
"
,node[l+1].address); else printf("-1
"
); } } } } }