Codeforces GYM 100651 D I Conduit! (水计算几何)

大致题意:

1e3 个线段,画在一张纸上,求可以看成多少个线段,( 两个线段部分重叠,或收尾相接将看成一个线段)

思路:

在同一一条直线上的两条线段: 他们斜率相等,他们在Y轴或X轴上的投影点相等。然后根据这两个排下序就可以搞出来了。

这题卡精度,要用到eps

//#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <iostream>
#include <cstring>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <string>
#include <vector>
#include <cstdio>
#include <ctime>
#include <bitset>
#include <algorithm>
#define SZ(x) ((int)(x).size())
#define ALL(v) (v).begin(), (v).end()
#define foreach(i, v) for (__typeof((v).begin()) i = (v).begin(); i != (v).end(); ++ i)
#define reveach(i, v) for (__typeof((v).rbegin()) i = (v).rbegin(); i != (v).rend(); ++ i)
#define REP(i,n) for ( int i=1; i<=int(n); i++ )
#define rep(i,n) for ( int i=0; i< int(n); i++ )
using namespace std;
typedef long long ll;
#define X first
#define Y second
typedef pair<double,double> pii;

template <class T>
inline bool RD(T &ret) {
    char c; int sgn;
    if (c = getchar(), c == EOF) return 0;
    while (c != '-' && (c<'0' || c>'9')) c = getchar();
    sgn = (c == '-') ? -1 : 1;
    ret = (c == '-') ? 0 : (c - '0');
    while (c = getchar(), c >= '0'&&c <= '9') ret = ret * 10 + (c - '0');
    ret *= sgn;
    return 1;
}
template <class T>
inline void PT(T x) {
    if (x < 0) {
        putchar('-');
        x = -x;
    }
    if (x > 9) PT(x / 10);
    putchar(x % 10 + '0');
}

const int N = 1e4+100;
const double inf = 1e30;
const double eps = 1e-6;
int n;
struct node{
        pii u,v;
        double k,p;
}all[N];

bool d1(double x,double y) {   return x > y + eps;} // x > y
bool d2(double x,double y) {   return x < y - eps;} // x < y
bool d3(double x,double y) {   return x > y - eps;} // x >= y
bool d4(double x,double y) {   return x < y + eps;}     // x <= y
bool dd(double x,double y) {   return fabs( x - y ) < eps;}  // x == y

