#include<bits/stdc++.h>
using namespace std;
#define X first
#define Y second
#define int long long
int crossproduct(pair <int, int> a, pair <int, int> b, pair <int, int> c)
{
return (b.X- a.X) * (c.Y - a.Y) - (b.Y - a.Y) * (c.X - a.X);
}
int n, h;
vector <pair <int, int> > funct(vector <pair <int, int> > x)
{
vector <pair <int, int> > res(n / 2 - 1);
vector <pair <int, int> > st;
for(int i = 1; i < n - 1; i++)
{
while(st.size() > 1 && crossproduct(st[st.size() - 2], st.back(), x[i]) >= 0)
{
st.pop_back();
}
st.push_back(x[i]);
if(!(i & 1))
{
auto [x1, y1] = st.rbegin()[0];
auto [x2, y2] = st.rbegin()[1];
/*int x1 = st[st.size() - 2].X;
int y1 = st[st.size() - 2].Y;
int x2 = x[i].X;
int y2 = x[i].Y;*/
int x0 = (x1 - x2) * (h - y2) + x2 * (y1 - y2);
int y0 = y1 - y2;
// cout << st.size() << "\n";
res[i / 2 - 1] = {x0, y1 - y2};
}
}
return res;
}
bool cmp(pair <pair <int, int>, pair <int, int> > a, pair <pair <int, int>, pair <int, int> > b)
{
if(a.X.X * b.X.Y == a.X.Y * b.X.X)
{
return a.Y.X < b.Y.X;
}
return a.X.X * b.X.Y < a.X.Y * b.X.X;
}
signed main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> n >> h;
vector <pair <int, int> > x(n);
for(int i = 0; i < n; i++)
{
cin >> x[i].X >> x[i].Y;
}
vector <pair <int, int> > a = funct(x);
reverse(x.begin(), x.end());
for(int i = 0; i < n; i++)
{
x[i].X *= -1;
}
vector <pair <int, int> > b = funct(x);
reverse(b.begin(), b.end());
for(int i = 0; i < b.size(); i++)
{
b[i].X *= -1;
}
int ans = 0;
vector <pair <pair <int, int>, pair <int, int> > > scan;
for(int i = 0; i < n / 2 - 1; i++)
{
scan.push_back({a[i], {-1, i}});
scan.push_back({b[i], {1, i}});
}
sort(scan.begin(), scan.end(), cmp);
set <int> s;
for(int i = 0; i < scan.size(); i++)
{
int t = scan[i].Y.X;
if(t == -1)
{
s.insert(scan[i].Y.Y);
}
else if(s.find(scan[i].Y.Y) != s.end())
{
ans++;
s.clear();
}
}
cout << ans;
return 0;
}
Compilation message
Main.cpp: In function 'std::vector<std::pair<long long int, long long int> > funct(std::vector<std::pair<long long int, long long int> >)':
Main.cpp:31:17: warning: unused variable 'y0' [-Wunused-variable]
31 | int y0 = y1 - y2;
| ^~
Main.cpp: In function 'int main()':
Main.cpp:65:22: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
65 | for(int i = 0; i < b.size(); i++)
| ~~^~~~~~~~~~
Main.cpp:78:22: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::pair<std::pair<long long int, long long int>, std::pair<long long int, long long int> > >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
78 | for(int i = 0; i < scan.size(); i++)
| ~~^~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
1484 KB |
Output is correct |
2 |
Correct |
5 ms |
1484 KB |
Output is correct |
3 |
Correct |
5 ms |
1484 KB |
Output is correct |
4 |
Correct |
44 ms |
10192 KB |
Output is correct |
5 |
Correct |
43 ms |
10192 KB |
Output is correct |
6 |
Correct |
47 ms |
10192 KB |
Output is correct |
7 |
Correct |
493 ms |
88388 KB |
Output is correct |
8 |
Correct |
479 ms |
88388 KB |
Output is correct |
9 |
Correct |
495 ms |
88328 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
492 KB |
Output is correct |
2 |
Correct |
1 ms |
492 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
2 ms |
492 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
492 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
1484 KB |
Output is correct |
2 |
Correct |
5 ms |
1484 KB |
Output is correct |
3 |
Correct |
5 ms |
1484 KB |
Output is correct |
4 |
Correct |
44 ms |
10192 KB |
Output is correct |
5 |
Correct |
43 ms |
10192 KB |
Output is correct |
6 |
Correct |
47 ms |
10192 KB |
Output is correct |
7 |
Correct |
493 ms |
88388 KB |
Output is correct |
8 |
Correct |
479 ms |
88388 KB |
Output is correct |
9 |
Correct |
495 ms |
88328 KB |
Output is correct |
10 |
Correct |
2 ms |
492 KB |
Output is correct |
11 |
Correct |
1 ms |
492 KB |
Output is correct |
12 |
Correct |
1 ms |
364 KB |
Output is correct |
13 |
Correct |
2 ms |
492 KB |
Output is correct |
14 |
Correct |
1 ms |
364 KB |
Output is correct |
15 |
Correct |
1 ms |
492 KB |
Output is correct |
16 |
Correct |
1 ms |
364 KB |
Output is correct |
17 |
Correct |
463 ms |
88388 KB |
Output is correct |
18 |
Correct |
474 ms |
88360 KB |
Output is correct |
19 |
Correct |
44 ms |
10192 KB |
Output is correct |
20 |
Correct |
474 ms |
88388 KB |
Output is correct |
21 |
Correct |
48 ms |
10192 KB |
Output is correct |
22 |
Correct |
468 ms |
88388 KB |
Output is correct |
23 |
Correct |
1 ms |
364 KB |
Output is correct |
24 |
Correct |
493 ms |
88388 KB |
Output is correct |
25 |
Correct |
44 ms |
10192 KB |
Output is correct |
26 |
Correct |
682 ms |
88388 KB |
Output is correct |
27 |
Correct |
23 ms |
5212 KB |
Output is correct |