Computational Geometry POJ2318 POJ2398 Toys

topic

Title link: POJ2318 POJ2398

n mezzanine, m toys.
Regarding the number of toys in the mezzanine.

answer

2318

In the judgment, the coordinates of the mass point (toy) are cross-multiplied from the word and the direction vector of the interlayer, and the right-hand rule is used to judge which side of the board the point is on.

Discuss by situation:

  1. The splint number is 1 11 and the toy is on the left side of the splint: put in0 0No. 0 mezzanine;
  2. On the right side of the splint, proceed to the right until it hits the splint jjj and on its left: put it onj − 1 j-1jMezzanine 1 ;
  3. On the right side of the splint, proceed to the right until the last splint jjj : putjjJ mezzanine.

2398

Compared with the previous question, the order of the splint and the output of the sum are more.

Sorting: You can sort by upper end point/lower end point;
output: traverse and record.

AC code

2318

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<cstdlib>
#include<string>
#include<queue>
#include<map>
#include<stack>
#include<list>
#include<set>
#include<deque>
#include<vector>
#include<ctime>

using namespace std;
//#pragma GCC optimize(2)
#define IO ios::sync_with_stdio(false);cin.tie(0);cout.tie(0)
#define ull unsigned long long
#define ll long long
#define rep(i, x, y) for(int i=x;i<=y;i++)
#define mms(x, n) memset(x, n, sizeof(x))
#define mmc(A, tree) memcpy(A, tree, sizeof(tree))
#define eps (1e-8)
#define PI (acos(-1.0))
#define INF (0x3f3f3f3f)
#define mod (ull)(1e9+7)
typedef pair<int, int> P;
const int N = 1e4;

struct p {
    double x, y;

//    p() = default;

//    p(double _x, double _y) : x(_x), y(_y) {}
//
//    p operator-(const p &a) const {
//        return {x - a.x, y - a.y};
//    }
//
//    p operator+(const p &a) const {
//        return {x + a.x, y + a.y};
//    }
} toys[N];

struct l {
    p s, e;

//    l() = default;

//    l(p _a, p _b) : s(_a), e(_b) {}
} line[N];

//bool zero(double x) {
//
//}

double cross(l a, p c) {
//    printf("%.12f\t", (a.e.x - a.s.x) * (c.y - a.e.y) - (c.x - a.s.x) * (a.e.y - a.s.y));
    return (a.e.x - a.s.x) * (c.y - a.s.y) - (c.x - a.s.x) * (a.e.y - a.s.y);
}

int ans[N];

int main() {
#ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
#endif
    IO;
    int n, m;
    p le, ri;
    while (cin >> n && n != 0) {
        cin >> m >> le.x >> le.y >> ri.x >> ri.y;
        mms(ans, 0);
        for (int i = 1; i <= n; i++) {
            line[i].s.y = le.y;
            line[i].e.y = ri.y;
            cin >> line[i].s.x >> line[i].e.x;
        }
        for (int i = 1; i <= m; i++) {
            cin >> toys[i].x >> toys[i].y;
            for (int j = 1; j <= n; j++) {
                if (cross(line[j], toys[i]) > eps) {
                    if (j == n) {
                        ans[j]++;
                        break;
                    } else continue;
                } else {
                    ans[j - 1]++;
                    break;
                }
            }
        }
        for (int i = 0; i <= n; i++) {
            cout << i << ": " << ans[i] << endl;
        }
        cout << endl;
    }
    return 0;
}

2398

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<cstdlib>
#include<string>
#include<queue>
#include<map>
#include<stack>
#include<list>
#include<set>
#include<deque>
#include<vector>
#include<ctime>

using namespace std;
//#pragma GCC optimize(2)
#define IO ios::sync_with_stdio(false);cin.tie(0);cout.tie(0)
#define ull unsigned long long
#define ll long long
#define rep(i, x, y) for(int i=x;i<=y;i++)
#define mms(x, n) memset(x, n, sizeof(x))
#define mmc(A, tree) memcpy(A, tree, sizeof(tree))
#define eps (1e-8)
#define PI (acos(-1.0))
#define INF (0x3f3f3f3f)
#define mod (ull)(1e9+7)
typedef pair<int, int> P;
const int N = 1e4;

struct p {
    ll x, y;

//    p() = default;

//    p(double _x, double _y) : x(_x), y(_y) {}
//
//    p operator-(const p &a) const {
//        return {x - a.x, y - a.y};
//    }
//
//    p operator+(const p &a) const {
//        return {x + a.x, y + a.y};
//    }
} toys[N];

struct l {
    p s, e;

//    l() = default;

//    l(p _a, p _b) : s(_a), e(_b) {}
} line[N];

//bool zero(double x) {
//
//}

double cross(l a, p c) {
//    printf("%.12f\t", (a.e.x - a.s.x) * (c.y - a.e.y) - (c.x - a.s.x) * (a.e.y - a.s.y));
    return (a.e.x - a.s.x) * (c.y - a.s.y) - (c.x - a.s.x) * (a.e.y - a.s.y);
}

bool cmp(const l &a, const l &b) {
    return a.e.x < b.e.x;
}

ll ans[N], ret[N];

int main() {
#ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
#endif
    IO;
    int n, m;
    p le, ri;
    while (cin >> n && n != 0) {
        cin >> m >> le.x >> le.y >> ri.x >> ri.y;
        mms(ans, 0);
        mms(ret, 0);
        for (int i = 1; i <= n; i++) {
            line[i].s.y = le.y;
            line[i].e.y = ri.y;
            cin >> line[i].s.x >> line[i].e.x;
        }
        sort(line + 1, line + 1 + n, cmp);
        for (int i = 1; i <= m; i++) {
            cin >> toys[i].x >> toys[i].y;
            for (int j = 1; j <= n; j++) {
                if (cross(line[j], toys[i]) < eps) {
                    ans[j - 1]++;
                    break;
                } else {
                    if (j == n) {
                        ans[j]++;
                        break;
                    } else continue;
                }
            }
        }
        ll maxx = -1;
        for (int i = 0; i <= n; i++) {
            ret[ans[i]]++;
            maxx = max(maxx, ans[i]);
        }
        cout << "Box\n";
        for (int i = 1; i <= maxx; i++) {
            if (ret[i]) cout << i << ": " << ret[i] << "\n";
        }
//        cout << "\n";
    }
    return 0;
}

Guess you like

Origin blog.csdn.net/qq_45934120/article/details/108148814