Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- #include <vector>
- #include <string>
- #include <queue>
- using namespace std;
- #define int long long
- struct Instruction{
- int cx, cy, a;
- };
- struct Car{
- int x, y;
- queue<Instruction> instructions;
- int t;
- };
- struct Person{
- int t;
- int sx, sy;
- int fx, fy;
- int picked;
- int finished;
- int car;
- };
- int dist(int x, int y, int z, int w) {
- return abs(x - z) + abs(y - w);
- }
- void emulate(vector<Car>& cars, vector<Person>& persons, int time, long double& cost) {
- for (auto& car : cars) {
- while(!car.instructions.empty() && car.t < time) {
- auto top_instruction = car.instructions.front();
- int w = abs(top_instruction.cx - car.x) + abs(top_instruction.cy - car.y);
- if (car.t + w <= time) {
- car.x = top_instruction.cx;
- car.y = top_instruction.cy;
- car.t += w;
- int a = top_instruction.a;
- if (a > 0) {
- if (persons[a - 1].picked > -1) {
- cerr << "you are trying to pick person #" << a << " once again!\n";
- }
- persons[a - 1].picked = car.t;
- }
- if (a < 0) {
- auto person = persons[-a - 1];
- if (person.finished > -1) {
- cerr << "you are trying to finish person #" << -a << "once again!\n";
- }
- person.finished = car.t - dist(person.sx, person.sy, person.fx, person.fy);
- int d1 = 10000000ll;
- if (person.picked > -1) {
- d1 = person.picked - person.t;
- } else {
- cerr << "person #" << - a << "is trying to finish but it wasnt picked\n";
- }
- int d2 = person.finished - person.picked;
- cost += (1 - static_cast<long double>(min(10000000ll, d1*d1 + d2*d2)) / 10000000) *
- (100 + dist(person.sx, person.sy, person.fx, person.fy));
- }
- car.instructions.pop();
- } else {
- int f_w = abs(top_instruction.cx - car.x);
- int s_w = abs(top_instruction.cy - car.y);
- if (car.t + f_w <= time) {
- car.t += f_w;
- car.x = top_instruction.cx;
- int remaining_time = time - car.t;
- if (top_instruction.cy > car.y) {
- car.y += remaining_time;
- } else {
- car.y -= remaining_time;
- }
- car.t = time;
- } else {
- int remaining_time = time - car.t;
- if (top_instruction.cx > car.x) {
- car.x += remaining_time;
- } else {
- car.x -= remaining_time;
- }
- car.t = time;
- }
- }
- }
- car.t = time;
- }
- }
- int score(string input, string output) {
- long double cost = 0.0;
- ifstream in(input);
- ifstream out(output);
- int w, h;
- in >> w >> h;
- int cars_count;
- in >> cars_count;
- vector<Car> cars;
- for (int i = 0; i < cars_count; ++i) {
- int x, y;
- in >> x >> y;
- queue<Instruction> empt;
- cars.push_back({x, y, empt, 0});
- }
- vector<Person> persons;
- int turn = 0;
- bool valid = true;
- while(true) {
- int f;
- out >> f;
- string empty = " ";
- string eempty = empty + empty;
- for (int cur = 0; cur < f; ++cur) {
- int car, len;
- out >> car >> len;
- queue<Instruction> instructions;
- for (int j = 0; j < len; ++j) {
- int cx, cy, a;
- out >> cx >> cy >> a;
- instructions.push(Instruction{cx, cy, a});
- }
- cars[car - 1].instructions = instructions;
- }
- if (!valid) {
- break;
- }
- int t;
- int sx, sy;
- int fx, fy;
- in >> t >> sx >> sy >> fx >> fy;
- if (t == -1) {
- t = 1000000;
- }
- emulate(cars, persons, t, cost);
- if (t == 1000000) {
- valid = false;
- continue;
- }
- persons.push_back({t, sx, sy, fx, fy, -1, -1, -1});
- }
- return int32_t(cost / persons.size());
- }
- signed main() {
- cout << score("tests/15", "results/15");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement