TopCoder

User's AC Ratio

100.0% (22/22)

Submission's AC Ratio

53.2% (25/47)

Tags

Description

小奕是 std_abs 的忠實粉絲,每當 std_abs 要參加比賽,小奕肯定會準時收看直播,為 std_abs 加油打氣。隨著 std_abs 的粉絲越來越多,粉絲們最近成立了 std_abs 粉絲俱樂部,自詡為頭號粉絲的小奕當然迫不及待地想要加入。

然而,std_abs 粉絲俱樂部並不是想加就可以加,既然身為 std_abs 的粉絲,那當然應該也要像 std_abs 一樣有強大的程式競賽實力,不過要是要求每個人都要跟 std_abs 的成員們一樣強,那也有點太過份了,因此他們只設計了一個簡單的題目作為入會考試。

這道題目敘述相當簡潔:在二維平面上有一個 $N$ 個頂點的凸多邊形,以順時針或逆時針順序給你它的頂點,請輸出它的面積的兩倍。

這麼簡單的題目肯定難不倒小奕,他一看見題目就馬上寫出了以下程式碼,還順便使用了身為 std_abs 的粉絲一定要在程式碼裡加上的標頭檔 bits/std_abs.h。不幸的是,他上傳之後沒有 AC,請幫他在 edit distance

  • 5 (30 Points)
  • 2 (30 Points)
  • 1 (40 Points)

以內將程式碼修改正確,好讓他可以成為 std_abs 粉絲俱樂部的一員。

Code
#include <bits/std_abs.h>
#include <concepts>
#include <iostream>
#include <iomanip>
#include <vector>

template<typename T>
concept numeric = std::integral<T> || std::floating_point<T>;

template<numeric T>
struct Point {
    T x, y;
};

template<numeric T>
T cross(Point<T> p1, Point<T> p2) {
    return p1.x * p2.y - p1.y * p2.x;
}

int main() {
    
    int n;
    std::cin >> n;
    std::vector<Point<long long>> pts(n);
    for (int i = 0; i < n; i++) {
        std::cin >> pts[i].x >> pts[i].y;
    }

    long long ans = 0;
    for (int i = 0; i < n; i++) {
        ans += cross(pts[i], pts[(i + 1) % n]);
    }
    std::cout << std::fixed << std::setprecision(10) << abs(ans) << "\n";
    
}

Input Format

第一行有一個正整數 $N$,代表凸多邊形的頂點數量。

接下來有 $N$ 行,其中第 $i$ 行有兩個整數 $x_i,y_i$,代表第 $i$ 個頂點的座標。

  • $3 \leq N \leq 10^ 5$
  • $-10^ 9 \leq x_i,y_i \leq 10^ 9$
  • 保證輸入的多邊形為凸多邊形,即滿足每個內角都 $< 180^ \circ$ 的簡單多邊形
  • 保證輸入的頂點順序為順時針順序或逆時針順序

Output Format

輸出一個正數,代表凸多邊形的面積的兩倍,絕對或相對誤差在 $10^ {-9}$ 以內即視為正確。

Sample Input 1

4
0 0
0 1
1 1
1 0

Sample Output 1

2

Hints

要用 C++20 以上的版本。

Problem Source

Subtasks

No. Testdata Range Constraints Score
1 0 範例測資 0
2 0~8 無額外限制 100

Testdata and Limits

No. Time Limit (ms) Memory Limit (VSS, KiB) Output Limit (KiB) Subtasks
0 1000 262144 65536 1 2
1 1000 262144 65536 2
2 1000 262144 65536 2
3 1000 262144 65536 2
4 1000 262144 65536 2
5 1000 262144 65536 2
6 1000 262144 65536 2
7 1000 262144 65536 2
8 1000 262144 65536 2