Hướng dẫn giải của Tam giác


Chỉ dùng lời giải này khi không có ý tưởng, và đừng copy-paste code từ lời giải này. Hãy tôn trọng người ra đề và người viết lời giải.
Nộp một lời giải chính thức trước khi tự giải là một hành động có thể bị ban.

Lưu ý: Các code mẫu dưới đây chỉ mang tính tham khảo và có thể không AC được bài tập này

Code mẫu của flashmt

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <vector>
#include <cmath>
using namespace std;

bool cmp(pair <int,int> u, pair <int,int> v)
{
    return u.first * v.second < v.first * u.second;
}

long long calc(vector < pair<int,int> > a)
{
    long long sumX = 0, sumY = 0, res = 0;
    sort(a.begin(), a.end(), cmp);
    for (int i = 0; i < int(a.size()); i++)
    {
        res += sumY * a[i].first - sumX * a[i].second;
        sumX += a[i].first;
        sumY += a[i].second;
    }
    return res;
}

int main()
{
    int n, x[2222], y[2222];
    long long ans = 0;

    cin >> n;
    for (int i = 0; i < n; i++) cin >> x[i] >> y[i];

    for (int i = 0; i < n; i++)
    {
        vector < pair<int,int> > a;
        for (int j = 0; j < n; j++) 
        {
            if (i == j) continue;
            int xx = x[j] - x[i], yy = y[j] - y[i];
            if (yy < 0) yy = -yy, xx = -xx;
            a.push_back(make_pair(xx, yy));
        }
        ans += calc(a);
    }

    printf("%.1lf\n", ans / 6.0);
}

Code mẫu của ladpro98

#include <bits/stdc++.h>
#define X first
#define Y second

const int N = 1010;

using namespace std;

typedef pair<int, int> point;

point a[N], P[N];
int n;

bool incAngle(const point &a, const point &b)
    { return a.X * b.Y < a.Y * b.X; }

long double dist(const point &a)
    { return sqrt(a.X * a.X + a.Y * a.Y); }

struct helper {
    long double sumSin, sumCos;
    helper() { sumSin = sumCos = 0; }

    void addLineSegment(long double length, long double sine, long double cosine) {
        long double newSumSin = sumSin * cosine + sumCos * sine + length * sine;
        long double newSumCos = sumCos * cosine - sumSin * sine + length * cosine;
        sumSin = newSumSin; sumCos = newSumCos;
    }
};

long double getCos(point a, point b) {
    long double dotProduct = a.X * b.X + a.Y * b.Y;
    return dotProduct / (dist(a) * dist(b));
}

long double getSin(point a, point b) {
    long double crossProduct = abs(a.X * b.Y - a.Y * b.X);
    return crossProduct / (dist(a) * dist(b));
}

long double solve(int O) {
    long double ans = 0;
    int size = 0;
    for (int i = O + 1; i < n; ++i)
        P[size++] = point(a[i].X - a[O].X, a[i].Y - a[O].Y);
    sort(P, P + size, incAngle);
    helper H;
    for (int i = 1; i < size; ++i) {
        H.addLineSegment(dist(P[i - 1]), getSin(P[i - 1], P[i]), getCos(P[i - 1], P[i]));
        ans += dist(P[i]) * H.sumSin;
    }
    return ans;
}

int main() {
    ios::sync_with_stdio(0); cin.tie(0);
    cin >> n;
    for (int i = 0; i < n; ++i) cin >> a[i].X >> a[i].Y;
    long double ans = 0;
    sort(a, a + n);
    for (int i = 0; i < n; ++i) ans += solve(i);
    cout << setprecision(1) << fixed << ans / 2 << endl;
    return 0;
}

Code mẫu của RR

#include <iostream>
#include <algorithm>
#include <iomanip>
#include <cmath>
#define FOR(i,a,b)  for(int i=a; i<=b; i++)
#define FORD(i,a,b) for(int i=a; i>=b; i--)
#define MAXN 1011
#define P pair<int,int>
#define F first
#define S second
#define X first
#define Y second
using namespace std;

int n,sumx,sumy;
P a[MAXN];
pair< double,P > b[MAXN];

