EDU 50 E. Covered Points 利用克莱姆法则计算线段交点

E. Covered Points

利用克莱姆法则计算线段交点。n^2枚举,最后把个数开方,从ans中减去。

ans加上每个线段的定点数, 定点数用gcs(△x , △y)+1计算。

#include <algorithm>
#include  <iterator>
#include  <iostream>
#include   <cstring>
#include   <cstdlib>
#include   <iomanip>
#include    <bitset>
#include    <cctype>
#include    <cstdio>
#include    <string>
#include    <vector>
#include     <stack>
#include     <cmath>
#include     <queue>
#include      <list>
#include       <map>
#include       <set>
#include   <cassert>

using namespace std;
#define lson (l , mid , rt << 1)
#define rson (mid + 1 , r , rt << 1 | 1)
#define debug(x) cerr << #x << " = " << x << "\n";
#define pb push_back
#define pq priority_queue

typedef long long ll;
typedef unsigned long long ull;
//typedef __int128 bll;
typedef pair<ll ,ll > pll;
typedef pair<int ,int > pii;
typedef pair<int,pii> p3;

//priority_queue<int> q;//这是一个大根堆q
//priority_queue<int,vector<int>,greater<int> >q;//这是一个小根堆q
#define fi first
#define se second
//#define endl ‘\n‘
//#define R register
#define OKC ios::sync_with_stdio(false);cin.tie(0)
#define FT(A,B,C) for(int A=B;A <= C;++A)  //用来压行
#define REP(i , j , k)  for(int i = j ; i <  k ; ++i)
#define max3(a,b,c) max(max(a,b), c);
#define min3(a,b,c) min(min(a,b), c);
//priority_queue<int ,vector<int>, greater<int> >que;

const ll mos = 0x7FFFFFFF;  //2147483647
const ll nmos = 0x80000000;  //-2147483648
const int inf = 0x3f3f3f3f;
const ll inff = 0x3f3f3f3f3f3f3f3f; //18
const int mod = 1e9+7;
const double esp = 1e-8;
const double PI=acos(-1.0);
const double PHI=0.61803399;    //黄金分割点
const double tPHI=0.38196601;

template<typename T>
inline T read(T&x){
    x=0;int f=0;char ch=getchar();
    while (ch<‘0‘||ch>‘9‘) f|=(ch==‘-‘),ch=getchar();
    while (ch>=‘0‘&&ch<=‘9‘) x=x*10+ch-‘0‘,ch=getchar();
    return x=f?-x:x;
}

/*-----------------------showtime----------------------*/

            const int maxn = 1009;
            struct node{
                ll x1,y1,x2,y2;
            }a[maxn];
            ll ans = 0;
            map<pii, int> mp;

            void cal(int i,int j){

                    ll x1 = a[i].x1,y1 = a[i].y1,x2 = a[i].x2,y2 = a[i].y2;

                    ll x3 = a[j].x1,y3 = a[j].y1,x4 = a[j].x2,y4 = a[j].y2;

                    ll a = (y1-y2) * (x4-x3) - (x2-x1)*(y3-y4);
                    ll t = (x2*y1 - x1*y2)*(x4-x3) - (x2-x1)*(x4*y3 - x3*y4);
                    ll p = (y1-y2) * (x4*y3-x3*y4) - (x2*y1-x1*y2)*(y3-y4);

                    if(a == 0)return;
                    if(t % a || p % a) return;
                    t = t/a; p = p/a;

                    if(x1 > x2) swap (x1,x2);   if(t < x1 || t > x2) return;

                    if(x3 > x4) swap (x3,x4);   if(t < x3 || t > x4) return;

                    if(y1 > y2) swap(y1,y2);    if(p < y1 || p > y2) return;

                    if(y3 > y4) swap(y3,y4);    if(p < y3 || p > y4) return;

                    mp[pii(t,p)] ++;
            }
int main(){
            int n;  scanf("%d", &n);
            for(int i=1; i<=n; i++){
                ll x1,y1,x2,y2;
                scanf("%lld%lld%lld%lld", &x1, &y1, &x2, & y2);
                a[i] = (node){x1,y1,x2,y2};
                ll d1 = abs(x1 - x2);
                ll d2 = abs(y1 - y2);
                ans += __gcd(d1, d2) + 1;
            }

            for(int i=1; i<=n; i++){
                for(int j=1; j<=n; j++){
                    if(i == j) continue;
                    cal(i,j);
                }
            }

            for(auto p : mp){
                ans -= (int)sqrt(p.se);
            }
            printf("%lld\n", ans);

            return 0;
}

