HUST 1376 Random intersection

神题。同学指教。1秒AC。。。http://blog.csdn.net/jtjy568805874/article/details/50724656

#include<cstdio>
#include<cstring>
#include<ctime>
#include<algorithm>
using namespace std;

const int maxn = 100000 + 10;
struct point
{
    double x;
    double y;
    int id;
}p[2 * maxn];
struct Line
{
    int a;
    int b;
}line[maxn];
int T, n, tot;
int flag[maxn];

bool cmp(const point&a, const point&b)
{
    if (a.x == b.x) return a.y < b.y;
    return a.x < b.x;
}

const double eps = 1e-8;
#define zero(x)(((x)>0?(x):(-x))<eps)

double xmult(point p1, point p2, point p0)
{
    return (p1.x - p0.x)*(p2.y - p0.y) - (p2.x - p0.x)*(p1.y - p0.y);
}

int dots_inline(point p1, point p2, point p3)
{
    return zero(xmult(p1, p2, p3));
}

int same_side(point p1, point p2, point l1, point l2)
{
    return xmult(l1, p1, l2)*xmult(l1, p2, l2)>eps;
}

int dot_online_in(point p, point l1, point l2)
{
    return zero(xmult(p, l1, l2)) && (l1.x - p.x)*(l2.x - p.x)<eps && (l1.y - p.y)*(l2.y - p.y)<eps;
}

int intersect_in(point u1, point u2, point v1, point v2)
{
    if (!dots_inline(u1, u2, v1) || !dots_inline(u1, u2, v2)) return !same_side(u1, u2, v1, v2) && !same_side(v1, v2, u1, u2);
    return dot_online_in(u1, v1, v2) || dot_online_in(u2, v1, v2) || dot_online_in(v1, u1, u2) || dot_online_in(v2, u1, u2);
}

int main()
{
    scanf("%d", &T);
    while (T--)
    {
        long long ans = 0;
        scanf("%d", &n); tot = 0; memset(flag, 0, sizeof flag);
        for (int i = 1; i <= n; i++)
        {
            scanf("%lf%lf", &p[tot].x, &p[tot].y); p[tot].id = i; tot++;
            scanf("%lf%lf", &p[tot].x, &p[tot].y); p[tot].id = i; tot++;
        }
        sort(p, p + tot, cmp);
        for (int i = 0; i < tot; i++)
        {
            if (!flag[p[i].id])
            {
                flag[p[i].id] = 1;
                line[p[i].id].a = i;
            }
            else
            {
                line[p[i].id].b = i;
                p[i].id = -p[i].id;
            }
        }

        for (int i = 0; i < tot; i++)
        {
            if (p[i].id>0)
            {
                int j;
                for (j = i + 1; p[j].id != -p[i].id; j++)
                {
                    if (p[j].id > 0)
                    {
                        if (intersect_in(p[line[p[i].id].a], p[line[p[i].id].b], p[line[p[j].id].a], p[line[p[j].id].b])) ans++;
                    }
                }

                for (;; j++)
                {
                    if (j+1<tot&&p[j].x == p[j + 1].x&&p[j].y == p[j + 1].y)
                    {
                        if (p[j+1].id>0)
                        if (intersect_in(p[line[p[i].id].a], p[line[p[i].id].b], p[line[p[j+1].id].a], p[line[p[j+1].id].b])) ans++;
                    }
                    else break;
                }
            }

        }
        printf("%lld\n", ans);
    }
    return 0;
}
时间: 2024-08-25 12:24:30

HUST 1376 Random intersection的相关文章

[Linked List]Intersection of Two Linked Lists

Total Accepted: 53721 Total Submissions: 180705 Difficulty: Easy Write a program to find the node at which the intersection of two singly linked lists begins. For example, the following two linked lists: A: a1 → a2 c1 → c2 → c3 B: b1 → b2 → b3 begin

python基础知识 - 集合与random

集合 集合是一种数据类型,一个类似列表的东西,特点是无序的,不重复的. 集合的作用: 可以把一个列表中的重复数据去除 定义集合: lowerLetterSet = set(string.ascii_lowercase)#小写字母集合 upperLetterSet = set(string.ascii_uppercase)#大写字母集合 numLetterSet = set(string.digits)#数字集合 specielLetterSet = set(string.punctuation)

numpy的random模块

[说明] 翻译自官网的文档. 随机抽样 (numpy.random) 简单的随机数据 rand(d0, d1, ..., dn) 随机值 >>> np.random.rand(3,2) array([[ 0.14022471, 0.96360618], #random [ 0.37601032, 0.25528411], #random [ 0.49313049, 0.94909878]]) #random randn(d0, d1, ..., dn) 返回一个样本,具有标准正态分布.

[LeetCode] 349 Intersection of Two Arrays &amp; 350 Intersection of Two Arrays II

这两道题都是求两个数组之间的重复元素,因此把它们放在一起. 原题地址: 349 Intersection of Two Arrays :https://leetcode.com/problems/intersection-of-two-arrays/description/ 350 Intersection of Two Arrays II:https://leetcode.com/problems/intersection-of-two-arrays-ii/description/ 题目&解法

spark 教程三 spark Map filter flatMap union distinct intersection操作

RDD的创建 spark 所有的操作都围绕着弹性分布式数据集(RDD)进行,这是一个有容错机制的并可以被并行操作的元素集合,具有只读.分区.容错.高效.无需物化.可以缓存.RDD依赖等特征 RDD的创建基础RDD 1.并行集合(Parallelized Collections):接收一个已经存在的Scala集合,然后进行各种并行运算 var sc=new SparkContext(conf) var rdd=sc.parallelize(Array(2,4,9,3,5,7,8,1,6)); rd

[TypeScript] Create random integers in a given range

Learn how to create random integers using JavaScript / TypeScript. /** * Returns a random int between * @param start inclusive * @param before exclusive */ export function randomInt(start: number, before: number) { return start + Math.floor(Math.rand

java中Random随机种子使用

在java中,通过Random生成随机数时,如果设置随机种子,则相同的种子,产生的随机数相同.若不设置则每次随机的不同. Random rnd = new Random(); rnd.setSeed(10);//用于设置种子. rnd.nextInt();// 用于产生随机数. rnd.nextInt(10); // 产生(0-9)数字.

random模块

首先random函数并不是一个真正的随机数,而是根据随机数算法算出来的数 random 函数返回一个从 0.0 到 1.0 的随机浮点数 (包括 0.0, 但是不包括 1.0).每次,调用 random,就会的到一个随机数. >>> import random >>> random.random()  0.9536935859948081  >>> random.random()  0.40421571099356646  randint 函数接受一个

HUST 1588 辗转数对

1588 - 辗转数对 时间限制:1秒 内存限制:128兆 155 次提交 27 次通过 题目描述 假设当前有一个数对(a, b),我们可以通过一步将这个数对变为一个新数对(a + b, b)或者是(a, a + b).初始的数对为(1, 1),你的任务是找到一个数字k,即通过最少的步数使得这个数对中至少一个数字等于n. 输入 输入包括多组数据,每组数据包括一行,每行有一个整数n. 输出 每组数据输出一行,每行一个整数n. 样例输入 5 3 样例输出 3 2 提示 第一个样例的方法是 (1,1)