编程语言:Java
题目:
题解:太菜了 /(ㄒoㄒ)/~~,用了StreamTokenizer还是超时了,这说明我是个菜鸟。(这道题数据还是挺水的,最开始我以为只翻转前k个结点,结果19分,后来看到测试点2是K!=N但是能够全反转才反应过来。)
结果:PAC(测试点5 TL)
import java.io.*;
import java.util.Scanner;
public class Main {
static StreamTokenizer in = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
static PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out));
static Scanner sc = new Scanner(new BufferedInputStream(System.in));
static node[] a, b;
static int n;
public static void main(String[] args) throws IOException {
in.nextToken();
int head = (int) in.nval;
in.nextToken();
n = (int) in.nval;
in.nextToken();
int k = (int) in.nval;
a = new node[n];
b = new node[n];
for (int i = 0; i < n; i++) {
in.nextToken();
int now = (int) in.nval;
in.nextToken();
int data = (int) in.nval;
in.nextToken();
int next = (int) in.nval;
a[i] = new node(now, data, next);
}
node now = find(head);
int idt = 0;
for (; ; ) {
b[idt++] = now;
if (now.next == -1)
break;
else
now = find(now.next);
}
node[] c = new node[idt];
int m = 0;
if (k <= idt) {
int q = k;
//翻转,这里可以不用新数组去存储反转结果,直接进行打印,可能不会超时,我懒得去尝试了,,,
for (; q <= idt; q += k) {
for (int i = q - 1; i > q - k - 1; i--) {
c[m++] = b[i];
}
}
for (int i = q - k; i < idt; i++)
c[i] = b[i];
for (int i = 0; i < idt; i++) {
if (i == idt - 1)
out.printf("%05d %d -1\n", c[i].now, c[i].data);
else
out.printf("%05d %d %05d\n", c[i].now, c[i].data, c[i + 1].now);
}
} else {
//完全翻转,直接从链表末尾开始打印
for (int i = idt - 1; i > -1; i--) {
if (i == 0)
out.printf("%05d %d -1\n", b[i].now, b[i].data);
else
out.printf("%05d %d %05d\n", b[i].now, b[i].data,b[i - 1].now);
}
}
out.flush();
}
private static node find(int head) {
for (int i = 0; i < n; i++) {
if (a[i].now == head)
return a[i];
}
return null;
}
}
class node {
int now;
int data;
int next;
public node(int now, int data, int next) {
this.now = now;
this.data = data;
this.next = next;
}
}
学到一种新解法,确实简洁但速度没快多少,如下:
import java.io.*;
import java.util.*;
public class Main {
static StreamTokenizer in = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
static PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out));
static Scanner sc = new Scanner(new BufferedInputStream(System.in));
static int MAXN=100005;
public static void main(String[] args) throws IOException {
int[] data=new int[MAXN];
int[] next=new int[MAXN];
int[] list=new int[MAXN];
in.nextToken();
int head= (int) in.nval;
in.nextToken();
int n= (int) in.nval;
in.nextToken();
int k= (int) in.nval;
for(int i=0;i<n;i++){
in.nextToken();
int a= (int) in.nval;
in.nextToken();
int b= (int) in.nval;
in.nextToken();
int c= (int) in.nval;
data[a]=b;
next[a]=c;
}
int sum=0;
while(head!=-1){
list[sum++]=head;
head=next[head];
}
for(int i=0;i<sum-sum%k;i+=k){
for(int j=0;j<k/2;j++){
int t=list[i+j];
list[i+j]=list[i+k-j-1];
list[i+k-j-1]=t;
}
}
for(int i=0;i<sum-1;i++)
out.printf("%05d %d %05d\n",list[i],data[list[i]],list[i+1]);
out.printf("%05d %d -1\n",list[sum-1],data[list[sum-1]]);
out.flush();
}
}