Hướng dẫn giải của VM 08 Bài 24 - Hình thoi


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 <utility>
#include <cmath>
#define fr(a,b,c) for (a=b;a<=c;a++)
#define frr(a,b,c) for (a=b;a>=c;a--)
#define pii pair<int,int>
#define F first
#define S second
#define z 100
using namespace std;

int n,gcd[111][111],d[222][222],e[222][222];
vector< pii > a;
vector< pii > b[222][222],c[222][222];
long long re;

int main()
{
    int i,j,x,y,A,B;
    fr(i,1,100)
        fr(j,i,100)
            if (i==j) gcd[i][j]=i;
            else gcd[i][j]=gcd[j][i]=gcd[i][j-i];
    cin >> n;
    fr(i,1,n) scanf("%d%d",&x,&y), a.push_back(make_pair(x,y));
    fr(i,0,n-2)
        fr(j,i+1,n-1)
            if (a[i].F!=a[j].F || a[i].S!=a[j].S)
            {
                x=a[i].F+a[j].F+z;
                y=a[i].S+a[j].S+z;
                A=a[i].S-a[j].S;
                B=a[j].F-a[i].F;
                if (!A) d[x][y]++;
                else
                    if (!B) e[x][y]++;
                    else
                    {
                        int k=gcd[abs(A)][abs(B)];
                        A/=k; B/=k;
                        if (A<0) A=-A, B=-B;
                        if (B>0) b[x][y].push_back(make_pair(A,B));
                        else c[x][y].push_back(make_pair(-B,A));
                    }
            }
    fr(x,0,200)
        fr(y,0,200)
        {
            re+=1LL*d[x][y]*e[x][y];
            if (!b[x][y].empty() && !c[x][y].empty())
            {
                sort(b[x][y].begin(),b[x][y].end());
                sort(c[x][y].begin(),c[x][y].end());
                int B=b[x][y].size(),C=c[x][y].size();
                i=0; j=0;
                while (i<B && j<C)
                {
                    if (b[x][y][i]<c[x][y][j]) ++i;
                    else 
                        if (b[x][y][i]>c[x][y][j]) ++j;
                        else
                        {
                            int ii=i,jj=j;
                            while (b[x][y][ii]==b[x][y][i] && ii<B) ++ii;
                            while (c[x][y][jj]==c[x][y][j] && jj<C) ++jj;
                            re+=1LL*(ii-i)*(jj-j);
                            i=ii; j=jj;
                        }
                }
            }
        }
    cout << re << endl;
    return 0;
}

Code mẫu của RR

#include <sstream>
#include <iomanip>
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <vector>
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <string>
#include <deque>
#include <complex>

#define FOR(i,a,b) for(int i=(a),_b=(b); i<=_b; i++)
#define FORD(i,a,b) for(int i=(a),_b=(b); i>=_b; i--)
#define REP(i,a) for(int i=0,_a=(a); i<_a; i++)
#define FORN(i,a,b) for(int i=(a),_b=(b);i<_b;i++)
#define DOWN(i,a,b) for(int i=a,_b=(b);i>=_b;i--)
#define SET(a,v) memset(a,v,sizeof(a))
#define sqr(x) ((x)*(x))
#define ll long long
#define F first
#define S second
#define PB push_back
#define MP make_pair

#define DEBUG(x) cout << #x << " = "; cout << x << endl;
#define PR(a,n) cout << #a << " = "; FOR(_,1,n) cout << a[_] << ' '; cout << endl;
#define PR0(a,n) cout << #a << " = "; REP(_,n) cout << a[_] << ' '; cout << endl;
using namespace std;

//Buffer reading
int INP,AM,REACHEOF;
const int BUFSIZE = (1<<12) + 17;
char BUF[BUFSIZE+1], *inp=BUF;
#define GETCHAR(INP) { \
    if(!*inp && !REACHEOF) { \
        memset(BUF,0,sizeof BUF);\
        int inpzzz = fread(BUF,1,BUFSIZE,stdin);\
        if (inpzzz != BUFSIZE) REACHEOF = true;\
        inp=BUF; \
    } \
    INP=*inp++; \
}
#define DIG(a) (((a)>='0')&&((a)<='9'))
#define GN(j) { \
    AM=0;\
    GETCHAR(INP); while(!DIG(INP) && INP!='-') GETCHAR(INP);\
    if (INP=='-') {AM=1;GETCHAR(INP);} \
    j=INP-'0'; GETCHAR(INP); \
    while(DIG(INP)){j=10*j+(INP-'0');GETCHAR(INP);} \
    if (AM) j=-j;\
}
//End of buffer reading

