Hướng dẫn giải của VOI 14 Bài 4 - Trò Chơi Với Những Viên Bi


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 happyboy99x

#include<algorithm>
#include<cassert>
#include<complex>
#include<vector>
#include<cstdio>
using namespace std;

typedef complex<long long> Point;

long long cross(const Point &a, const Point &b) {
    return imag(conj(a) * b);
}

struct Ball {
    Point pos;
    int color, id;

    Ball(const Point &_pos, int _color, int _id):
        pos (_pos), color (_color), id (_id) {}

    bool operator < (const Ball &b) const {
        long long crs = cross(pos, b.pos);
        return crs == 0 ? norm(pos) < norm(b.pos) : crs > 0;
    }

    bool isBad() const {
        return imag(pos) < 0 || (imag(pos) == 0 && real(pos) <= 0);
    }
};

int main() {
    vector<Ball> balls;
    int n; scanf("%d", &n);
    for(int i = 0; i < 2 * n; ++i) {
        int x, y; scanf("%d %d", &x, &y);
        balls.push_back(Ball(Point(x, y), i < n ? 0 : 1, i));
    }
    n *= 2;
    for(int root = 0; root < n; ++root) {
        vector<Ball> upper;
        for(int i = 0; i < n; ++i) {
            Ball x = balls[i];
            x.pos -= balls[root].pos;
            if(!x.isBad()) upper.push_back(x);
        }
        sort(upper.begin(), upper.end());
        int m = upper.size();
        for(int i = 0; i < m; ) {
            int j = i;
            while(j < m && cross(upper[i].pos, upper[j].pos) == 0) ++j;
            vector<int> ballByType[2];
            for(int k = i; k < j; ++k)
                ballByType[upper[k].color].push_back(upper[k].id);
            if(!ballByType[0].empty() && !ballByType[1].empty()) {
                printf("%d %d %d\n", root + 1, ballByType[0].front() + 1, ballByType[1].front() + 1);
                return 0;
            }
            int need = balls[root].color ^ 1;
            if(ballByType[need].size() >= 2) {
                printf("%d %d %d\n", root + 1, ballByType[need][0] + 1, ballByType[need][1] + 1);
                return 0;
            }
            i = j;
        }
    }
    printf("-1\n");
    return 0;
}

Code mẫu của ladpro98

#include <bits/stdc++.h>
#define ii pair<int, int>
#define iii pair<ii, int>
#define dii pair<long double, ii>
#define X first
#define Y second
using namespace std;
const int N = 2222;
const int green = 1;
const int red = 2;
const long double eps = 1e-13;
iii a[N];
dii vec[N * N];
int n, m, nv;
int point[5];

int gcd(int a, int b) {
    if (b == 0) return a;
    if (a % b == 0) return b;
    return gcd(b, a % b);
}

int main()
{
    scanf("%d\n", &n); m = 0;
    int i, j, x, y, g;
    for(i=1; i<=n; i++) {
        scanf("%d %d\n", &x, &y);
        a[++m] = iii(ii(x, y),  green);
    }
    for(i=1; i<=n; i++) {
        scanf("%d %d\n", &x, &y);
        a[++m] = iii(ii(x, y), red);
    }
    for(i=1; i<=m; i++) for(j=i+1; j<=m; j++)
    if (i != j) {
        vec[++nv] = dii((long double)(a[i].X.X - a[j].X.X) / (a[i].X.Y - a[j].X.Y),
                        ii(i, j));
    }
    sort(vec+1, vec+1+nv);
    dii v1, v2; int product; long double diff;
    long long x1, x2, y1, y2;
    for(i=1; i<nv; i++) {
        v1 = vec[i]; v2 = vec[i+1];
        diff = abs(v1.X - v2.X);
        if (diff < eps) {
            x1 = a[v1.Y.X].X.X - a[v1.Y.Y].X.X;
            x2 = a[v1.Y.X].X.Y - a[v1.Y.Y].X.Y;
            y1 = a[v2.Y.X].X.X - a[v2.Y.Y].X.X;
            y2 = a[v2.Y.X].X.Y - a[v2.Y.Y].X.Y;
            if (x1 * y2 != x2 * y1) continue;
            point[1] = v1.Y.X; point[2] = v1.Y.Y;
            point[3] = v2.Y.X; point[4] = v2.Y.Y;
            product = a[v1.Y.X].Y * a[v1.Y.Y].Y * a[v2.Y.X].Y * a[v2.Y.Y].Y;
            if (product != 1 && product != 16) {
                sort(point+1, point+5);
                if (!(point[1] == point[2] || point[2] == point[3] || point[3] == point[4])) continue;
                for(j=1; j<=4; j++)
                    if (point[j] != point[j-1]) printf("%d ", point[j]);
                return 0;
            }
        }
    }
    printf("-1");
    return 0;
}

Code mẫu của RR

#include <bits/stdc++.h>

#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 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

struct Point {
    long long x, y;
    int id, color;

    Point() {}
    Point(long long x, long long y) : x(x), y(y) {}

    Point operator - (Point a) { return Point(x-a.x, y-a.y); }
} a[2011], b[2011];
int n, cur;

int ccw(Point a, Point b, Point c) {
    b = b - a; c = c - a;
    long long t = b.x*c.y - b.y*c.x;
    if (t == 0) return 0;
    else if (t < 0) return -1;
    else return 1;
}

struct Comp {
    Point pivot;
    bool operator () (const Point &a, const Point &b) {
        return ccw(pivot, a, b) == 1;
    }
} comp;

int main() {
    ios :: sync_with_stdio(false);
    cin >> n; n *= 2;
    FOR(color,0,1)
        FOR(i,1,n/2) {
            int u = color*n/2 + i;
            cin >> a[u].x >> a[u].y;
            a[u].id = u;
            a[u].color = color;
        }

    FOR(i,1,n) {
        int cur = 0;
        FOR(j,1,n) if (j != i)
        if (a[j].y > a[i].y || (a[j].y == a[i].y && a[j].x > a[i].x)) {
            b[++cur] = a[j];
        }
        comp.pivot = a[i];
        sort(b+1, b+cur+1, comp);

        FOR(j,1,cur-1) {
            int sum = a[i].color + b[j].color + b[j+1].color;
            if (ccw(a[i], b[j], b[j+1]) == 0 && sum && sum < 3) {
                cout << a[i].id << ' ' << b[j].id << ' ' << b[j+1].id << endl;
                return 0;
            }
        }
    }
    cout << -1 << 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.