Submission #1280052

#TimeUsernameProblemLanguageResultExecution timeMemory
1280052knhatdevPlanine (COCI21_planine)C++20
110 / 110
162 ms23932 KiB
#include<bits/stdc++.h> #define int long long #define ii pair<int,int> #define dd pair<double, double> #define fi first #define se second #define task "" using namespace std; const int N = 1e6 + 5, oo = 1e18; #define EPS 0.00001 struct point2d{ int x, y; point2d() {} point2d(int x, int y): x(x), y(y) {} point2d operator + (point2d t){ return point2d(x + t.x, y + t.y); } point2d operator - (point2d t){ return point2d(x - t.x, y - t.y); } }; struct point{ double x, y; point() {} point(int x, int y): x(x), y(y) {} point operator + (point t){ return point(x + t.x, y + t.y); } point operator - (point t){ return point(x - t.x, y - t.y); } }; struct line2d{ int a, b, c; line2d(int a, int b, int c): a(a), b(b), c(c) {} line2d(point2d &u, point2d &v) : a(u.y - v.y), b(v.x - u.x), c(u.x * v.y - v.x * u.y) {}; }; int cross(point2d a, point2d b){ //tich co huong return a.x * b.y - a.y * b.x; } int dot(point2d a, point2d b){ // tich vo huong return a.x * b.x + a.y * b.y; } double abs(point2d a) { return sqrt(a.x * a.x + a.y * a.y); } double angle(point2d a, point2d b){ // goc giua 2 vector return acos(dot(a, b) / abs(a) / abs(b)); } point get_intersect(line2d x, line2d y) { int d = x.a * y.b - x.b * y.a; if(d == 0) return {oo, oo}; //Neu d == 0: 2 duong song song hoac trung nhau point t; t.x = (double)(x.b * y.c - y.b * x.c) / d; t.y = (double)(y.a * x.c - x.a * y.c) / d; return t; } int ccw(point2d a, point2d b, point2d c) { int t = cross(b - a, c - b); if (t == 0) return 0; if (t < 0) return -1; else return 1; } int n; int h; point2d a[N]; dd b[N]; main(){ ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); if(fopen(task ".inp", "r")){ freopen(task ".inp", "r", stdin); freopen(task ".out", "w", stdout); } cin >> n >> h; for(int i = 1; i <= n; i++) { cin >> a[i].x >> a[i].y; } line2d tmp(0, 1, -h); int m = 0; vector<int> v; v.push_back(1); v.push_back(2); for(int i = 3; i < n; i++) { while(v.size() >= 2 && ccw(a[v[v.size() - 2]], a[v[v.size() - 1]], a[i]) >= 0) { v.pop_back(); } v.push_back(i); // for(int x : v) cout << x << ' '; // cout << '\n'; if(i % 2 == 1) { // cout << a[i].x << ' ' << a[i].y << "; " << a[v[v.size() - 2]].x << ' ' << a[v[v.size() - 2]].y << '\n'; point x = get_intersect(line2d(a[i], a[v[v.size() - 2]]), tmp); b[++m].fi = x.x; } // cout << b[m].fi << ' ' << b[m].se << '\n'; } int j = m; v.clear(); v.push_back(n); v.push_back(n - 1); for(int i = n - 2; i >= 3; i--) { while(v.size() >= 2 && ccw(a[v[v.size() - 2]], a[v[v.size() - 1]], a[i]) <= 0) { v.pop_back(); } v.push_back(i); if(i % 2 == 1) { point x = get_intersect(line2d(a[i], a[v[v.size() - 2]]), tmp); b[j--].se = x.x; } } // for(int i = 1; i <= m; i++) // cout << b[i].fi << ' ' << b[i].se << '\n'; sort(b + 1, b + 1 + m, [](dd x, dd y) { return (x.se < y.se) || (x.se == y.se && x.fi < y.fi); }); double last_pos = -1e18; int ans = 0; for(int i = 1; i <= m; i++) { if(b[i].fi - EPS > last_pos) { ans++; last_pos = b[i].se; } } cout << ans; }

Compilation message (stderr)

Main.cpp:77:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
   77 | main(){
      | ^~~~
Main.cpp: In function 'int main()':
Main.cpp:80:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   80 |         freopen(task ".inp", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
Main.cpp:81:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   81 |         freopen(task ".out", "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...