knightL

SNWS C

Feb 5th, 2022
808
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.63 KB | None | 0 0
  1. import java.io.*;
  2. import java.security.SecureRandom;
  3. import java.util.*;
  4.  
  5. public class CMain {
  6.  
  7.     private QuickReader in;
  8.     private PrintWriter out;
  9.  
  10.     public CMain(QuickReader in, PrintWriter out) {
  11.         this.in = in;
  12.         this.out = out;
  13.     }
  14.  
  15.     public static void main(String[] args) {
  16.         QuickReader in = new QuickReader(System.in);
  17.        
  18.         try(PrintWriter out = new PrintWriter(new BufferedOutputStream(System.out), true))
  19.         {
  20.             new CMain(in, out).solve();
  21.         }
  22.     }
  23.    
  24.     public void solve()
  25.     {
  26.         int n = in.nextInt();
  27.        
  28.         int[] res = new int[2*n];
  29.         int top = 0;
  30.        
  31.         TreeSet<Integer> c0, c1, c2;
  32.         c0 = new TreeSet<>();
  33.         c1 = new TreeSet<>();
  34.         c2 = new TreeSet<>();
  35.        
  36.         for(int i=1;i<=n;i++)
  37.             c0.add(i);
  38.        
  39.         int last = -1;
  40.        
  41.         for(int i=0;i<2*(2*n-1);i++)
  42.         {
  43.             int x = in.nextInt()-1;
  44.             if(res[x] == 0)
  45.             {
  46.                 if( last == -1 )
  47.                 {
  48.                     if(c0.isEmpty())
  49.                     {
  50.                         res[x] = c1.first();
  51.                         c1.remove(res[x]);
  52.                         c2.add(res[x]);
  53.                     }
  54.                     else
  55.                     {
  56.                         res[x] = c0.first();
  57.                         c0.remove(res[x]);
  58.                         c1.add(res[x]);
  59.                     }
  60.                     last = res[x];
  61.                 }
  62.                 else
  63.                 {
  64.                     if(c1.isEmpty() || (c1.size() == 1 && c1.first() == last))
  65.                     {
  66.                         if(!c0.isEmpty())
  67.                         {
  68.                             res[x] = c0.first();
  69.                             c0.remove(res[x]);
  70.                             c1.add(res[x]);
  71.                         }
  72.                         else
  73.                         {
  74.                             res[x] = c1.first();
  75.                             c1.remove(res[x]);
  76.                             c2.add(res[x]);
  77.                         }
  78.                     }
  79.                     else
  80.                     {
  81.                         res[x] = c1.first();
  82.                         if(res[x] == last)
  83.                             res[x] = c1.last();
  84.                         c1.remove(res[x]);
  85.                         c2.add(res[x]);
  86.                     }
  87.                     last = -1;
  88.                 }
  89.                
  90.             }
  91.             out.println(res[x]);
  92.         }
  93.     }
  94.  
  95. }
  96.  
  97. class QuickReader
  98. {
  99.     BufferedReader in;
  100.     StringTokenizer token;
  101.    
  102.     public QuickReader(InputStream ins)
  103.     {
  104.         in=new BufferedReader(new InputStreamReader(ins));
  105.         token=new StringTokenizer("");
  106.     }
  107.  
  108.     public boolean hasNext()
  109.     {
  110.         while (!token.hasMoreTokens())
  111.         {
  112.             try
  113.             {
  114.                 String s = in.readLine();
  115.                 if (s == null) return false;
  116.                 token = new StringTokenizer(s);
  117.             } catch (IOException e)
  118.             {
  119.                 throw new InputMismatchException();
  120.             }
  121.         }
  122.         return true;
  123.     }
  124.  
  125.     public String next()
  126.     {
  127.         hasNext();
  128.         return token.nextToken();
  129.     }
  130.  
  131.     public int nextInt()
  132.     {
  133.         return Integer.parseInt(next());
  134.     }
  135.  
  136.     public int[] nextInts(int n)
  137.     {
  138.         int[] res = new int[n];
  139.         for (int i = 0; i < n; i++)
  140.             res[i] = nextInt();
  141.         return res;
  142.     }
  143.  
  144.     public long nextLong() {
  145.         return Long.parseLong(next());
  146.     }
  147.    
  148.     public long[] nextLongs(int n)
  149.     {
  150.         long[] res = new long[n];
  151.         for (int i = 0; i < n; i++)
  152.             res[i] = nextLong();
  153.         return res;
  154.     }
  155.    
  156. }
  157.  
  158.  
Advertisement
Add Comment
Please, Sign In to add comment