Advertisement
Guest User

Untitled

a guest
Dec 19th, 2014
159
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.73 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.FileReader;
  3. import java.io.File;
  4. import java.io.IOException;
  5. import java.io.PrintWriter;
  6. import java.util.StringTokenizer;
  7. import java.util.HashMap;
  8. import java.util.ArrayList;
  9. //Отдельное присвоение для каждой переменной строчкой
  10. //Лучше все таки поссчитать
  11. class Lis
  12. {
  13.     int index;
  14.     long current;
  15.     long rival;
  16.     Lis next;
  17.     Lis(long current, long rival, int index)
  18.     {
  19.         this.current=current;
  20.         this.rival=rival;
  21.         this.index=index;
  22.         next=null;
  23.     }
  24. }
  25.  
  26. public class Task {
  27.     BufferedReader in;
  28.     PrintWriter out;
  29.     StringTokenizer st;
  30.     Lis insert(Lis now,long current,long rival, int index)
  31.     {  
  32.         now.next=new Lis(current,rival,index);
  33.         return now.next;
  34.     }    
  35.     public static void main(String[] args) throws IOException {
  36.         Task solver = new Task();
  37.         solver.open();
  38.         solver.solve();
  39.         solver.close();
  40.     }
  41.     private void solve() throws IOException {
  42.         int n=nextInt();
  43.         Lis list=new Lis(nextLong(),nextLong(),1);
  44.         Lis first=list;
  45.         long currentMax=0, rivalMax=0;
  46.         currentMax+=list.current;
  47.         rivalMax+=list.rival;
  48.         for(int i=1;i<n;i++)
  49.         {
  50.             list=insert(list,nextLong(),nextLong(),i+1);
  51.             currentMax+=list.current;
  52.             rivalMax+=list.rival;
  53.         }
  54.         if (currentMax>rivalMax)
  55.         {
  56.             out.print("0");
  57.             return;
  58.         }
  59.         else if (currentMax==rivalMax)
  60.         {
  61.             out.print("-1");
  62.             return;
  63.         }
  64.         int temp=0;
  65.         for(int i=0;i<n-1;i++)
  66.         {
  67.            if ((list.current>list.rival && list.next.current<list.next.rival) || (list.current<list.rival && list.next.current>list.next.rival))
  68.            {
  69.                if (list.current+list.next.current>list.rival+list.next.rival)
  70.                {      
  71.                  list.current=list.current+list.next.current;
  72.                  list.rival=list.rival+list.next.rival;
  73.                  list.next=list.next.next;
  74.                  temp++;
  75.                }
  76.            }else
  77.            if (list.current+list.next.current<list.rival+list.next.rival)
  78.            {
  79.                list.current=list.current+list.next.current;
  80.                list.rival=list.rival+list.next.rival;
  81.                
  82.                list.next=list.next.next;
  83.                temp++;
  84.            }
  85.         }
  86.         out.println(temp);
  87.         for(int i=0;i<temp;i++)
  88.         {
  89.            if (first.next.index-1!=first.index)
  90.            {
  91.                out.println(first.index+" "+(first.index+1));
  92.            }
  93.         }
  94.        
  95.        
  96.        
  97.  
  98.     }
  99.  
  100.     private long nextLong() throws IOException {
  101.         return Long.parseLong(nextToken());
  102.     }
  103.  
  104.     private void close() throws IOException {
  105.         in.close();
  106.         out.close();
  107.     }
  108.  
  109.     private void open() {
  110.         out = new PrintWriter(System.out);
  111.     }
  112.  
  113.     public int nextInt() throws IOException {
  114.         return Integer.parseInt(nextToken());
  115.     }
  116.  
  117.     private String nextToken() throws IOException {
  118.         while (st == null || !st.hasMoreTokens()) {
  119.             String str = in.readLine();
  120.             if (str == null) return null;
  121.             st = new StringTokenizer(str);
  122.  
  123.         }
  124.         return st.nextToken();
  125.     }
  126.  
  127.     private boolean hasMoreTokens() throws IOException {
  128.         while (st == null || !st.hasMoreTokens()) {
  129.             String str = in.readLine();
  130.             if (str == null) return false;
  131.             st = new StringTokenizer(str);
  132.  
  133.         }
  134.         return true;
  135.     }
  136. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement