[CodeForces]CodeForces - 13D 几何 思维

  大致题意:

    给出N个红点和M个蓝点,问可以有多少个红点构成的三角形,其内部不含有蓝点

   假设我们现在枚举了一条线段(p[i],p[j]),我们可以记录线段下方满足(min(p[i].x,p[j].x)<x<=max(p[i].x,p[j].x) 的数量

    时间复杂度为O(N*N*M)

   那么我们就可以枚举三角形O(1)判断三角形内有无红点。

  

  1 #include<cstdio>
  2 #include<iostream>
  3 #include<cstring>
  4 #include<algorithm>
  5 #include<queue>
  6 #include<set>
  7 #include<map>
  8 #include<stack>
  9 #include<time.h>
 10 #include<cstdlib>
 11 #include<cmath>
 12 #include<list>
 13 using namespace std;
 14 #define MAXN 5000006
 15 #define eps 1e-9
 16 #define For(i,a,b) for(int i=a;i<=b;i++)
 17 #define Fore(i,a,b) for(int i=a;i>=b;i--)
 18 #define lson l,mid,rt<<1
 19 #define rson mid+1,r,rt<<1|1
 20 #define mkp make_pair
 21 #define pb push_back
 22 #define cr clear()
 23 #define sz size()
 24 #define met(a,b) memset(a,b,sizeof(a))
 25 #define iossy ios::sync_with_stdio(false)
 26 #define fr freopen
 27 #define pi acos(-1.0)
 28 #define Vector Point
 29 #define fir first
 30 #define sec second
 31 #define it_s_too_hard main
 32 #define I_can_t_solve_it solve
 33 //const long long inf=1LL<<62;
 34 const int inf=1e9+9;
 35 const int Mod=1e9+7;
 36 typedef unsigned long long ull;
 37 typedef long long ll;
 38 typedef pair<int,int> pii;
 39 typedef pair<ll,ll> pll;
 40 typedef double ld;
 41 inline int dcmp(ld x){ if(fabs(x)<=eps) return 0; return x<0?-1:1;}
 42 inline int scan(){
 43     int x=0,f=1;char ch=getchar();
 44     while(ch<‘0‘||ch>‘9‘){if(ch==‘-‘)f=-1;ch=getchar();}
 45     while(ch>=‘0‘&&ch<=‘9‘){x=x*10+ch-‘0‘;ch=getchar();}
 46     return x*f;
 47 }
 48 inline ll scan(ll x){
 49     int f=1;char ch=getchar();x=0;
 50     while(ch<‘0‘||ch>‘9‘){if(ch==‘-‘)f=-1;ch=getchar();}
 51     while(ch>=‘0‘&&ch<=‘9‘){x=x*10+ch-‘0‘;ch=getchar();}
 52     return x*f;
 53 }
 54 struct Point{
 55     int x,y;
 56     Point(int x=0,int y=0):x(x),y(y) {}
 57     Point operator - (const Point &a)const { return Point(x-a.x,y-a.y);}
 58     Point operator * (const ll &a)const{return Point(x*a,y*a);  }
 59     Point operator + (const Point &a)const{return Point(x+a.x,y+a.y);}
 60     bool operator == (const Point &a)const{ return dcmp(x-a.x)==0 && dcmp(y-a.y)==0;}
 61     bool operator < (const Point &a)const{if(x==a.x) return y<a.y;return x<a.x;}
 62     void read(){scanf("%d%d",&x,&y);}
 63     void out(){cout<<x<<" "<<y<<endl;}
 64 };
 65 double Dot(Vector a,Vector b){
 66     return a.x*b.x+a.y*b.y;
 67 }
 68 double dis(Vector a){
 69     return sqrt(Dot(a,a));
 70 }
 71 ll Cross(Vector a,Vector b){
 72     return a.x*1LL*b.y-a.y*1LL*b.x;
 73 }
 74 bool chk(Point p1,Point p2,Point p3,Point p){
 75     return abs(Cross(p2-p,p1-p))+abs(Cross(p3-p,p2-p))+abs(Cross(p1-p,p3-p))==abs(Cross(p2-p1,p3-p1));
 76 }
 77 int n,m;
 78 Point p[5005],q[5005];
 79 int mp[505][505];
 80 void solve(){
 81     n=scan();m=scan();
 82     met(mp,0);
 83     For(i,0,n-1) p[i].read();
 84     For(i,1,m) q[i].read();
 85     sort(p,p+n);
 86     For(i,0,n-1){
 87         For(j,i+1,n-1){
 88             if(p[i].x==p[j].x) continue;
 89             For(k,1,m){
 90                 if(q[k].x>p[i].x && q[k].x<=p[j].x && Cross(q[k]-p[j],p[i]-p[j])<0) mp[i][j]++;
 91             }
 92         }
 93     }
 94     int ans=0;
 95     For(i,0,n-1){
 96         For(j,i+1,n-1){
 97             For(k,j+1,n-1){
 98                 int c1=mp[i][j],c2=mp[j][k],c3=mp[i][k];
 99                 if(c1+c2==c3) ans++;
100             }
101         }
102     }
103     cout<<ans<<endl;
104 }
105 int it_s_too_hard(){
106     int t=1;
107     For(i,1,t){
108         I_can_t_solve_it();
109     }
110     return 0;
111 }

蒻菜代码

原文地址:https://www.cnblogs.com/cjbiantai/p/9438081.html

时间: 2024-08-06 07:59:02

[CodeForces]CodeForces - 13D 几何 思维的相关文章

Codeforces Codeforces Round #484 (Div. 2) E. Billiard

Codeforces Codeforces Round #484 (Div. 2) E. Billiard 题目连接: http://codeforces.com/contest/982/problem/E Description Consider a billiard table of rectangular size $n \times m$ with four pockets. Let's introduce a coordinate system with the origin at t

Codeforces Codeforces Round #484 (Div. 2) D. Shark

Codeforces Codeforces Round #484 (Div. 2) D. Shark 题目连接: http://codeforces.com/contest/982/problem/D Description For long time scientists study the behavior of sharks. Sharks, as many other species, alternate short movements in a certain location and

CodeForces 1102C-简单的思维题

题目链接:http://codeforces.com/problemset/problem/1102/C C. Doors Breaking and Repairing time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output You are policeman and you are playing a game with Slavik

CodeForces 789D 欧拉路径计数,思维

CodeForces 789D 题意:n个点m条边的无向图,求经过其中m-2条边两次,剩下2条边一次的方案数有几种,如果剩下两条边的集合一样算同一种. tags: 选出两条边,其它m-2条边假想复制成两条,这样就是要求欧拉路径是否存在,即奇点个数是否为0或2. 所以该怎么选这两条边呢? 先把边分为自环边和普通边. 1.选取两条不相邻普通边,图中存在4个奇点,不满足欧拉路径条件: 2.选取两条相邻普通边,图中存在2个奇点,满足欧拉路径条件: 3.选取一条普通边一条自环,图中存在2个奇点,满足欧拉路

CodeForces 789E bfs建模,思维

CodeForces 789E 题意:有k种可乐,每种的测试为ai/1000. 要你合成一种浓度为n/1000的可乐,问最小要杯可乐,每种可乐可重复取. tags:  要注意到浓度绝不会超过1000/1000. 假设选取m杯可乐,则 (a1+a2+......+am) / m = n,变换一下为(a1-n)+(a2-n)+......+(am-n) = 0.即要选 m杯可乐,其浓度减 n之和为0.而浓度不超过1000,故(a1-n)+(a2-n)+....+(as-n)的和肯定在 -1000~1

2017-03-16 Codeforces 453A 概率期望,思维 UOJ 228(待补)

Codeforces 453A   A. Little Pony and Expected Maximum 题意:一个m面质地均匀的骰子,每面出现的概率都是独立的1/m, 你需要投掷n次,其结果是这n次出现的最大点数.问投掷n次骰子的结果的期望值是多少,要求相对误差或绝对误差不超过1e-4. tags:枚举骰子出现最大值i,计算出最大值为i时的概率,就得到了答案. 最大值为i的概率=(i/m)^n-((i-1)/m)^n. #include<bits/stdc++.h> using names

Codeforces Round #399 B 思维 C 模拟 D 概率dp E SG博弈

Divide by Zero 2017 and Codeforces Round #399 (Div. 1 + Div. 2, combined)B. Code For 1 题意:数n,不断拆分为 n/2, n&1, n/2,直到都为0或1.求区间[l, r]有多少个1. tags:画一画很容易看出来,类似dfs中序遍历. //#399 B #include<bits/stdc++.h> using namespace std; #pragma comment(linker, &quo

Codeforces 864 C Bus 思维

题目链接: http://codeforces.com/problemset/problem/864/C 题目描述: 输入a, b, f, k , 一段长度为a的路来回走, 中间f的地方有一个加油站, 油罐的容量为b, 问想要走b次这条路至少需要加多少次油 解题思路: 由于K <= 1e4, 所以将每次需要走路的连续序列构造出来再去遍历一遍这个序列看啥时候需要加油就可以了 代码: #include <iostream> #include <cstdio> #include &

codeforces 497B Tennis Game 思维+二分~

链接:http://codeforces.com/problemset/problem/497/B 这种题考察的是基本功..像我这种基本功为0的喳喳只能卡在这里了... 思路:n范围为10^5, 必须找出nlogn的算法才行.枚举t,然后用二分找出解..巧妙~~ 好题赞一个,虽然不会做~~~ 代码: /* *********************************************** Author :ltwy Created Time :2014年12月18日 星期四 16时28