Advertisement
Guest User

checker

a guest
Apr 24th, 2018
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.58 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <vector>
  4. #include <string>
  5. #include <queue>
  6. using namespace std;
  7. #define int long long
  8.  
  9. struct Instruction{
  10.     int cx, cy, a;
  11. };
  12.  
  13. struct Car{
  14.     int x, y;
  15.     queue<Instruction> instructions;
  16.     int t;
  17. };
  18.  
  19. struct Person{
  20.     int t;
  21.     int sx, sy;
  22.     int fx, fy;
  23.     int picked;
  24.     int finished;
  25.     int car;
  26. };
  27.  
  28. int dist(int x, int y, int z, int w) {
  29.     return abs(x - z) + abs(y - w);
  30. }
  31.  
  32. void emulate(vector<Car>& cars, vector<Person>& persons, int time, long double& cost) {
  33.     for (auto& car : cars) {
  34.         while(!car.instructions.empty() && car.t < time) {
  35.             auto top_instruction = car.instructions.front();
  36.             int w = abs(top_instruction.cx - car.x) + abs(top_instruction.cy - car.y);
  37.             if (car.t + w <= time) {
  38.                 car.x = top_instruction.cx;
  39.                 car.y = top_instruction.cy;
  40.                 car.t += w;
  41.                 int a = top_instruction.a;
  42.                 if (a > 0) {
  43.                     if (persons[a - 1].picked > -1) {
  44.                         cerr << "you are trying to pick person #" << a << " once again!\n";  
  45.                     }
  46.                     persons[a - 1].picked = car.t;
  47.                 }
  48.                 if (a < 0) {
  49.                     auto person = persons[-a - 1];
  50.                     if (person.finished > -1) {
  51.                         cerr << "you are trying to finish person #" << -a << "once again!\n";
  52.                     }
  53.                     person.finished = car.t - dist(person.sx, person.sy, person.fx, person.fy);
  54.                     int d1 = 10000000ll;
  55.                     if (person.picked > -1) {
  56.                         d1 = person.picked - person.t;
  57.                     } else {
  58.                         cerr << "person #" << - a << "is trying to finish but it wasnt picked\n";
  59.                     }
  60.                     int d2 = person.finished - person.picked;
  61.                     cost += (1 - static_cast<long double>(min(10000000ll, d1*d1 + d2*d2)) / 10000000) *
  62.                         (100 + dist(person.sx, person.sy, person.fx, person.fy));
  63.                 }
  64.                 car.instructions.pop();
  65.             } else {
  66.                 int f_w = abs(top_instruction.cx - car.x);
  67.                 int s_w = abs(top_instruction.cy - car.y);
  68.                 if (car.t + f_w <= time) {
  69.                     car.t += f_w;
  70.                     car.x = top_instruction.cx;
  71.                     int remaining_time = time - car.t;
  72.                     if (top_instruction.cy > car.y) {
  73.                         car.y += remaining_time;
  74.                     } else {
  75.                         car.y -= remaining_time;
  76.                     }
  77.                     car.t = time;
  78.                 } else {
  79.                     int remaining_time = time - car.t;
  80.                     if (top_instruction.cx > car.x) {
  81.                         car.x += remaining_time;
  82.                     } else {
  83.                         car.x -= remaining_time;
  84.                     }
  85.                     car.t = time;
  86.                 }
  87.             }
  88.         }
  89.         car.t = time;
  90.     }
  91. }
  92.  
  93. int score(string input, string output) {
  94.     long double cost = 0.0;
  95.     ifstream in(input);
  96.     ifstream out(output);
  97.     int w, h;
  98.     in >> w >> h;
  99.     int cars_count;
  100.     in >> cars_count;
  101.     vector<Car> cars;
  102.     for (int i = 0; i < cars_count; ++i) {
  103.         int x, y;
  104.         in >> x >> y;
  105.         queue<Instruction> empt;
  106.         cars.push_back({x, y, empt, 0});
  107.     }
  108.     vector<Person> persons;
  109.     int turn = 0;
  110.     bool valid = true;
  111.     while(true) {
  112.             int f;
  113.             out >> f;
  114.             string empty = "   ";
  115.             string eempty = empty + empty;
  116.             for (int cur = 0; cur < f; ++cur) {
  117.                 int car, len;
  118.                 out >> car >> len;
  119.                 queue<Instruction> instructions;
  120.                 for (int j = 0; j < len; ++j) {
  121.                     int cx, cy, a;
  122.                     out >> cx >> cy >> a;
  123.                     instructions.push(Instruction{cx, cy, a});
  124.                 }
  125.                 cars[car - 1].instructions = instructions;
  126.             }
  127.  
  128.             if (!valid) {
  129.                 break;
  130.             }
  131.    
  132.             int t;
  133.             int sx, sy;
  134.             int fx, fy;
  135.             in >> t >> sx >> sy >> fx >> fy;
  136.             if (t == -1) {
  137.                 t = 1000000;
  138.             }
  139.             emulate(cars, persons, t, cost);
  140.             if (t == 1000000) {
  141.                 valid = false;
  142.                 continue;
  143.             }
  144.             persons.push_back({t, sx, sy, fx, fy, -1, -1, -1});
  145.     }
  146.     return int32_t(cost / persons.size());
  147. }
  148.  
  149. signed main() {
  150.     cout << score("tests/15", "results/15");
  151.     return 0;
  152. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement