Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.*;
- import java.security.SecureRandom;
- import java.util.*;
- public class CMain {
- private QuickReader in;
- private PrintWriter out;
- public CMain(QuickReader in, PrintWriter out) {
- this.in = in;
- this.out = out;
- }
- public static void main(String[] args) {
- QuickReader in = new QuickReader(System.in);
- try(PrintWriter out = new PrintWriter(new BufferedOutputStream(System.out), true))
- {
- new CMain(in, out).solve();
- }
- }
- public void solve()
- {
- int n = in.nextInt();
- int[] res = new int[2*n];
- int top = 0;
- TreeSet<Integer> c0, c1, c2;
- c0 = new TreeSet<>();
- c1 = new TreeSet<>();
- c2 = new TreeSet<>();
- for(int i=1;i<=n;i++)
- c0.add(i);
- int last = -1;
- for(int i=0;i<2*(2*n-1);i++)
- {
- int x = in.nextInt()-1;
- if(res[x] == 0)
- {
- if( last == -1 )
- {
- if(c0.isEmpty())
- {
- res[x] = c1.first();
- c1.remove(res[x]);
- c2.add(res[x]);
- }
- else
- {
- res[x] = c0.first();
- c0.remove(res[x]);
- c1.add(res[x]);
- }
- last = res[x];
- }
- else
- {
- if(c1.isEmpty() || (c1.size() == 1 && c1.first() == last))
- {
- if(!c0.isEmpty())
- {
- res[x] = c0.first();
- c0.remove(res[x]);
- c1.add(res[x]);
- }
- else
- {
- res[x] = c1.first();
- c1.remove(res[x]);
- c2.add(res[x]);
- }
- }
- else
- {
- res[x] = c1.first();
- if(res[x] == last)
- res[x] = c1.last();
- c1.remove(res[x]);
- c2.add(res[x]);
- }
- last = -1;
- }
- }
- out.println(res[x]);
- }
- }
- }
- class QuickReader
- {
- BufferedReader in;
- StringTokenizer token;
- public QuickReader(InputStream ins)
- {
- in=new BufferedReader(new InputStreamReader(ins));
- token=new StringTokenizer("");
- }
- public boolean hasNext()
- {
- while (!token.hasMoreTokens())
- {
- try
- {
- String s = in.readLine();
- if (s == null) return false;
- token = new StringTokenizer(s);
- } catch (IOException e)
- {
- throw new InputMismatchException();
- }
- }
- return true;
- }
- public String next()
- {
- hasNext();
- return token.nextToken();
- }
- public int nextInt()
- {
- return Integer.parseInt(next());
- }
- public int[] nextInts(int n)
- {
- int[] res = new int[n];
- for (int i = 0; i < n; i++)
- res[i] = nextInt();
- return res;
- }
- public long nextLong() {
- return Long.parseLong(next());
- }
- public long[] nextLongs(int n)
- {
- long[] res = new long[n];
- for (int i = 0; i < n; i++)
- res[i] = nextLong();
- return res;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment