| ||||||||||
| Online Judge | Problem Set | Authors | Online Contests | User | ||||||
|---|---|---|---|---|---|---|---|---|---|---|
| Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest | |||||||||
这道题注意理解题意,贴个AC了的代码
import java.util.Scanner;
import java.util.Stack;
public class Main{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while (sc.hasNext()) {
int n = sc.nextInt();
if (n == 0)
break;
while (sc.hasNext()) {
int[] seq = new int[n + 1];
seq[1] = sc.nextInt();
if (seq[1] == 0)
break;
for (int i = 2; i <= n; ++i)
seq[i] = sc.nextInt();
slove(seq, n);
}
System.out.println();
}
}
static void slove(int[] seq, int n) {
Stack<Integer> stack = new Stack<Integer>();
int j = 1;
for (int i = 1; i <= n; ++i) {
stack.push(i);
if (i == seq[j]) {
stack.pop();
j++;
while (!stack.isEmpty()) {
int x = stack.pop();
if (seq[j] != x){
stack.push(x);//如果此数不相等,必须要再push进栈
break;
}
j++;
}
}
}
if (j == n+1)
System.out.println("Yes");
else
System.out.println("No");
}
}
Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator