Advertisement
erek1e

Gathers no Moss - BIO 2022 Round 2

Mar 28th, 2023
517
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.33 KB | None | 0 0
  1. #include <iostream>
  2. #include <set>
  3. #include <vector>
  4. #include <tuple>
  5. #include <algorithm>
  6.  
  7. using namespace std;
  8.  
  9. void setIO() {
  10.     #ifndef EREKLE
  11.         freopen("input.txt", "r", stdin);
  12.         freopen("output.txt", "w", stdout);
  13.     #endif
  14. }
  15.  
  16. struct Point {
  17.     int x, y;
  18.     Point(int x0, int y0): x(x0), y(y0){}
  19.     Point(){}
  20. };
  21.  
  22. typedef pair<Point, Point> Slope;
  23.  
  24. struct Command {
  25.     int x, type;
  26.     Slope slope;
  27.     bool operator < (const Command &c2) const {
  28.         return pair<int, int>(x, type) < pair<int, int>(c2.x, c2.type);
  29.     }
  30.     Command(int x2, int type2, Slope slope2): x(x2), type(type2), slope(slope2){}
  31. };
  32.  
  33. int main() {
  34.     setIO();
  35.     int x = 0; // x value used to compare slopes
  36.     auto higher = [&x](const Slope &s1, const Slope &s2) {
  37.         double y1 = s1.first.y + (double)(s1.second.y - s1.first.y) * (x-s1.first.x)/(s1.second.x-s1.first.x);
  38.         double y2 = s2.first.y + (double)(s2.second.y - s2.first.y) * (x-s2.first.x)/(s2.second.x-s2.first.x);
  39.         return y1 > y2;
  40.     };
  41.  
  42.     int n; cin >> n;
  43.     vector<Command> commands; // x, type (0=start, 1=finish), slope
  44.     Slope curSlope{{0, 0}, {0, 0}};
  45.     for (int i = 0; i < n; ++i) {
  46.         Slope s;
  47.         cin >> s.first.x >> s.first.y >> s.second.x >> s.second.y;
  48.         if (!s.first.x && !s.first.y) curSlope = s;
  49.         commands.emplace_back(s.first.x, 0, s);
  50.         commands.emplace_back(s.second.x, 1, s);
  51.     }
  52.     sort(commands.begin(), commands.end());
  53.  
  54.     int slopeCount = 1; // slopes traversed
  55.     set<Slope, decltype(higher)> activeSlopes(higher);
  56.     for (int ci = 0; ci < (int)commands.size(); ++ci) {
  57.         Command &c = commands[ci];
  58.         x = c.x;
  59.         if (c.type == 0) {
  60.             activeSlopes.insert(c.slope);
  61.         } else {
  62.             activeSlopes.erase(c.slope);
  63.             if (ci+1 == (int)commands.size() || commands[ci+1].x > c.x) {
  64.                 if (curSlope.second.x == c.x) {
  65.                     set<Slope, decltype(higher)>::iterator it = activeSlopes.upper_bound(curSlope);
  66.                     if (it == activeSlopes.end()) break;
  67.                     curSlope = *it;
  68.                     ++slopeCount;
  69.                 }
  70.             }
  71.         }
  72.     }
  73.     cout << slopeCount << endl;
  74.     cout << curSlope.second.x << ' ' << curSlope.second.y << endl;
  75.     return 0;
  76. }
  77.  
Tags: Bio
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement