「几何」[USACO12DEC]疯狂的栅栏Crazy Fences

时间:2019-09-13
本文章向大家介绍「几何」[USACO12DEC]疯狂的栅栏Crazy Fences,主要包括「几何」[USACO12DEC]疯狂的栅栏Crazy Fences使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

[USACO12DEC]疯狂的栅栏Crazy Fences

题目链接:[USACO12DEC]疯狂的栅栏Crazy Fences

题目大意

给你\(n\)个坐标,每个坐标包含四个位置信息 \(x_1,y_1,x_2,y_2\) 然后\(x_1,y_1\)\(x_2, y_2\)通过一条直线连接,再给你 \(m\) 个坐标,每个坐标两个字母\(x_1, y_1\)分别代表牛的位置,每两头牛如果不通过直线能相互到达,那么就说明这两头牛在一个社区,现在求一个社区的最大奶牛数。

题目题解

想了爆搜,看了下数据范围,啊,没了没了,然后洛谷上没有题解,去USACO上找题解
题解在这里:题解

这个题解的代码 quq太TM难看了,写了半天发现有些思路都不知道他是怎么想的,那就直接copy吧

这个题的总体思路还算简单,如果当前由当前点从某一个方向上释放一条直线,直线通过图上的直线的次数是奇数就说明肯定在多边形内,如果是偶数,就说明在多边形外,然后对于每个多边形我们考虑图形内奶牛可以交互,这里可以排序嘛,以\(x\)从小到大排序,考虑前后奶牛是否能够交互即可(关于最终判断,有些问题没有捋清楚,如果有dalao知道,可以在下面留言ing)

代码如下

#include <cstdio>
#include <map>
#include <vector>
#include <algorithm>
#include <cstring>
using namespace std;

#define NFENCES_MAX 1005
#define NCOWS_MAX 1005

bool rayHits(long long cx, long long cy, long long f1x, long long f1y, long long f2x, long long f2y) {
    if ((f1y > cy) ^ (f2y > cy)) {
        return (f1y - f2y < 0) ^ (f2x * (f1y - cy) + f1x * (cy - f2y) > cx * (f1y - f2y));
    } else return false;
}

map<pair<int,int>, vector<int> > pointMap;
pair<pair<int,int>, pair<int,int> > fences[NFENCES_MAX];
int cycle[NFENCES_MAX];

char parities[NCOWS_MAX][NFENCES_MAX];

char* parityptrs[NCOWS_MAX];
inline bool ptrcmp(char* a, char* b) {
    return strcmp(a, b) < 0;
}

int main() {
    int nFences, nCows;
    scanf("%d", &nFences);
    scanf("%d", &nCows);
    for (int i = 0; i < nFences; i++) {
        pair<int,int> p1, p2;
        scanf("%d", &p1.first);
        scanf("%d", &p1.second);
        scanf("%d", &p2.first);
        scanf("%d", &p2.second);
        pointMap[p1].push_back(i);
        pointMap[p2].push_back(i);
        fences[i] = pair<pair<int,int>, pair<int,int> >(p1, p2);

        cycle[i] = -1;
        memset(parities[i], 0, nCows);
    }
    for (int i = 0; i < nCows; i++) {
        parityptrs[i] = parities[i];
    }

    int nCycles = 0;
    for (int i = 0; i < nFences; i++) {
        if (cycle[i] == -1) {
            int j = i;
            pair<int,int> last = fences[i].first;
            do {
                cycle[j] = nCycles;
                last = fences[j].first == last ? fences[j].second : fences[j].first;
                vector<int> const& v = pointMap[last];
                j = (v[0] == j ? v[1] : v[0]);
            } while (j != i);
            nCycles++;
        }
    }

    for (int i = 0; i < nCows; i++) {
        int cowx, cowy;
        scanf("%d", &cowx);
        scanf("%d", &cowy);
        for (int j = 0; j < nFences; j++) {
            parities[i][cycle[j]] ^= (char)rayHits(cowx, cowy, fences[j].first.first, fences[j].first.second, fences[j].second.first, fences[j].second.second);
        }
        for (int j = 0; j < nCycles; j++) {
            parities[i][j] = parities[i][j] ? '1' : '0';
        }
        parities[i][nCycles] = '\0';
    }

    sort(parityptrs, parityptrs + nCows, ptrcmp);

    int maxans = 0;
    int curcount = 0;
    for (int i = 0; i < nCows; i++) {
        if (i == 0 || strcmp(parityptrs[i], parityptrs[i-1]) != 0) {
            curcount = 1;
        } else {
            curcount++;
        }
        maxans = max(maxans, curcount);
    }
    printf("%d\n", maxans);
}

原文地址:https://www.cnblogs.com/Nicoppa/p/11516378.html