ENG  RUS Timus Online Judge
Online Judge
Problems
Authors
Online contests
Site news
Webboard
Problem set
Submit solution
Judge status
Guide
Register
Authors ranklist
Current contest
Scheduled contests
Past contests
Rules
back to board

## Discussion of Problem 1651. Shortest Subchain

why wrong 7
Posted by mingotlik 26 Aug 2012 13:55
I use dp code.
///////////////////

import java.io.BufferedWriter;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.StreamTokenizer;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;

public class Timus_1651 {
static StreamTokenizer st;
public static void main(String[] args) throws IOException{
BufferedWriter out = new BufferedWriter(new OutputStreamWriter(System.out));
int n = nextInt();
int []a = new int [n+1];
for (int i = 1; i <=n; i++) {
a[i] = nextInt();
}
int []dp = new int [100001];
int []p = new int [100001];
int INF  = (int)1e8;
Arrays.fill(dp, INF);
dp[a[1]] = 0;
for (int i = 2; i <=n; i++) {
if (dp[a[i]] > dp[a[i-1]]+1){
dp[a[i]] = dp[a[i-1]]+1;
p[a[i]] = a[i-1];
}
}
int cur = a[n];
ArrayList<Integer> ans = new ArrayList<Integer>();
while (true){
if (cur == a[1]) break;
cur = p[cur];
}
Collections.reverse(ans);
for (int i = 0; i < ans.size(); i++) {
out.write(ans.get(i)+" ");
}
out.close();
}
private static int nextInt() throws IOException{
st.nextToken();
return (int) st.nval;
}

}