计算几何 POJ2318 POJ2398 Toys

题目

题目链接:POJ2318 POJ2398

n个夹层,m个玩具。
关于夹层中玩具数量的问题。

题解

2318

在判断时使质点(玩具)坐标从字与夹层的方向向量进行叉乘运算,根据右手定则判断该点位于板子的哪一侧。

分情况进行讨论:

  1. 夹板编号为 1 1 1 且玩具位于夹板左侧:放入 0 0 0 号夹层;
  2. 位于夹板右侧则一直向右进行直到碰到夹板 j j j 且位于其左侧:放于 j − 1 j-1 j1号夹层;
  3. 位于夹板右侧则一直向右进行直到最后一个夹板 j j j :放入 j j j 号夹层。

2398

与上题相比多了对夹板的排序以及总和的输出。

排序:按照上端点/下端点进行排序都可以;
输出:遍历并记录即可。

AC代码

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;
}

猜你喜欢

转载自blog.csdn.net/qq_45934120/article/details/108148814