Submission #696262

#TimeUsernameProblemLanguageResultExecution timeMemory
696262nicolexxuuLongest beautiful sequence (IZhO17_subsequence)Java
0 / 100
388 ms174692 KiB
import java.util.*; import java.io.*; public class subsequence { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(br.readLine()); int n = Integer.parseInt(st.nextToken()); int[] a = new int[n], k = new int[n]; st = new StringTokenizer(br.readLine()); for(int i = 0; i < n; i++) a[i] = Integer.parseInt(st.nextToken()); st = new StringTokenizer(br.readLine()); for(int i = 0; i < n; i++) k[i] = Integer.parseInt(st.nextToken()); br.close(); int[][][] dp1 = new int[1<<10][1<<10][11]; int[][][] dp2 = new int[1<<10][1<<10][11]; int[][] res = new int[n][2]; for(int i = 0; i < n; i++) { int par = -1, ans = 0; int l = a[i] >> 10; int r = a[i] - (l << 10); for(int j = 0; j < (1 << 10); j++) { // possible prev lefts int bLeft = k[i] - Integer.bitCount(l&j); if(bLeft >= 0 && bLeft <= 10 ) { if(dp1[l][r][bLeft] > ans) { ans = dp1[l][r][bLeft]; par = dp2[l][r][bLeft]; } } } res[i] = new int[] {ans+1, par}; for(int j = 0; j < (1 << 10); j++) { int b = Integer.bitCount(j&r); if(ans+1 > dp1[l][j][b]) { dp1[l][j][b] = ans+1; dp2[l][j][b] = i; } } } int idx = 0; for(int i = 0; i < n; i++) idx = (res[i][0] > res[idx][0]) ? i : idx; System.out.println(res[idx][0]); ArrayDeque<Integer> l = new ArrayDeque<>(); while(idx != -1) { l.add(idx); idx = res[idx][1]; } StringBuilder sb = new StringBuilder(""); while(!l.isEmpty()) sb.append(l.pollLast()+1).append(" "); System.out.println(sb); } } /* 4 1 2 3 4 10 0 1 0 5 5 3 5 3 5 10 1 20 1 20 */
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...