【BZOJ1132】Tro(叉积)

题意:平面上有N个点. 求出所有以这N个点为顶点的三角形的面积和

N<=3000 N个点的坐标,其值在[0,10000]

思路:按从左到右的预处理点排序

每次枚举最左点作为原点,把叉积从大到小排序

面积用叉积算,因为每次以最左的点作为原点,叉积一定都大于0

2S=xi*yj-yi*xj,xi和yi已经固定,只要维护xj和yj的后缀和就好

  1 #include<bits/stdc++.h>
  2 using namespace std;
  3 typedef long long ll;
  4 typedef unsigned int uint;
  5 typedef unsigned long long ull;
  6 typedef pair<int,int> PII;
  7 typedef pair<ll,ll> Pll;
  8 typedef vector<int> VI;
  9 typedef vector<PII> VII;
 10 //typedef pair<ll,ll>P;
 11 #define N  100100
 12 #define M  2000010
 13 #define fi first
 14 #define se second
 15 #define MP make_pair
 16 #define pb push_back
 17 #define pi acos(-1)
 18 #define mem(a,b) memset(a,b,sizeof(a))
 19 #define rep(i,a,b) for(int i=(int)a;i<=(int)b;i++)
 20 #define per(i,a,b) for(int i=(int)a;i>=(int)b;i--)
 21 #define lowbit(x) x&(-x)
 22 #define Rand (rand()*(1<<16)+rand())
 23 #define id(x) ((x)<=B?(x):m-n/(x)+1)
 24 #define ls p<<1
 25 #define rs p<<1|1
 26 
 27 const ll MOD=1e9+7,inv2=(MOD+1)/2;
 28       double eps=1e-6;
 29       int INF=1e9;
 30       int dx[4]={-1,1,0,0};
 31       int dy[4]={0,0,-1,1};
 32 
 33 
 34 struct P
 35 {
 36     ll x,y;
 37 }p[N],t[N];
 38 
 39 int n;
 40 
 41 ll operator*(P a,P b)
 42 {
 43     return a.x*b.y-a.y*b.x;
 44 }
 45 
 46 ll operator<(P a,P b)
 47 {
 48     return a.y<b.y||(a.y==b.y&&a.x<b.x);
 49 }
 50 
 51 bool cmp(P a,P b)
 52 {
 53     return a*b>0;
 54 }
 55 
 56 ll read()
 57 {
 58    ll v=0,f=1;
 59    char c=getchar();
 60    while(c<48||57<c) {if(c=='-') f=-1; c=getchar();}
 61    while(48<=c&&c<=57) v=(v<<3)+v+v+c-48,c=getchar();
 62    return v*f;
 63 }
 64 
 65 
 66 void solve()
 67 {
 68     sort(p+1,p+n+1);
 69     ll ans=0;
 70     rep(i,1,n-2)
 71     {
 72         int m=0;
 73         ll sx=0,sy=0;
 74         rep(j,i+1,n)
 75         {
 76             m++;
 77             t[m].x=p[j].x-p[i].x;
 78             t[m].y=p[j].y-p[i].y;
 79         }
 80         sort(t+1,t+m+1,cmp);
 81         rep(j,1,m)
 82         {
 83             sx+=t[j].x;
 84             sy+=t[j].y;
 85         }
 86         rep(j,1,m)
 87         {
 88             sx-=t[j].x;
 89             sy-=t[j].y;
 90             ans+=t[j].x*sy-t[j].y*sx;
 91         }
 92     }
 93     if(ans%2==1) printf("%lld.5\n",ans/2);
 94      else printf("%lld.0\n",ans/2);
 95 }
 96 
 97 int main()
 98 {
 99     n=read();
100     rep(i,1,n) p[i].x=read(),p[i].y=read();
101     solve();
102     return 0;
103 }

 

posted on 2019-10-22 15:48  myx12345  阅读(178)  评论(0编辑  收藏  举报

导航