原文地址:https://www.cnblogs.com/ckxkexing/p/10303299.html

时间: 2024-10-17 00:31:00

EDU 50 E. Covered Points 利用克莱姆法则计算线段交点的相关文章

hdu 1086(计算几何入门题——计算线段交点个数)

链接:http://acm.hdu.edu.cn/showproblem.php?pid=1086 You can Solve a Geometry Problem too Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 7167    Accepted Submission(s): 3480 Problem Description Ma

利用数组计算斐波那契数列

本题要求编写程序,利用数组计算菲波那契(Fibonacci)数列的前N项,每行输出5个,题目保证计算结果在长整型范围内.Fibonacci数列就是满足任一项数字是前两项的和(最开始两项均定义为1)的数列,例如::1,1,2,3,5,8,13,.... 输入格式: 输入在一行中给出一个整数N(1). 输出格式: 输出前N个Fibonacci数,每个数占11位,每行输出5个.如果最后一行输出的个数不到5个,也需要换行. 如果输入的N不在有效范围内,则输出"Invalid.". 输入样例1:

利用sklearn计算文本相似性

利用sklearn计算文本相似性,并将文本之间的相似度矩阵保存到文件当中.这里提取文本TF-IDF特征值进行文本的相似性计算. #!/usr/bin/python # -*- coding: utf-8 -*- import numpy import os import sys from sklearn import feature_extraction from sklearn.feature_extraction.text import TfidfTransformer from sklea

利用MATLAB计算三维坐标序列距离误差程序

1.三维坐标储存在文件中,格式如下: 各坐标间的距离真值是一定值,计算相邻距离的标准差. 2.MATLAB程序如下: %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % 名称:caculateAccuracy.m % 功能:读取三维世界坐标,计算精度 % 作者:LYC % 单位:中科院苏州医工所 % 日期:2014.5.5 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

poj 1915 BFS 利用 pre 计算步数------------------路径

// BFS #include <stdio.h> #include <string.h> int visited[301][301]; // visited 已经访问过了 int dic[8][2]={{2,1},{1,2},{-1,2},{-2,1},{-2,-1},{-1,-2},{1,-2},{2,-1}}; int head,end,n,ex,ey,sx,sy; struct quene { int x,y,pre; // pre 前一个结点 }q[100000]; vo

poj 1279 Art Gallery(利用极角计算半平面交)

题意:给出n个点的坐标描述一个多边形画廊.在画廊平面上找到一片表面,从该区域能够看到画廊墙壁上的每一个点: 思路:将这片表面称为多边形的核.核中一点与多边形边界上任意一点的连线都在多边形内部.凸多边形的核为其本身,凹多边形的核为其内部的一部分或不存在: 将多边形的n个顶点转化为n条边的直线方程:逆时针用多边形的边剖分多边形所在平面,保留向里的部分,舍去向外的部分,剩下的即为核: 利用叉积公式计算核面积,即为所求面积: #include<cstdio> #include<cstring&g

Covered Points Count(思维题)

C. Covered Points Count time limit per test 3 seconds memory limit per test 256 megabytes input standard input output standard output You are given nn segments on a coordinate line; each endpoint of every segment has integer coordinates. Some segment

Codeforces 1000C Covered Points Count

C. Covered Points Count题目大意:有n条线段,问有多少个点被i条线段覆盖(i=1~n).很常见的线段覆盖套路题QAQ.坐标排序后把左端点当做+1,右端点当做-1,扫一遍统计答案即可.但是记得开ll,数组大小开双倍. 1 #include <iostream> 2 #include <cstdio> 3 #include <algorithm> 4 #include <cmath> 5 #include <queue> 6 #

Covered Points Count CF1000C 思维 前缀和 贪心

Covered Points Count time limit per test 3 seconds memory limit per test 256 megabytes input standard input output standard output You are given nn segments on a coordinate line; each endpoint of every segment has integer coordinates. Some segments c