Advertisement
Guest User

Taxi.cpp

a guest
Jun 30th, 2013
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.37 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <vector>
  4. #include <algorithm>
  5. #include <functional>
  6.  
  7. using namespace std;
  8.  
  9. long cars(vector<long long> x, long long m, long long d)
  10. {
  11.     sort(x.begin(), x.end(), greater<long long>());
  12.    
  13.     // find the worst car that can get us from taxipark to airport
  14.     long lcar = -1;
  15.     for (long i = 0; i < x.size() && x[i] >= m - d; i++)
  16.     {
  17.         lcar = i;
  18.     }
  19.    
  20.     // exit if there is no such car
  21.     if (lcar == -1)
  22.     {
  23.         return 0;
  24.     }
  25.  
  26.     long min = 0;
  27.     long long p = 0;   
  28.     for (long i = 0; i < x.size() && p < d; i++)
  29.     {
  30.         // check if we can get to the airport right now using lcar     
  31.         if (p + x[lcar] - (d - p) >= m && m > d)
  32.         {
  33.             return min + 1;
  34.         }
  35.  
  36.         if (i != lcar || m == d)
  37.         {
  38.             // check if car[i] can actually reach us
  39.             if (x[i] <= d - p) return 0;               
  40.  
  41.             p = p + x[i] - (d - p);
  42.             ++min;
  43.         }
  44.     }
  45.  
  46.     // if we are already in the airport
  47.     if (p >= m)
  48.     {      
  49.         return min;
  50.     }
  51.  
  52.     // if we got to the taxipark, use lcar to get to the airport
  53.     if (p >= d)
  54.     {
  55.         return min + 1;
  56.     }
  57.  
  58.     return 0;
  59. }
  60.  
  61.  
  62. // not working for some reason :(
  63. int main()
  64. {  
  65.     freopen("taxi.in", "r", stdin);
  66.     freopen("taxi.out", "w", stdout);  
  67.    
  68.     long long m, d;
  69.     long n;
  70.     cin >> m >> d >> n;
  71.     vector<long long> x(n);
  72.     for (long long i = 0; i < n; i++)
  73.     {
  74.         cin >> x[i];
  75.     }  
  76.  
  77.     long c = cars(x, m, d);
  78.     cout << c;
  79.  
  80.     return 0;
  81. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement