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 price of electricity/battery is also sky high. To reduce power consumption the Japanese government is trying to descourage people from riding trains – as a result
the ticket price is also kept sky high and it is strictly proportional to the square of the distance between two stations.

All the trains move in clockwise or counter clockwise order in a closed triangular track. These triangular tracks can be formed by connecting any three stations in clockwise or counterclockwise order. For simplicity you can assume that a station is denoted
by a point in a two dimensional Cartesian Coordinate system. But these triangular tracks and ticket pricing policy can create new troubles. As the ticket price between two stations is proportional to the square of the distance, people often avoid the shortest
route to destination and rather choose the longer one through another station. This causes more electricity expense per passenger and creates unwanted crowd in the stations. So the government would prefer not to make such tracks.



Figure 1: The figure above shows 6 places. It also shows all possible triangular tracks (not necessarily valid site) by connecting them. The green track is one invalid track site, on the other hand the red track is one valid track site. There are five other
valid track sites in the above figure. fv

For example in the figure on the left you can see a closed triangular track marked with green. If someone wants to go from station D to station E he can go directly by riding a clockwise train or can go via station C by riding a counter clockwise train:
That is he first buys ticket from station D to C and then he buys ticket of station C to E. But in the current ticket pricing system the route via C (which is also much longer) will be cheaper. So this site CED is not a place to build a track. For the similar
reasons AEB is a valid site for building track. On a valid track the shortest distance between any two stations is also the unique cheapest route between them. Given the coordinate of all stations you will have to find the number of sites (a group of three
places) for valid tracks.

Input

The input file contains at most 15 sets of inputs. The description of each set is given below:

Each set starts with an n (2<n<1201) which denotes the number of stations. Each of the next n lines contains two integer xi, yi (0≤xi, yi≤10000) which denotes the Cartesian coordinate of the i-th station. You can
assume that a track can be built via through any three stations, no three places will be collinear to avoid the problem of degenerate tracks and the connecting railroad between two stations can always be represented by the straight line connecting them.

Output

For each set of input produce two line of output. The first line contains the serial of output and the second line displays the total number of sites where a track can be built. Look at the output for sample input for details.

Sample Input            Output for Sample Input

6 
26 23 
51 94 
103 110 
164 107 
116 67 
73 16 
2 
1 1 
2 2 
0 

Scenario 1:

There are 6 sites for making valid tracks

Scenario 2:

There are 0 sites for making valid tracks

 


Problem setter: Shahriar Manzoor, Special Thanks: Derek Kisman

题意:给定平面上n个无三点共线的点,求这些点组成多少个锐角或直角三角形

思路:首先明确一个直角和钝角都唯一对应一个三角形,所以我们不去统计比较难统计的锐角,先计算钝角的可能注意精度,再减去

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
typedef long long ll;
using namespace std;
const int maxn = 1250;
const double pi= acos(-1.0);
const double eps = 1e-9;

struct point {
	double x, y;
} p[maxn];
double du[maxn<<1];

int main() {
	int t, n, cas = 1;
	while (scanf("%d", &n) != EOF && n) {
		for (int i = 0; i < n; i++)
			scanf("%lf%lf", &p[i].x, &p[i].y);
		ll tmp = 0;
		for (int i = 0; i < n; i++) {
			if (i)
				swap(p[i], p[0]);
			for (int k = 1; k < n; k++)
				du[k] = atan2(p[k].y-p[0].y, p[k].x-p[0].x);
			sort(du+1, du+n);
			for (int k = 1; k < n; k++)
				du[k+n-1] = du[k] + 2 * pi;
			int cnt1 = 1, cnt2 = 1;
			for (int k = 1; k < n; k++) {
				while (du[cnt1] - du[k] <= 0.5*pi-eps)
					cnt1++;
				while (du[cnt2] - du[k] <= pi)
					cnt2++;
				tmp += cnt2-cnt1;
			}
		}

		ll ans = n*(n-1)*(n-2) / 6 - tmp;
		printf("Scenario %d:\n", cas++);
		printf("There are %lld sites for making valid tracks\n", ans);
	}
	return 0;
}
时间: 2024-10-15 22:06:05

UVA - 12123 Magnetic Train Tracks的相关文章

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

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

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

UVaLive 4064 Magnetic Train Tracks (极角排序)

题意:给定 n 个不三点共线的点,然后问你能组成多少锐角或者直角三角形. 析:可以反过来求,求有多少个钝角三角形,然后再用总的减去,直接求肯定会超时,但是可以枚举每个点,以该点为钝角的那个顶点,然后再枚举另一条边,维护与该边大于90度并小于等于180度的点的数量,这里要用极角排序,这样就可以减小时间复杂度,但是会WA,要控制精度,但是精度是个迷,表示不会. 代码如下: #pragma comment(linker, "/STACK:1024000000,1024000000") #in

UVa 11586 - Train Tracks

题目:给你一些积木碎片,每个碎片的两端只能是凸或凹(M或F),凸凹可拼起来,能否拼成一个环. 分析:图论,欧拉回路.判断入度等于出度即可,即M和F相同且大于1组. 说明:╮(╯▽╰)╭. #include <cstring> #include <cstdio> char buf[202]; int main() { int n; while (~scanf("%d",&n)) { getchar(); while (n --) { gets(buf);

《算法竞赛入门经典——训练指南》第二章题库

UVa特别题库 UVa网站专门为本书设立的分类题库配合,方便读者提交: http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=442 注意,下面注有"extra"的习题并没有在书中出现,但在上面的特别题库中有,属于附加习题. 基础练习 (Basic Problems) UVa11388 GCD LCM UVa11889 Benefit UVa10943 How do y

计算几何题目分类

转载 一.基础题目 1.1 有固定算法的题目 A, 最近点对问题最近点对问题的算法基于扫描线算法.ZOJ 2107    Quoit Design    典型最近点对问题POJ    3714    Raid    变种最近点对问题 B,最小包围圆最小包围圆的算法是一种增量算法,期望是O(n).ZOJ    1450    Minimal Circle  HDU    3007    Buried memory C,旋转卡壳POJ 3608    Bridge Across Islands   

jQuery中的supersized的插件的功能描述

Supersized特性: 自动等比例调整图片并填充整浏览器个屏幕. 循环展示图片,支持滑动和淡入淡出等多种图片切换效果. 导航按钮,支持键盘方向键导航. XHTML <div id="loading"> </div>  <div id="supersized"></div>    <div id="prevthumb"></div>  <div id="ne

UVa 299 - Train Swapping

题目:给你一个序列,每次只能交换相邻的两个数,问使得序列递增的最小的交换次数. 分析:数学,逆序数. 如果在一个序列中存在a[i] > a[j]并且 i < j,则a[i]与a[j]构成一对逆序对: 一个序列的逆序对的总数,就是这个序列的逆序数: 如题,相邻元素交换,每次交换序列逆序数必然改变1,而一个递增的序列逆序数为0: 因此,最少的交换次数即为逆序数,而每次按照逆序对减少的方式交换就得到递增序列. 说明:整理很久以前刷过的题目. #include <iostream> #in