1. 程式人生 > >ACM-ICPC 2017 Asia Urumqi G. The Mountain

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(1≤t≤20) which is the total number of cases.

In each case, the first line provides the integer n (1 \le n \le 100)n(1≤n≤100) 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​(0≤xi​,yi​≤1000) 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
#include<iostream>  
#include<cstdio>  
#include<cmath>  
#include<cstring>  
#include<algorithm>  
#include<queue>  
#include<set>  
#include<vector>  
using namespace std;  
int n;
typedef struct Node{
	int x,y;
};
Node node[1000+10];
double chaji(Node n0,Node n1,Node n2){
	return (n1.x-n0.x)*(n2.y-n0.y)-(n2.x-n0.x)*(n1.y-n0.y);
}
double dis(Node n1,Node n2){  
    return sqrt((n2.x-n1.x)*(n2.x-n1.x)+(n2.y-n1.y)*(n2.y-n1.y));  
}  
bool cmp(Node n1,Node n2){  
    double fz=chaji(node[0],n1,n2);  
    if(fz>0) return 1;  
    if(fz<0) return 0;  
    return dis(node[0],n1)<dis(node[0],n2);  
} 
double mianji(int i){
	Node n0=node[i],n1=node[i+1],n2=node[i+2];
	double sum=chaji(n0,n1,n2);
	if(sum>0) return sum/2;
	else return sum/-2;
}
int main(){ 
	int n;
	int t;
	cin>>t;
	for(int i=0;i<t;i++){
	cin>>n;
	int index=0;
	for(int i=0;i<n;i++) {
		scanf("%d%d",&node[i].x,&node[i].y);
	}
	//sort(node,node+n,cmp);
	double sum=0;
	for(int i=2;i<n;i++){
		sum+=chaji(node[0],node[i-1],node[i]);
	}
	printf("%.6lf\n",fabs(sum/2));
}
	return 0;
}

歡迎大家加入 早起學習群,一起學習一起進步!(群號:642179511)