ATSTNG

H. Гонка со временем

Jan 14th, 2017
208
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.79 KB | None | 0 0
  1. // Sad story about Ruby, Python and (N == 10**5) problems... =/
  2.  
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using System.IO;
  7.  
  8. namespace scripts_are_not_good_enof
  9. {
  10.     class JobDoneException: Exception {  }
  11.            
  12.     class Pupil: IComparable {
  13.         private static int encounter = 0;
  14.         public int house, speed, id;
  15.         public double reach_time;
  16.         public bool lifted;
  17.        
  18.         public Pupil(int house, int speed){
  19.             Pupil.encounter += 1;
  20.             this.id += encounter;
  21.            
  22.             this.house = house;
  23.             this.speed = speed;
  24.             this.reach_time = (double)house/speed;
  25.             this.lifted = false;
  26.         }
  27.        
  28.         public double cur_pos(double time){
  29.             return Math.Max(0, this.house - this.speed*time);
  30.         }
  31.        
  32.         public int CompareTo(Object other){
  33.             return (Math.Abs(this.reach_time - (other as Pupil).reach_time) < double.Epsilon) ? 0 :
  34.                    (this.reach_time > (other as Pupil).reach_time) ? -1 : 1;
  35.         }
  36.     }
  37.    
  38.     class Program
  39.     {  
  40.         public static double time = 0;
  41.         public static int our_speed;
  42.         public static List<String> ans;
  43.         public static List<Pupil> pupils;
  44.        
  45.         static String format_double(double n){
  46.             var res = n.ToString().Replace(',', '.');
  47.             if (res.IndexOf('.') == -1) res += ".0";
  48.             return res;
  49.         }
  50.        
  51.         static void DriveBy(Pupil pupil){
  52.             // route to meet given pupil and lift him to the school
  53.             double cur_pos = pupil.cur_pos(time);
  54.            
  55.             if (cur_pos < double.Epsilon || our_speed < pupil.speed)
  56.                 throw new JobDoneException();
  57.             var closing_speed = our_speed + pupil.speed;
  58.             ans.Add(String.Format("{0} {1}",
  59.                                   pupil.id,
  60.                                   format_double(cur_pos*our_speed/closing_speed)
  61.                                  ));
  62.             time += 2*cur_pos/closing_speed;
  63.             pupil.lifted = true;
  64.         }
  65.        
  66.         public static List<int> parse_int(String s){
  67.             return s.Split().Select(x => int.Parse(x)).ToList();
  68.         }
  69.        
  70.         public static void Main(string[] args)
  71.         {  
  72.             /*
  73.             var input = new StreamReader("in.txt");
  74.             var output = new StreamWriter("out.txt");
  75.             /*/
  76.             var input = new StreamReader("race.in");
  77.             var output = new StreamWriter("race.out");
  78.             //*/
  79.                
  80.             ans = new List<String>();
  81.             pupils = new List<Pupil>();
  82.            
  83.             var inp = parse_int(input.ReadLine());
  84.             int n = inp[0]; our_speed = inp[1];
  85.             for (int i = 0; i < n; i++){
  86.                 inp = parse_int(input.ReadLine());
  87.                 pupils.Add(new Pupil(inp[0], inp[1]));
  88.             }
  89.             pupils.Sort();
  90.            
  91.             try {
  92.                 for (int i = 0; i < pupils.Count(); i++) DriveBy(pupils[i]);
  93.             } catch (JobDoneException) { }
  94.            
  95.             output.WriteLine(format_double(
  96.                 Math.Max(time,
  97.                 (pupils.Select(ppl => ppl.lifted ? 0 : ppl.reach_time).ToList().Max() ))
  98.             ));
  99.             output.WriteLine( ans.Count() );
  100.             foreach (String line in ans) { output.WriteLine(line); }
  101.            
  102.             output.Close();
  103.         }
  104.        
  105.     }
  106. }
Advertisement
Add Comment
Please, Sign In to add comment