[POI2008]Tro
Time Limit: 20 Sec Memory Limit: 162 MBSubmit: 1796 Solved: 604
[Submit][Status][Discuss]
Description
平面上有N个点. 求出所有以这N个点为顶点的三角形的面积和 N<=3000
Input
第一行给出数字N,N在[3,3000] 下面N行给出N个点的坐标,其值在[0,10000]
Output
保留一位小数,误差不超过0.1
Sample Input
5
0 0
1 2
0 2
1 0
1 1
0 0
1 2
0 2
1 0
1 1
Sample Output
7.0
HINT
枚举起点,然后求出以该点为起点的所有向量,然后求面积就可以了。
1 #include<cstring> 2 #include<cmath> 3 #include<cstdio> 4 #include<iostream> 5 #include<algorithm> 6 7 #define N 3007 8 #define ll long long 9 using namespace std; 10 inline int read() 11 { 12 int x=0,f=1;char ch=getchar(); 13 while(!isdigit(ch)){if(ch=='-')f=-1;ch=getchar();} 14 while(isdigit(ch)){x=(x<<1)+(x<<3)+ch-'0';ch=getchar();} 15 return x*f; 16 } 17 18 int n;ll ans; 19 struct Node 20 { 21 int x,y; 22 friend inline ll operator*(Node x,Node y) 23 { 24 return x.x*y.y-x.y*y.x; 25 } 26 friend inline bool operator<(Node x,Node y) 27 { 28 if (x.y==y.y) return x.x<y.x; 29 return x.y<y.y; 30 } 31 }a[N],b[N]; 32 bool cmp(Node x,Node y) 33 { 34 return x*y>0; 35 } 36 37 void solve() 38 { 39 sort(a+1,a+n+1); 40 for (int i=1;i<=n-2;i++) 41 { 42 int tot=0;ll sumx=0,sumy=0; 43 for (int j=i+1;j<=n;j++) 44 b[++tot].x=a[j].x-a[i].x, 45 b[tot].y=a[j].y-a[i].y; 46 sort(b+1,b+tot+1,cmp); 47 for (int j=1;j<=tot;j++) 48 sumx+=b[j].x, 49 sumy+=b[j].y; 50 for (int j=1;j<=tot;j++) 51 { 52 sumx-=b[j].x; 53 sumy-=b[j].y; 54 ans+=(ll)b[j].x*sumy-b[j].y*sumx; 55 } 56 } 57 } 58 int main() 59 { 60 n=read(); 61 for (int i=1;i<=n;i++) 62 a[i].x=read(),a[i].y=read(); 63 solve(); 64 if (ans&1) printf("%lld.5",ans/2); 65 else printf("%lld.0",ans/2); 66 } 67 #undef ll