bool cmp(const node& a, const node& b) {
        if( dd(a.k , b.k)  ) {
                if( dd( a.p , b.p )  ) {
                        if( dd( a.u.X , b.u.X ) ) return d2( a.u.Y , b.u.Y);
                        return d2( a.u.X , b.u.X );
                }
                return d2( a.p, b.p) ;
        }
        return d2(a.k , b.k) ;
}
int main(){

        while( scanf("%d",&n) && n){
                REP(i,n){
                        scanf("%lf%lf%lf%lf",&all[i].u.X, &all[i].u.Y, &all[i].v.X, &all[i].v.Y);
                        if( d1( all[i].u.X , all[i].v.X ) ) swap(all[i].u, all[i].v);
                        if( dd( all[i].u.X , all[i].v.X ) ) {
                                if( d1( all[i].u.Y , all[i].v.Y ) ) swap(all[i].u, all[i].v);
                                all[i].k = inf;
                                all[i].p = all[i].u.X;
                        }
                        else {
                                all[i].k = ( ( all[i].u.Y - all[i].v.Y )/( all[i].u.X - all[i].v.X ) );
                                all[i].p = all[i].u.Y - all[i].k * all[i].u.X;
                        }
                }
                sort(all+1,all+1+n,cmp);
                all[0].k = all[0].p = -inf;
                int ans = 0, pos = 0;
                double bord;
                REP(i,n){
                        if( dd( all[i].k , inf) ) { pos = i; break; }
                        if( dd( all[i].k , all[i-1].k ) == false || dd( all[i].p , all[i-1].p ) == false ){
                                ans ++;
                                bord = all[i].v.X;
                        }else{
                                if( d1( all[i].u.X , bord ) ) ans ++;
                                bord = max( bord, all[i].v.X );
                        }
                }
                if( pos ) {
                        ans ++;
                        bord = all[pos].v.Y;
                        for(int i = pos+1; i <= n; i ++){
                                if( dd( all[i].p , all[i-1].p ) == false ) ans ++, bord = all[i].v.Y;
                                else{
                                        if( d1( all[i].u.Y , bord ) ) ans ++;
                                        bord = max( bord, all[i].v.Y );
                                }
                        }
                }
                PT(ans),puts("");
        }
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-12-28 05:44:19

Codeforces GYM 100651 D I Conduit! (水计算几何)的相关文章

codeforces Gym 100187H H. Mysterious Photos 水题

H. Mysterious Photos Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100187/problem/H Description Everyone probably heard the rumours about the constellation of Bermuda Triangle: any person who looks to this constellation of th

Codeforces gym Hello 2015 Div1 B and Div2 D

Codeforces gym 100571 problem D Problem 给一个有向图G<V,E>和源点S,边的属性有长度L和颜色C,即E=<L,C>.进行Q次询问,每次给定一个点X,输出S到X的最短路的长度(不存在则输出 -1).但要求S到X的路径中相邻两条边颜色不一样. Limits Time Limit(ms): 1000 Memory Limit(MB): 256 |V|, |E|: [1, 10^5] X, S: [1, |V| ] L: [1, 10^9] |C|

Codeforces gym Hello 2015 Div1 E

Codeforces gym 100570 problem E (一种处理动态最长回文子串问题的方法) Problem 给一个长度为N的字符串S,字符集是'a'-'z'.进行Q次操作,操作分三种.一,修改位置X的字符为C:二,查询以P位置为中心的最长回文子串的长度,并输出:三,查询以P与P+1的中间位置为中心的最长回文子串的长度,并输出. More 第二种操作子串长度为奇数,一定存在:第三种操作子串长度为偶数,若不存在,输出 -1. Limits Time Limit(ms): 4000(1s足

Codeforces gym Hello 2015 Div1 C and Div2 E

Codeforces gym 100570 problem C Codeforces gym 100571 problem E Problem 给一个N行M列的矩阵Ma,进行Q次(Q<=10)查询,每次给定一个K,问有多少子矩阵,满足最大值max - 最小值min <=K. Limits Time Limit(ms): 8000 Memory Limit(MB): 512 N, M: [1, 400] Q: [1, 10] Ma(i, j), K: [1, 10^9] Solution (Th

【Codeforces 368A】Brain&#39;s Photos 水题

黑白灰都是#Black&White #include <cstdio> int n,m; int main() { scanf("%d%d",&n,&m); int ok=0; for(int i=0;i<n;i++) for(int j=0;j<m;j++) { char s[5]; scanf("%s",s); if(s[0]!='W'&&s[0]!='B'&&s[0]!='G')

【模拟】ECNA 2015 I What&#39;s on the Grille? (Codeforces GYM 100825)

题目链接: http://codeforces.com/gym/100825 题目大意: 栅栏密码.给定N(N<=10),密钥为一个N*N的矩阵,'.'代表空格可以看到,'X'代表被遮挡,还有密文字符串S,长度为N*N 每次将这个矩阵顺时针旋转90°,把矩阵中空格对应的位置按照从上到下从左到右的顺序依次填充上密文字符,求最终这个密文字符能否填满N*N的矩阵,能按顺序输出得到的答案,不能输出"invalid grille" 题目思路: [模拟] 直接模拟即可.旋转的坐标公式很好推.

codeforces 707A A. Brain&#39;s Photos(水题)

题目链接: A. Brain's Photos 题意: 问是黑白还是彩色; 思路: 没有思路: AC代码: #include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <cmath> #include <bits/stdc++.h> #include <stack> #include <map> u

Codeforces gym Hello 2015 Div2 B

Codeforces gym 100571 problem B Problem 设函数F(x),F(1)与F(2)已知,且当 i>=3,F(i)=a*F(i-2)+b*F(i-1).再给一个长度为N的数列A,进行Q次如下操作:每次给一个区间[L, R],对于每个k(L=<k<=R),将A[k]=A[k]+F[k-L+1].最后输出数列A(mod 10^9+7). Limits Time Limit(ms): 1000 Memory Limit(MB): 256 N, Q: [1, 10^

Codeforces Gym - 101147J Whistle&#39;s New Car

Discription Statements Whistle has bought a new car, which has an infinite fuel tank capacity. He discovered an irregular country since it has n cities and there are exactly n?-?1roads between them, of course, all cities are connected. He is so much