void inp() {
    scanf("%d",&n);
    FOR(i,1,n) {
        scanf("%d%d",&a[i].X,&a[i].Y);
        sumx+=a[i].X;
        sumy+=a[i].Y;
    }
}

inline double sqr(double a) {
    return a*a;
}
inline double dist(P a,P b) {
    return sqrt(sqr(a.X-b.X) + sqr(a.Y-b.Y));
}

double a1,b1,c1;
inline void find(P a, P b) {
    a1 = b.Y - a.Y;
    b1 = a.X - b.X;
    c1 = -a1*a.X - b1*a.Y;
}

void solve() {
    double res=0.0;
    FOR(i,1,n-2) {
        int m=0;
        FOR(j,i+1,n) {
            b[++m].F = atan2(a[j].X - a[i].X, a[j].S - a[i].S);
            b[m].S = a[j];
        }
        sort(b+1,b+m+1);

        sumx-=a[i].X;
        sumy-=a[i].Y;
        double sx=sumx, sy=sumy;

        FOR(j,1,m-1) {
            sx-=b[j].S.X;
            sy-=b[j].S.Y;
            find(a[i],b[j].S);
            res+=abs(sx*a1+sy*b1+(n-i-j)*c1)/sqrt(a1*a1+b1*b1)*dist(a[i],b[j].S);
        }
    }
    printf("%.1lf",res/2);
}

void init() {
    FOR(i,1,n)
    FOR(j,i+1,n)
        if (a[i].Y>a[j].Y || (a[i].Y==a[j].Y && a[i].X>a[j].X)) {
            P tmp = a[i];
            a[i] = a[j];
            a[j] = tmp;
        }
}

int main() {
    inp();
    init();
    solve();
    return 0;
}

Code mẫu của hieult

#include <set>
#include <map>
#include <list>
#include <cmath>
#include <queue>
#include <stack>
#include <cstdio>
#include <string>
#include <vector>
#include <cstdlib>
#include <cstring>
#include <sstream>
#include <iomanip>
#include <iostream>
#include <algorithm>
#include <ctime>
#include <deque>
#include <bitset>
#include <cctype>
#include <utility>

using namespace std;

typedef long long ll;
typedef long double ld;
typedef unsigned int ui;
typedef unsigned long long ull;

#define Rep(i,n) for(int i = 0; i < (n); ++i)
#define Repd(i,n) for(int i = (n)-1; i >= 0; --i)
#define For(i,a,b) for(int i = (a); i <= (b); ++i)
#define Ford(i,a,b) for(int i = (a); i >= (b); --i)
#define Fit(i,v) for(__typeof((v).begin()) i = (v).begin(); i != (v).end(); ++i)
#define Fitd(i,v) for(__typeof((v).rbegin()) i = (v).rbegin(); i != (v).rend(); ++i)
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define sz(a) ((int)(a).size())
#define all(a) (a).begin(), (a).end()
#define ms(a,x) memset(a, x, sizeof(a))

template<class F, class T> T convert(F a, int p = -1) {
    stringstream ss;
    if (p >= 0)
        ss << fixed << setprecision(p);
    ss << a;
    T r;
    ss >> r;
    return r;
}
template<class T> T gcd(T a, T b) {
    T r;
    while (b != 0) {
        r = a % b;
        a = b;
        b = r;
    }
    return a;
}
template<class T> T lcm(T a, T b) {
    return a / gcd(a, b) * b;
}
template<class T> T sqr(T x) {
    return x * x;
}
template<class T> T cube(T x) {
    return x * x * x;
}
template<class T> int getbit(T s, int i) {
    return (s >> i) & 1;
}
template<class T> T onbit(T s, int i) {
    return s | (T(1) << i);
}
template<class T> T offbit(T s, int i) {
    return s & (~(T(1) << i));
}
template<class T> int cntbit(T s) {
    return s == 0 ? 0 : cntbit(s >> 1) + (s & 1);
}

