UVaLive 4064 Magnetic Train Tracks (极角排序)

题意:给定 n 个不三点共线的点,然后问你能组成多少锐角或者直角三角形。

析:可以反过来求,求有多少个钝角三角形,然后再用总的减去,直接求肯定会超时,但是可以枚举每个点,以该点为钝角的那个顶点,然后再枚举另一条边,维护与该边大于90度并小于等于180度的点的数量,这里要用极角排序,这样就可以减小时间复杂度,但是会WA,要控制精度,但是精度是个迷,表示不会。

代码如下:

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <cstring>
#include <set>
#include <queue>
#include <algorithm>
#include <vector>
#include <map>
#include <cctype>
#include <cmath>
#include <stack>
#include <sstream>
#include <list>
#include <assert.h>
#include <bitset>
#include <numeric>
#define debug() puts("++++")
#define gcd(a, b) __gcd(a, b)
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define fi first
#define se second
#define pb push_back
#define sqr(x) ((x)*(x))
#define ms(a,b) memset(a, b, sizeof a)
#define sz size()
#define be begin()
#define ed end()
#define pu push_up
#define pd push_down
#define cl clear()
#define lowbit(x) -x&x
//#define all 1,n,1
#define FOR(i,n,x)  for(int i = (x); i < (n); ++i)
#define freopenr freopen("in.in", "r", stdin)
#define freopenw freopen("out.out", "w", stdout)
using namespace std;

typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int, int> P;
const int INF = 0x3f3f3f3f;
const LL LNF = 1e17;
const double inf = 1e20;
const double PI = acos(-1.0);
const double eps = 1e-8;
const int maxn = 1500 + 50;
const int maxm = 1e6 + 10;
const LL mod = 1000000000000000LL;
const int dr[] = {-1, 1, 0, 0, 1, 1, -1, -1};
const int dc[] = {0, 0, 1, -1, 1, -1, 1, -1};
const char *de[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
int n, m;
const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
inline bool is_in(int r, int c) {
  return r >= 0 && r < n && c >= 0 && c < m;
}
inline int readInt(){ int x;  scanf("%d", &x);  return x; }

int a[maxn], b[maxn];

double p[maxn<<1];

int main(){
  int kase = 0;
  while(scanf("%d", &n) == 1 && n){
    for(int i = 0; i < n; ++i)  scanf("%d %d", a + i, b + i);
    int ans = 0;
    for(int i = 0; i < n; ++i){
      int cnt = 0;
      for(int j = 0; j < n; ++j)  if(i != j)
        p[cnt++] = atan2(b[j]-b[i], a[j]-a[i]);
      sort(p, p + cnt);
      for(int j = 0; j < cnt; ++j)  p[j+cnt] = p[j] + PI * 2.;
      int k = 0, l = 0;
      for(int j = 0; j < cnt; ++j){
        while(p[k] - p[j] + eps <= PI / 2.)  ++k;
        while(p[l] - p[j] + eps < PI)  ++l;
        ans += l - k;
      }
    }
    ans = n * (n-1) * (n-2) / 6 - ans;
    if(n < 3)  ans = 0;
    printf("Scenario %d:\n", ++kase);
    printf("There are %d sites for making valid tracks\n", ans);
  }
  return 0;
}

  

原文地址:https://www.cnblogs.com/dwtfukgv/p/8970060.html

时间: 2024-08-05 14:10:37

UVaLive 4064 Magnetic Train Tracks (极角排序)的相关文章

LA 4064 Magnetic Train Tracks

题意:给定平面上$n(3\leq n \leq 1200)$个无三点共线的点,问这些点组成了多少个锐角三角形. 分析:显然任意三点可构成三角形,而锐角三角形不如直角或钝角三角形容易计数,因为后者有且仅有一个角度大于等于$90^{\circ}$的特征角. 于是考虑固定平面上每一个顶点,也就是固定了钝角或直角三角形的一个特征顶点,将其余所有点按照极角排序,然后固定一条侧边,统计有多少条 边和该侧边夹角不小于$90^{\circ}$.这些边必然是连续的,可以使用区间统计的办法,用二分查找在$O(log

UVA 12123 - Magnetic Train Tracks(计数问题)

题目链接:12123 - Magnetic Train Tracks 题意:给定n个点,求有几个锐角三角形. 思路:和UVA 11529是同类的题,枚举一个做原点,然后剩下点根据这个原点进行极角排序,然后利用two pointer去遍历一遍,找出角度小于90度的锐角,然后扣掉这些得到钝角三角形的个数,然后在用总情况去扣掉钝角就是锐角或直角 代码: #include <stdio.h> #include <string.h> #include <math.h> #incl

LA 4064 (计数 极角排序) Magnetic Train Tracks

这个题和UVa11529很相似. 枚举一个中心点,然后按极角排序,统计以这个点为钝角的三角形的个数,然后用C(n, 3)减去就是答案. 另外遇到直角三角形的情况很是蛋疼,可以用一个eps,不嫌麻烦的话就用整数的向量做点积. 1 #include <cstdio> 2 #include <cmath> 3 #include <algorithm> 4 using namespace std; 5 6 typedef long long LL; 7 const int ma

UVA - 12123 Magnetic Train Tracks

Description The rail roads of Japan are being redesigned. So the governent is planning to install ultra-modern Magnetic trains instead of the current normal trains. As fuel price have gone high and nations have shut down their nuclear plants so the p

UVALive 5102 Fermat Point in Quadrangle 极角排序+找距离二维坐标4个点最近的点

题目链接:点击打开链接 题意: 给定二维坐标上的4个点 问: 找一个点使得这个点距离4个点的距离和最小 输出距离和. 思路: 若4个点不是凸4边形.则一定是端点最优. 否则就是2条对角线的交点最优,可以简单证明一下. 对于凸4边形则先极角排序一下. #include <cstdio> #include <cstring> #include <cmath> #include <algorithm> using namespace std; typedef do

UVALive 5102 Fermat Point in Quadrangle 极角排序+找距离二维坐标4个点近期的点

题目链接:点击打开链接 题意: 给定二维坐标上的4个点 问: 找一个点使得这个点距离4个点的距离和最小 输出距离和. 思路: 若4个点不是凸4边形.则一定是端点最优. 否则就是2条对角线的交点最优,能够简单证明一下. 对于凸4边形则先极角排序一下. #include <cstdio> #include <cstring> #include <cmath> #include <algorithm> using namespace std; typedef do

极角排序+求锐角三角形个数——uva12123

这个版本还不能处理三点共线的情况(处理起来其实比较麻烦) 可以用atan2来排序(较为简单,但是精度误差大),也可以用叉积排序(比较优秀) atan2 #include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <cmath> #include <cstdlib> using namespace std; typedef

Hdu5032 极角排序+树状数组

题目链接 思路:参考了题解.对询问进行极角排序,然后用树状数组维护一下前缀和即可. /* ID: onlyazh1 LANG: C++ TASK: test */ #include<bits/stdc++.h> using namespace std; #define lson l,m,rt<<1 #define rson m+1,r,rt<<1|1 typedef long long ll; const int maxn=1010; const int maxm=10

Codeforces Round #124 (Div. 1) C. Paint Tree(极角排序)

C. Paint Tree time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output You are given a tree with n vertexes and n points on a plane, no three points lie on one straight line. Your task is to paint