Advertisement
Guest User

Untitled

a guest
Jun 13th, 2014
254
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.93 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.IOException;
  3. import java.io.InputStreamReader;
  4. import java.io.PrintWriter;
  5. import java.util.Comparator;
  6. import java.util.StringTokenizer;
  7. import java.util.*;
  8.  
  9. public class Cardboard_va implements Runnable {
  10.     public static void main(String[] args) {
  11.         new Thread(new Cardboard_va()).run();
  12.     }
  13.  
  14.     BufferedReader br;
  15.     StringTokenizer in;
  16.     PrintWriter out;
  17.  
  18.     public String nextToken() throws IOException {
  19.         while (in == null || !in.hasMoreTokens()) {
  20.             in = new StringTokenizer(br.readLine());
  21.         }
  22.  
  23.         return in.nextToken();
  24.     }
  25.  
  26.     public int nextInt() throws IOException {
  27.         return Integer.parseInt(nextToken());
  28.     }
  29.  
  30.     public long nextLong() throws IOException {
  31.         return Long.parseLong(nextToken());
  32.     }
  33.  
  34.     public double nextDouble() throws IOException {
  35.         return Double.parseDouble(nextToken());
  36.     }
  37.  
  38.     public class Level {
  39.         int a, b, ind;
  40.  
  41.         public Level(int a, int b, int ind) {
  42.             this.a = a;
  43.             this.b = b;
  44.             this.ind = ind;
  45.         }
  46.     }
  47.  
  48.     public class LevelComparator implements Comparator<Level> {
  49.  
  50.         @Override
  51.         public int compare(Level o1, Level o2) {
  52.             return o1.b == o2.b ? o1.a - o2.a : o1.b - o2.b;
  53.         }
  54.     }
  55.  
  56.     public class Level1Comparator implements Comparator<Level> {
  57.  
  58.         @Override
  59.         public int compare(Level o1, Level o2) {
  60.             return o1.a - o2.a;
  61.         }
  62.     }
  63.  
  64.     public class LevelDiffComparator implements Comparator<Level> {
  65.         public int compare(Level o1, Level o2) {
  66.             return o1.b - o1.a - (o2.b - o2.a);
  67.         }
  68.     }
  69.  
  70.     Random rnd = new Random(29);
  71.  
  72.     public class Node {
  73.         int x, id, y, size;
  74.         long sum;
  75.         Node l, r;
  76.  
  77.         public Node(int x, int id) {
  78.             this.x = x;
  79.             this.id = id;
  80.             y = rnd.nextInt();
  81.             size = 1;
  82.             sum = x;
  83.         }
  84.  
  85.         public String toString() {
  86.             return x + " " + id + " " + size + " " + sum + " [ Left: " + l + ", Right: " + r + "]";
  87.         }
  88.     }
  89.  
  90.  
  91.     public int size(Node n) {
  92.         return n == null ? 0 : n.size;
  93.     }
  94.  
  95.     public long sum(Node n) {
  96.         return n == null ? 0 : n.sum;
  97.     }
  98.  
  99.     public void update(Node n) {
  100.         n.size = size(n.l) + size(n.r) + 1;
  101.         n.sum = sum(n.l) + sum(n.r) + n.x;
  102.     }
  103.  
  104.     public Node[] split(Node n, int x, int id) {
  105.         if (n == null) {
  106.             return new Node[]{null, null};
  107.         }
  108.  
  109.         if (n.x < x || (n.x == x && n.id < id)) {
  110.             Node[] t = split(n.r, x, id);
  111.             n.r = t[0];
  112.             update(n);
  113.             return new Node[]{n, t[1]};
  114.         } else {
  115.             Node[] t = split(n.l, x, id);
  116.             n.l = t[1];
  117.             update(n);
  118.             return new Node[]{t[0], n};
  119.         }
  120.     }
  121.  
  122.     public Node merge(Node l, Node r) {
  123.         if (l == null)
  124.             return r;
  125.         if (r == null)
  126.             return l;
  127.         if (l.y < r.y) {
  128.             l.r = merge(l.r, r);
  129.             update(l);
  130.             return l;
  131.         } else {
  132.             r.l = merge(l, r.l);
  133.             update(r);
  134.             return r;
  135.         }
  136.     }
  137.  
  138.     public long sum(Node n, int k) {
  139.         if (k == 0) {
  140.             return 0;
  141.         }
  142.  
  143.         if (size(n.l) >= k) {
  144.             return sum(n.l, k);
  145.         }
  146.         long ans = sum(n.l);
  147.         ans += n.x;
  148.         return ans + sum(n.r, k - size(n.l) - 1);
  149.     }
  150.  
  151.     public Node delete(Node n, int x, int id) {
  152.         Node[] s1 = split(n, x, id);
  153.         Node[] s2 = split(s1[1], x, id + 1);
  154.         return merge(s1[0], s2[1]);
  155.     }
  156.  
  157.     public Node add(Node n, int x, int id) {
  158.         Node add = new Node(x, id);
  159.         Node[] s = split(n, x, id);
  160.         return merge(s[0], merge(add, s[1]));
  161.     }
  162.  
  163.     public void solve() throws IOException {
  164.         int n = nextInt();
  165.         int w = nextInt();
  166.  
  167.         Level[] a = new Level[n];
  168.         for (int i = 0; i < n; i++) {
  169.             a[i] = new Level(nextInt(), nextInt(), i);
  170.         }
  171.  
  172.         Arrays.sort(a, new LevelComparator());
  173.  
  174.         Node root = null;
  175.  
  176.         int bd = -1;
  177.         long ans = Long.MAX_VALUE;
  178.         long total = 0;
  179.         for (int i = 0; i < w - n; i++) {
  180.             total += a[i].a;
  181.             root = add(root, a[i].b - a[i].a, a[i].ind);
  182.         }
  183.  
  184.         for (int i = Math.max(w - n, 0); i < n; i++) {
  185.             root = add(root, a[i].a, a[i].ind);
  186.         }
  187.  
  188.         for (int s = Math.max(w - n, 0); s <= Math.min(w, n); s++) {
  189.             long cnt = total + sum(root, w - s);
  190.  
  191.             if (cnt < ans) {
  192.                 ans = cnt;
  193.                 bd = s;
  194.             }
  195.  
  196.             if (s < n) {
  197.                 total += a[s].a;
  198.                 root = delete(root, a[s].a, a[s].ind);
  199.                 root = add(root, a[s].b - a[s].a, a[s].ind);
  200.             }
  201.         }
  202.  
  203.         if (bd == -1) {
  204.             throw new AssertionError();
  205.         }
  206.  
  207.         int[] r = new int[n];
  208.         Level[] b = new Level[n];
  209.  
  210.         for (int i = 0; i < b.length; i++) {
  211.             if (i < bd) {
  212.                 b[i] = new Level(a[i].b - a[i].a, 0, a[i].ind);
  213.                 r[a[i].ind]++;
  214.             } else
  215.                 b[i] = a[i];
  216.         }
  217.  
  218.         Arrays.sort(b, new Level1Comparator());
  219.         for (int i = 0; i < w - bd; i++) {
  220.             r[b[i].ind]++;
  221.         }
  222.  
  223.         out.println(ans);
  224.         for (int i = 0; i < n; i++) {
  225.             out.print(r[i]);
  226.         }
  227.     }
  228.  
  229.     public void run() {
  230.         try {
  231.             br = new BufferedReader(new InputStreamReader(System.in));
  232.             out = new PrintWriter(System.out);
  233.  
  234.             solve();
  235.  
  236.             out.close();
  237.         } catch (IOException e) {
  238.             e.printStackTrace();
  239.             System.exit(1);
  240.         }
  241.     }
  242. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement