ACM-ICPC 2017 Asia Urumqi G. The Mountain

All as we know, a mountain is a large landform that stretches above the surrounding land in a limited area. If we as the tourists take a picture of a distant mountain and print it out, the image on the surface of paper will be in the shape of a particular polygon.

From mathematics angle we can describe the range of the mountain in the picture as a list of distinct points, denoted by (x_1,y_1)(x1,y1) to (x_n,y_n)(xn,yn). The first point is at the original point of the coordinate system and the last point is lying on the xx-axis. All points else have positive y coordinates and incremental xx coordinates. Specifically, all x coordinates satisfy 0 = x_1 < x_2 < x_3 < ... < x_n0=x1<x2<x3<...<xn. All yy coordinates are positive except the first and the last points whose yy coordinates are zeroes.

The range of the mountain is the polygon whose boundary passes through points (x_1,y_1)(x1,y1) to (x_n,y_n)(xn,yn) in turn and goes back to the first point. In this problem, your task is to calculate the area of the range of a mountain in the picture.

Input

The input has several test cases and the first line describes an integer t (1 \le t \le 20)t(1t20) which is the total number of cases.

In each case, the first line provides the integer n (1 \le n \le 100)n(1n100) which is the number of points used to describe the range of a mountain. Following nn lines describe all points and the ii-th line contains two integers x_ixi and y_i (0 \le x_i, y_i \le 1000)yi(0xi,yi1000) indicating the coordinate of the ii-th point.

Output

For each test case, output the area in a line with the precision of 66 digits.

样例输入

3
3
0 0
1 1
2 0
4
0 0
5 10
10 15
15 0
5
0 0
3 7
7 2
9 10
13 0

样例输出

1.000000
125.000000
60.500000

题目来源

ACM-ICPC 2017 Asia Urumqi

 

//根据题意首尾的两个点都在x轴上,因此分成前后两个三角形和中间若干个梯形即可。

复制代码
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <vector>
#include <queue>
#include <stack>
#include <cstdlib>
#include <iomanip>
#include <cmath>
#include <cassert>
#include <ctime>
#include <map>
#include <set>
using namespace std;
int n,t;
const int N=120;
struct Node{
    int x,y;
}nod[N];
int main()
{
    scanf("%d",&t);
    double ans;
    while(t--)
    {
        scanf("%d",&n);
        for(int i=0;i<n;i++) scanf("%d%d",&nod[i].x,&nod[i].y);
        ans=0;
        for(int i=1;i<n;i++){
            ans+=(nod[i-1].y+nod[i].y)*(nod[i].x-nod[i-1].x)/2.0;
        }
        printf("%.6f\n",ans);
    }
    return 0;
}
复制代码

 

posted on   cltt  阅读(394)  评论(0编辑  收藏  举报

编辑推荐:
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 25岁的心里话
· 按钮权限的设计及实现
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

导航

统计

点击右上角即可分享
微信分享提示