题目 | One Fourth
AtCoder Beginner Contest 250
F - One Fourth
https://atcoder.jp/contests/abc250/tasks/abc250_f
Time Limit: 2 sec / Memory Limit: 1024 MB
Score :
Problem Statement
ABC 250 is a commemorable quarter milestone for Takahashi, who aims to hold ABC 1000, so he is going to celebrate this contest by eating as close to
The pizza that Takahashi bought has a planar shape of convex
Takahashi has decided to cut and eat the pizza as follows.
- First, Takahashi chooses two non-adjacent vertices from the vertices of the pizza and makes a cut with a knife along the line passing through those two points, dividing the pizza into two pieces.
- Then, he chooses one of the pieces at his choice and eats it.
Let aa be the quarter (
Constraints
- All values in input are integers.
- The given points are the vertices of a convex
-gon in the counterclockwise order.
Input
Input is given from Standard Input in the following format:
Output
Print the answer as an integer.
Sample Input 1
5
3 0
2 3
-1 3
-3 1
-1 -1
Sample Output 1
1
Suppose that he makes a cut along the line passing through the
Then,
Sample Input 2
4
400000000 400000000
-400000000 400000000
-400000000 -400000000
400000000 -400000000
Sample Output 2
1280000000000000000
Sample Input 3
6
-816 222
-801 -757
-165 -411
733 131
835 711
-374 979
Sample Output 3
157889
我的笔记
法:TLE x20
思路:
在
求法:
包含
三角形面积:已知
代码:
#include <bits/stdc++.h>
using namespace std;
const int SIZE = 1e5 + 10;
int N;
pair<int, int> points[SIZE];
inline long long areax2(pair<int, int> a, pair<int, int> b, pair<int, int> c)
{
return abs(1ll * (c.first - a.first) * (b.second - a.second) - 1ll * (b.first - a.first) * (c.second - a.second));
}
int main()
{
cin >> N;
for (int i = 0; i < N; i++)
cin >> points[i].first >> points[i].second;
long long ax8 = 0;
for (int i = 1; i < N - 1; i++)
{
ax8 += areax2(points[0], points[i], points[i + 1]);
}
long long ans = INT64_MAX;
// START
for (int i = 0; i < N; i++)
{
for (int j = 0; j < N; j++)
{
if (abs(i - j) <= 1 || abs(i - j) == N - 1)
continue;
long long bx2 = 0;
for (int k = (i + 1) % N; k != j; k = (k + 1) % N)
{
bx2 += areax2(points[i], points[k], points[(k + 1) % N]);
}
ans = min(ans, abs(ax8 - 4 * bx2));
}
}
// END
cout << ans << endl;
return 0;
}
法:TLE x11
上面的方法是从多边形分割为小三角形计算,同一个小三角形的面积重复计算了很多次。我们可以用小三角形的面积来组成多边形,可以减少很多次计算。
思路:
在
如下图,若取
求法:
取
取模很重要!只要是
代码:
// 将此代码替换到方法一代码的START注释与END注释之间
for (int i = 0; i < N; i++)
{
long long bx2 = 0;
for (int j = (i + 1) % N, t = 0; t < N - 2; j = (j + 1) % N, t++)
{
bx2 += areax2(points[i], points[j], points[(j + 1) % N]);
ans = min(ans, abs(ax8 - 4 * bx2));
}
}
法:AC
思路:
既然题目要求
求法:
代码:
// 将此代码替换到方法一代码的START注释与END注释之间
long long bx2 = 0;
int j = 1;
for (int i = 0; i < N; i++)
{
while (bx2 * 4 < ax8)
{
bx2 += areax2(points[i], points[j], points[(j + 1) % N]);
j = (j + 1) % N;
ans = min(ans, abs(ax8 - 4 * bx2));
}
bx2 -= areax2(points[i], points[(i + 1) % N], points[j]);
ans = min(ans, abs(ax8 - 4 * bx2));
}
本文采用 CC BY-SA 4.0 许可,本文 Markdown 源码:Haotian-BiJi