const long double PI = acos((long double) -1.0);

struct Point {
    int x, y;

    Point(int x = 0, int y = 0) : x(x), y(y) {}

    Point operator + (Point a) { return Point(x+a.x, y+a.y); }
    Point operator - (Point a) { return Point(x-a.x, y-a.y); }
    Point operator * (int k) { return Point(x*k, y*k); }
    Point operator / (int k) { return Point(x/k, y/k); }

    int operator * (Point a) { return x*a.x + y*a.y; }

    void read() { scanf("%d%d", &x, &y); }
} a[1511];

vector<double> ls[222][222];

int nPoint, now, cnt[1511];
double val[1511];

int main() {
    scanf("%d", &nPoint);
    FOR(i,1,nPoint) {
        a[i].read();
        a[i] = a[i] * 2;
    }
    FOR(i,1,nPoint) FOR(j,i+1,nPoint) {
        Point O = (a[i] + a[j]) / 2;
        ls[O.x + 100][O.y + 100].push_back(atan2(a[i].x - O.x, a[i].y - O.y));
    }

    int res = 0;
    FOR(x,0,200) FOR(y,0,200) {
        int now = 0;
        REP(t,ls[x][y].size()) val[++now] = ls[x][y][t];
        sort(val+1, val+now+1);

        FOR(t,1,now) val[now+t] = val[t] + 2*M_PI;

        // PR(val, now);

        int j = 0; val[0] = -1000;
        FOR(t,1,now+now) {
            if (fabs(val[t] - val[j]) > 1e-6) {
                ++j;
                val[j] = val[t];
                cnt[j] = 1;
            }
            else ++cnt[j];
        }
        now = j;

        // PR(val, now);
        // PR(cnt, now);

        int t2 = 1;
        FOR(t,1,now) {
            if (val[t] > M_PI) break;

            while (t2 < now && val[t2] < val[t] + M_PI / 2 - 1e-6) ++t2;
            if (fabs(val[t2] - val[t] - M_PI / 2) < 1e-6) res += cnt[t] * cnt[t2];
        }
    }
    cout << res << endl;
    return 0;
}

Code mẫu của khuc_tuan

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

struct Point {
    int x, y;
    Point() {}
    Point(int x, int y) : x(x), y(y) {}
    Point add(Point p) { return Point(x+p.x,y+p.y); }
    Point sub(Point p) { return Point(x-p.x,y-p.y); }
    Point mul(int k) { return Point(x*k,y*k); }
    Point div(int k) { return Point(x/k,y/k); }
};

int n, result;
Point a[1505];
vector<Point> list[222][222];

bool cmp(Point a, Point b) {
    int t = a.x * (a.y - b.y) - a.y * (a.x - b.x);
    return t < 0;
}

void process(vector<Point> &v) {
    sort( v.begin(), v.end(), cmp);
    int j = 0, k = 0;
    for(int i=0;i<v.size();++i) {
        j >?= i + 1;
        k >?= i + 1;
        while(j<v.size() && v[j].x * v[i].x + v[j].y * v[i].y > 0) ++j;
        while(k<v.size() && v[k].x * v[i].x + v[k].y * v[i].y >= 0) ++k;
        result += k - j;
    }
}

int main() {
    scanf("%d", &n);
    for(int i=0;i<n;++i) scanf("%d%d", &a[i].x, &a[i].y), a[i] = a[i].mul(2);
    for(int i=0;i<n;++i)
        for(int j=i+1;j<n;++j) {
            Point mid = a[i].add(a[j]).div(2);
            Point v = a[i] .sub( mid );
            if(v.y<0) v = v.mul(-1);
            if(v.y==0 && v.x<0) v.x = -v.x;
            list[mid.x+110][mid.y+110].push_back(v);
        }
    for(int i=0;i<222;++i)
        for(int j=0;j<222;++j)
            process(list[i][j]);
    printf("%d\n", result);
    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.