首页 > 代码库 > UVA - 12123 Magnetic Train Tracks
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; }
UVA - 12123 Magnetic Train Tracks