const int bfsz = 1 << 16;
char bf[bfsz + 5];
int rsz = 0;
int ptr = 0;
char gc() {
    if (rsz <= 0) {
        ptr = 0;
        rsz = (int) fread(bf, 1, bfsz, stdin);
        if (rsz <= 0)
            return EOF;
    }
    --rsz;
    return bf[ptr++];
}
void ga(char &c) {
    c = EOF;
    while (!isalpha(c))
        c = gc();
}
int gs(char s[]) {
    int l = 0;
    char c = gc();
    while (isspace(c))
        c = gc();
    while (c != EOF && !isspace(c)) {
        s[l++] = c;
        c = gc();
    }
    s[l] = '\0';
    return l;
}
template<class T> bool gi(T &v) {
    v = 0;
    char c = gc();
    while (c != EOF && c != '-' && !isdigit(c))
        c = gc();
    if (c == EOF)
        return false;
    bool neg = c == '-';
    if (neg)
        c = gc();
    while (isdigit(c)) {
        v = v * 10 + c - '0';
        c = gc();
    }
    if (neg)
        v = -v;
    return true;
}

typedef pair<int, int> II;
const ld PI = acos(-1.0);
const ld eps = 1e-9;
const int dr[] = { -1, 0, +1, 0, 1, 1, -1, -1 };
const int dc[] = { 0, +1, 0, -1, 1, -1, 1, -1 };
const int inf = (int) 1e9 + 5;
const ll linf = (ll) 1e16 + 5;
const int mod = (ll) (1e9 + 7 + eps);
#define ok puts("ok")
#define maxn 2000005

struct Point{
    int x, y;
    ld g;
    Point(){};
    Point(int _x, int _y){
        x = _x; y = _y;
        g = atan2(y, x);
    }
    Point(int _x, int _y, ld _g){
        x = _x; y = _y; g = _g;
    }

    bool operator <(const Point& P) const{
        return g < P.g;
    }
};

Point A[1005];
int n, N;
ll X, Y, res = 0;

ll go(int id){
    ll ret = 0;
    int x = A[id].x, y = A[id].y, x1, y1, n = N - 1;
    vector<Point> P;
    Rep(i, N) if(i != id) P.pb(Point(A[i].x - x, A[i].y - y));
    X = 0; Y = 0;
    Rep(i, n) {
        X += P[i].x;
        Y += P[i].y;
    }
    Rep(i, n) P.pb(Point(P[i].x, P[i].y, P[i].g + PI + PI));
    sort(all(P));
    int u = 0, numT;
    ll xT = 0, yT = 0;
    Rep(i, n){
        while(P[u].g <= P[i].g + PI){
            xT += P[u].x;
            yT += P[u].y;
            u++;
        }
        numT = u - i;
        x1 = P[i].x; y1 = P[i].y;
        ret += x1 * (2 * yT - Y) - y1 * (2 * xT - X);
        xT -= P[i].x;   yT -= P[i].y;

    }

    return abs(ret);
}

int main(){
//  freopen("in.txt", "r", stdin);

    cin >> N;
    Rep(i, N) cin >> A[i].x >> A[i].y;

    Rep(i, N) res += go(i);
    ld kq = res * 1.0 / 12;
    cout << fixed << setprecision(1) << kq << endl;

    return 0;
}

Code mẫu của ll931110

//#pragma comment(linker, "/STACK:16777216")
#include <algorithm>
#include <bitset>
#include <cmath>
#include <ctime>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <fstream>
#include <functional>
#include <iostream>
#include <map>
#include <set>
#include <sstream>
#include <stack>
#include <queue>
#include <vector>
#include <utility>
#define pi acos(-1.0)
using namespace std;

int n;
pair<int,int> p[1010];

double dist(int i,int j)
{
    return sqrt( (p[i].first - p[j].first) * (p[i].first - p[j].first) + (p[i].second - p[j].second) * (p[i].second - p[j].second) );
}

void line(int i,int j,long long &a,long long &b,long long &c)
{
    a = p[i].second - p[j].second;  b = p[j].first - p[i].first;  c = -(1LL * a * p[i].first + 1LL * b * p[i].second);
}

double pivot(int i)
{
    vector< pair<double,int> > v;
    for (int j = i + 1; j < n; j++)
    {
        double angle = atan2(1.0 * (p[j].second - p[i].second),1.0 * (p[j].first - p[i].first));        
        v.push_back(make_pair(angle,j));
        v.push_back(make_pair(angle + 2 * pi,j));
    }
    sort(v.begin(),v.end());
    int sz = v.size()/2;
    long long posx = 0,posy = 0,pos = 0,negx = 0,negy = 0,neg = 0;
    for (int j = 0; j < sz; j++) if (j && v[j].first < v[0].first + pi)
    {
        int u = v[j].second;  posx += p[u].first;  posy += p[u].second;  pos++;
    }
    else
    {
        int u = v[j].second;  negx += p[u].first;  negy += p[u].second;  neg++;
    }        
    int last = 0;
    while (v[last].first - (v[0].first + pi) < -1e-8) last++;

    long long a,b,c;
    line(i,v[0].second,a,b,c);
    double ans = (abs(1LL * a * posx + 1LL * b * posy + 1LL * c * pos) + abs(1LL * a * negx + 1LL * b * negy + 1LL * c * neg))/sqrt(1LL * a * a + 1LL * b * b)*dist(i,v[0].second)/4;
    for (int j = 1; j < sz; j++)
    {
        line(i,v[j].second,a,b,c);
        int u = v[j].second;
        posx -= p[u].first;  posy -= p[u].second;  pos--;  negx += p[u].first;  negy += p[u].second;  neg++;
        while (v[last].first - (v[j].first + pi) < -1e-8)
        {
            posx += p[u].first;  posy += p[u].second;  pos++;
            negx -= p[u].first;  negy -= p[u].second;  neg--;
            last++;
        }        
        ans += (abs(1LL * a * posx + 1LL * b * posy + 1LL * c * pos) + abs(1LL * a * negx + 1LL * b * negy + 1LL * c * neg))/sqrt(1LL * a * a + 1LL * b * b)*dist(i,v[j].second)/4;
    }
    return ans;
}

int main()
{
//    freopen("tro9a.in","r",stdin);
//    freopen("tri.ou","w",stdout);
    scanf("%d", &n);
    for (int i = 0; i < n; i++) scanf("%d %d", &p[i].first, &p[i].second);
    sort(p,p + n);
    double ret = 0.0;
    for (int i = 0; i < n - 1; i++) 
        ret += pivot(i);  
    printf("%.1lf\n", ret);
}

Code mẫu của khuc_tuan

#include <iostream>
#include <cmath>
using namespace std;

#define fi first
#define se second

int n, nb;
pair<int,int> a[1010];
pair<double, int> b[1010];
long double total = 0;
double dist[1010][1010];

int main() {
    scanf("%d", &n);
    for(int i=0;i<n;++i)
        scanf("%d%d", &a[i].fi, &a[i].se);
    sort( a, a + n);

    for(int i=0;i<n;++i)
        for(int j=i+1;j<n;++j)
            dist[i][j] = dist[j][i] = sqrt((a[i].fi-a[j].fi)*(a[i].fi-a[j].fi) + (a[i].se-a[j].se)*(a[i].se-a[j].se));

    //for(int i=0;i<n;++i) printf("%d %d\n", a[i].fi, a[i].se);

    for(int i=0;i<n;++i) {
        nb = 0;
        double tx = 0, ty = 0;
        for(int j=i+1;j<n;++j) {
            tx += a[j].fi;
            ty += a[j].se;
            b[nb].se = j;
            b[nb].fi = (a[j].se - a[i].se) / dist[i][j];
            ++nb;
        }
        sort( b, b + nb);

        /*printf("%d ", i);
        for(int j=0;j<nb;++j) printf("%d ", b[j].se);
        printf("\n");*/

        for(int j=0;j<nb;++j) {
            int id = b[j].se;
            tx -= a[id].fi;
            ty -= a[id].se;
            // Ax + By + C = 0
            double A = a[id].se - a[i].se;
            double B = a[i].fi - a[id].fi;
            double C = -(A * a[i].fi + B * a[i].se);
            total += abs(A * tx + B * ty + (nb - j - 1) * C) / sqrt(A * A + B * B) / 2 * dist[i][id];
            // printf("%d %d %f\n", i, id, abs(A * tx + B * ty + (nb - j - 1) * C) / sqrt(A * A + B * B) / 2 * dist[i][id]);
        }
    }
    cout.setf(ios::fixed | ios::showpoint);
    cout.precision(1);
    cout << total << endl;
    return 0;
}

Bình luận

Hãy đọc nội quy trước khi bình luận.


Không có bình luận tại thời điểm này.