Advertisement
Guest User

Sockets and Plugs

a guest
Sep 22nd, 2014
199
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.84 KB | None | 0 0
  1. import java.awt.Point;
  2. import java.io.*;
  3. import java.lang.reflect.Array;
  4. import java.math.BigInteger;
  5. import java.text.DecimalFormat;
  6. import java.util.*;
  7.  
  8. import javax.naming.spi.DirStateFactory.Result;
  9. import javax.security.auth.kerberos.KerberosKey;
  10. import javax.tools.JavaFileObject.Kind;
  11.  
  12. import static java.lang.Math.*;
  13.  
  14. import java.math.BigInteger;
  15. import java.util.Random;
  16.  
  17. public class Main {
  18.  
  19.     final boolean ONLINE_JUDGE = System.getProperty("ONLINE_JUDGE") != null;
  20.     // final boolean ONLINE_JUDGE = true;
  21.     BufferedReader in;
  22.     PrintWriter out;
  23.     StringTokenizer tok = new StringTokenizer("");
  24.  
  25.     void init() throws FileNotFoundException {
  26.         if (ONLINE_JUDGE) {
  27.             in = new BufferedReader(new InputStreamReader(System.in));
  28.             out = new PrintWriter(System.out);
  29.         } else {
  30.             in = new BufferedReader(new FileReader("input.txt"));
  31.             out = new PrintWriter("output.txt");
  32.         }
  33.     }
  34.  
  35.     String readString() throws IOException {
  36.         while (!tok.hasMoreTokens()) {
  37.             tok = new StringTokenizer(in.readLine());
  38.         }
  39.         return tok.nextToken();
  40.     }
  41.  
  42.     int readInt() throws IOException {
  43.         return Integer.parseInt(readString());
  44.     }
  45.  
  46.     int gcd(int a, int b) {
  47.         if (b == 0)
  48.             return a;
  49.         else
  50.             return gcd(b, a % b);
  51.     }
  52.  
  53.     int fib(int n) {
  54.         if (n == 1)
  55.             return 1;
  56.         if (n == 2)
  57.             return 2;
  58.         else
  59.             return fib(n - 1) + fib(n - 2);
  60.     }
  61.  
  62.     long readLong() throws IOException {
  63.         return Long.parseLong(readString());
  64.     }
  65.  
  66.     double readDouble() throws IOException {
  67.         return Double.parseDouble(readString());
  68.     }
  69.  
  70.     public static void main(String[] args) {
  71.         new Main().run();
  72.         // Sworn to fight and die
  73.     }
  74.  
  75.     public static void mergeSort(int[] a) {
  76.         mergeSort(a, 0, a.length - 1);
  77.     }
  78.  
  79.     private static void mergeSort(int[] a, int levtIndex, int rightIndex) {
  80.         final int MAGIC_VALUE = 50;
  81.         if (levtIndex < rightIndex) {
  82.             if (rightIndex - levtIndex <= MAGIC_VALUE) {
  83.                 insertionSort(a, levtIndex, rightIndex);
  84.             } else {
  85.                 int middleIndex = (levtIndex + rightIndex) / 2;
  86.                 mergeSort(a, levtIndex, middleIndex);
  87.                 mergeSort(a, middleIndex + 1, rightIndex);
  88.                 merge(a, levtIndex, middleIndex, rightIndex);
  89.             }
  90.         }
  91.     }
  92.  
  93.     private static void merge(int[] a, int levtIndex, int middleIndex,
  94.             int rightIndex) {
  95.         int length1 = middleIndex - levtIndex + 1;
  96.         int length2 = rightIndex - middleIndex;
  97.         int[] levtArray = new int[length1];
  98.         int[] rightArray = new int[length2];
  99.         System.arraycopy(a, levtIndex, levtArray, 0, length1);
  100.         System.arraycopy(a, middleIndex + 1, rightArray, 0, length2);
  101.         for (int k = levtIndex, i = 0, j = 0; k <= rightIndex; k++) {
  102.             if (i == length1) {
  103.                 a[k] = rightArray[j++];
  104.             } else if (j == length2) {
  105.                 a[k] = levtArray[i++];
  106.             } else {
  107.                 a[k] = levtArray[i] <= rightArray[j] ? levtArray[i++]
  108.                         : rightArray[j++];
  109.             }
  110.         }
  111.     }
  112.  
  113.     private static void insertionSort(int[] a, int levtIndex, int rightIndex) {
  114.         for (int i = levtIndex + 1; i <= rightIndex; i++) {
  115.             int current = a[i];
  116.             int j = i - 1;
  117.             while (j >= levtIndex && a[j] > current) {
  118.                 a[j + 1] = a[j];
  119.                 j--;
  120.             }
  121.             a[j + 1] = current;
  122.         }
  123.     }
  124.  
  125.     public void run() {
  126.         try {
  127.             long t1 = System.currentTimeMillis();
  128.             init();
  129.             solve();
  130.             out.close();
  131.             long t2 = System.currentTimeMillis();
  132.             System.err.println("Time = " + (t2 - t1));
  133.         } catch (Exception e) {
  134.             e.printStackTrace(System.err);
  135.             System.exit(-1);
  136.         }
  137.     }
  138.  
  139.     class LOL implements Comparable<LOL> {
  140.         int x;
  141.         int y;
  142.         int k;
  143.  
  144.         public LOL(int x, int y, int k) {
  145.             this.x = x;
  146.             this.y = y;
  147.             this.k = k;
  148.         }
  149.  
  150.         @Override
  151.         public int compareTo(LOL o) {
  152.  
  153.             // if (y == o.y)
  154.             // return (o.x - x);
  155.             return (int) (x - o.x); // ---->
  156.             // return o.x - x; // <----
  157.             // return o.y-y;
  158.         }
  159.  
  160.     }
  161.  
  162.     public void solve() throws IOException   {
  163.         int n = readInt();
  164.         int a = readInt();
  165.         int b = readInt();
  166.        
  167.        
  168.        
  169.         ArrayList<LOL> first = new ArrayList<LOL>();
  170.         ArrayList<LOL> second = new ArrayList<LOL>();
  171.         ArrayList<LOL> third = new ArrayList<LOL>();
  172.        
  173.         for (int i = 0; i < n; i++) {
  174.             int key = readInt();
  175.             if (key == 1) first.add(new LOL(readInt(), i + 1,1));
  176.             if (key == 2) second.add(new LOL(readInt(), i + 1,2));
  177.             if (key == 3) third.add(new LOL(readInt(), i + 1,3));
  178.         }
  179.        
  180.         Collections.sort(first);
  181.         Collections.sort(second);
  182.         Collections.sort(third);
  183.        
  184.         ArrayList<LOL> solution = new ArrayList<LOL>();
  185.        
  186.         for (int i = 0; (i < a) && (i < first.size()); i++) {
  187.             solution.add(first.get(i));
  188.         }
  189.        
  190.         int freePlaces = a - first.size();
  191.         if (freePlaces > 0) {
  192.             int i = 0;
  193.             while ((freePlaces > 0) && (third.size() > 0)) {
  194.                 third.get(i).k = 1;
  195.                 solution.add(third.get(i));
  196.                 third.remove(i);
  197.                 //i++;
  198.                 freePlaces--;
  199.             }
  200.         }
  201.        
  202.         for (int i = 0; (i < b) && (i < second.size()); i++) {
  203.             solution.add(second.get(i));
  204.         }
  205.        
  206.         freePlaces = b - second.size();
  207.         if (freePlaces > 0) {
  208.             int i = 0;
  209.             while ((freePlaces > 0) && (third.size() > 0)) {
  210.                 third.get(i).k = 2;
  211.                 solution.add(third.get(i));
  212.                 third.remove(i);
  213.                 freePlaces--;
  214.                 //i++;
  215.             }
  216.         }
  217.        
  218.         Collections.sort(solution);
  219.        
  220.         for (int i = solution.size() - 1, j = 0; (i >= 0) && (third.size() > 0); i--) {
  221.             if (solution.get(i).x >= third.get(j).x) {
  222.                 solution.get(i).x = third.get(j).x;
  223.                 solution.get(i).y = third.get(j).y;
  224.                 third.remove(j);
  225.             }
  226.         }
  227.        
  228.         int ans = 0;
  229.        
  230.         for (int i = 0; i < solution.size(); i++) {
  231.             ans += solution.get(i).x;
  232.         }
  233.         out.println(solution.size() + " " + ans);
  234.         int num = 1;
  235.         for (int i = 0; i < solution.size(); i++) {
  236.             if (solution.get(i).k == 1) {
  237.                 out.println(solution.get(i).y + " " + num);
  238.                 num++;
  239.             }
  240.         }
  241.         num = a + 1;
  242.         for (int i = 0; i < solution.size(); i++) {
  243.             if (solution.get(i).k == 2) {
  244.                 out.println(solution.get(i).y + " " + num);
  245.                 num++;
  246.             }
  247.         }
  248.        
  249.     }
  250. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement