列表

详情


NC232543. [POI2008]Tro

描述

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

输入描述

第一行给出数字 N ,N 下面 N 行给出 N 个点的坐标,其值在
输入都是整数

输出描述

保留一位小数,误差不超过0.1

示例1

输入:

5
0 0
1 2
0 2
1 0
1 1

输出:

7.0

原站题解

上次编辑到这里,代码来自缓存 点击恢复默认模板

C++(g++ 7.5.0) 解法, 执行用时: 328ms, 内存消耗: 640K, 提交时间: 2022-10-19 17:13:11

#include<bits/stdc++.h>
using namespace std;

using ll = long long;

constexpr ll eps = 0;

struct point{
	ll x,y;
	friend point operator +(const point &lhs, const point &rhs) {
		return {lhs.x+rhs.x, lhs.y+rhs.y};
	}
	friend point operator -(const point &lhs, const point &rhs) {
		return {lhs.x-rhs.x, lhs.y-rhs.y};
	}
	friend point operator *(const ll &k, const point &t){
		return {k*t.x, k*t.y};
	}
    friend ll operator *(const point &lhs, const point & rhs) {
    	return lhs.x*rhs.y - lhs.y*rhs.x;
    }
};

void solve(){
    int n; cin >> n;
    vector<point>a(n);
    for(auto &[x,y]: a) cin >> x >> y;

    auto solve = [&](const point &p)->ll{
    	vector<point>tmp;
    	for(auto &q: a){
    		if(q.y>p.y || (q.y==p.y && q.x > p.x)) tmp.push_back(q-p);
    	}
    	
    	sort(tmp.begin(), tmp.end(), [](const point &a, const point &b){
    		return a * b < 0;
    	});
    	ll res = 0;
    	point sumq = {0,0};

    	for(auto &q: tmp){
    		res += q * sumq;
    		sumq = sumq + q;
    	}

    	return res;
    };

    ll ans = 0;
    for(auto &p: a) ans += solve(p);

    cout << ans/2 << (ans%2==1?".5":".0") << endl;
}

int main()
{
	ios::sync_with_stdio(false);
	cin.tie(0);

    solve();

	return 0;
}

C++ 解法, 执行用时: 320ms, 内存消耗: 552K, 提交时间: 2022-04-09 21:10:27

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int> Pair;
const int maxn=3e3+5;
const int mod=1e9+7;
struct Point{
    ll x,y;
}p[maxn],t[maxn];
ll sumx[maxn],sumy[maxn];
bool cmp(Point p1,Point p2){
    if(p1.x==p2.x)return p1.y<p2.y;
    return p1.x<p2.x;
}
bool cmp1(Point p1,Point p2){
    return p2.y*p1.x>p2.x*p1.y;
}
Point solve(Point p1,Point p2){
    return Point{p1.x-p2.x,p1.y-p2.y};
}
int main(){
    int n;
    scanf("%d",&n);
    for (int i = 1; i <= n; ++i) {
        scanf("%lld %lld",&p[i].x,&p[i].y);
    }
    sort(p+1,p+1+n,cmp);
    ll ans=0;
    for (int i = 1; i <= n; ++i) {
        for (int j = i+1; j <= n; ++j) t[j]=solve(p[j],p[i]);
        sort(t+1+i,t+1+n,cmp1);
        for (int j = n; j > i; --j) {
            sumx[j]=sumx[j+1]+t[j].x;
            sumy[j]=sumy[j+1]+t[j].y;
            ans+=t[j].x*sumy[j+1]-t[j].y*sumx[j+1];
        }
    }
    printf("%lld.%lld\n",ans/2,ans%2*5);
}

上一题