Codeforces Round #308 (Div. 2) D. Vanya and Triangles (判断三角形数量)
2016-04-15 09:46
423 查看
D. Vanya and Triangles
time limit per test
4 seconds
memory limit per test
512 megabytes
input
standard input
output
standard output
Vanya got bored and he painted n distinct points on the plane. After that he connected all the points pairwise and saw that
as a result many triangles were formed with vertices in the painted points. He asks you to count the number of the formed triangles with the non-zero area.
Input
The first line contains integer n (1 ≤ n ≤ 2000)
— the number of the points painted on the plane.
Next n lines contain two integers each xi, yi ( - 100 ≤ xi, yi ≤ 100)
— the coordinates of the i-th point. It is guaranteed that no two given points coincide.
Output
In the first line print an integer — the number of triangles with the non-zero area among the painted points.
Examples
input
output
input
output
input
output
Note
Note to the first sample test. There are 3 triangles formed: (0, 0) - (1, 1) - (2, 0); (0, 0) - (2, 2) - (2, 0); (1, 1) - (2, 2) - (2, 0).
Note to the second sample test. There is 1 triangle formed: (0, 0) - (1, 1) - (2, 0).
Note to the third sample test. A single point doesn't form a single triangle.
题意:
给出n个点,求将n个点相互连线之后,可组成多少个三角形。
分析:
本题时间给的太宽裕,n^3的暴力枚举加快速判断是否能形成三角形就可以解决,不过每个三角形都计算过3次,所以最终结果需要除以3。
更快速的方法是n^2的逐个按点枚举,将斜率存储下来。若该斜率大于1的话,证明该直线上存在2个以上的点,通过计算该斜率出现多少次,即可知道重复计算了哪些三角形。而n个点最多可形成n*(n-1)*(n-2)/3!个三角形,将重复计算的三角形除去即可。为了防止精度问题,将斜率的分子分母约分后,分别存储即可,本题采用了分子+分母*1000,作为该斜率的存储。
time limit per test
4 seconds
memory limit per test
512 megabytes
input
standard input
output
standard output
Vanya got bored and he painted n distinct points on the plane. After that he connected all the points pairwise and saw that
as a result many triangles were formed with vertices in the painted points. He asks you to count the number of the formed triangles with the non-zero area.
Input
The first line contains integer n (1 ≤ n ≤ 2000)
— the number of the points painted on the plane.
Next n lines contain two integers each xi, yi ( - 100 ≤ xi, yi ≤ 100)
— the coordinates of the i-th point. It is guaranteed that no two given points coincide.
Output
In the first line print an integer — the number of triangles with the non-zero area among the painted points.
Examples
input
4 0 0 1 1 2 0 2 2
output
3
input
30 01 12 0
output
1
input
11 1
output
0
Note
Note to the first sample test. There are 3 triangles formed: (0, 0) - (1, 1) - (2, 0); (0, 0) - (2, 2) - (2, 0); (1, 1) - (2, 2) - (2, 0).
Note to the second sample test. There is 1 triangle formed: (0, 0) - (1, 1) - (2, 0).
Note to the third sample test. A single point doesn't form a single triangle.
题意:
给出n个点,求将n个点相互连线之后,可组成多少个三角形。
分析:
本题时间给的太宽裕,n^3的暴力枚举加快速判断是否能形成三角形就可以解决,不过每个三角形都计算过3次,所以最终结果需要除以3。
更快速的方法是n^2的逐个按点枚举,将斜率存储下来。若该斜率大于1的话,证明该直线上存在2个以上的点,通过计算该斜率出现多少次,即可知道重复计算了哪些三角形。而n个点最多可形成n*(n-1)*(n-2)/3!个三角形,将重复计算的三角形除去即可。为了防止精度问题,将斜率的分子分母约分后,分别存储即可,本题采用了分子+分母*1000,作为该斜率的存储。
#include <algorithm> #include <iostream> #include <sstream> #include <cstring> #include <cstdlib> #include <string> #include <vector> #include <cstdio> #include <cmath> #include <queue> #include <stack> #include <map> #include <set> using namespace std; #define INF 0x3f3f3f3f const int N=2015; const int mod=1e9+7; struct node { int x,y; }p ; int x[N*100]; long long sum=0; int gcd(int a,int b) { return b==0?a:gcd(b, a%b); } bool cmp(node a,node b) { return a.x>b.x; } int solve(int i,int j) { int xx=p[i].x-p[j].x; int yy=p[i].y-p[j].y; if (xx<0) { xx*=-1; yy*=-1; } if (xx==0) { yy=abs(yy); } int t=gcd(xx, abs(yy)); xx/=t; yy/=t; yy+=200; return xx*1000+yy; } int main() { int n; while (cin>>n) { for (int i=0; i<n; i++) { scanf("%d %d",&p[i].x,&p[i].y); } sort(p, p+n, cmp); sum=(long long)n*(n-1)*(n-2); long long temp=0; for (int i=0; i<n; i++) { temp=0; memset(x, 0, sizeof(x)); for (int j=0; j<n; j++) { if (i==j) continue; x[solve(i, j)]++; } for (int j=0; j<n; j++) { if (i==j) continue; int flag=x[solve(i, j)]+1; x[solve(i, j)]=0; if (flag>2) { temp+=(flag-1)*(flag-2); } } sum-=temp; } sum/=6; cout<<sum<<endl; } return 0; }
相关文章推荐
- Spring自动注入properties文件
- 子线程执行scheduledTimer
- 长时间对着电脑眼睛会收到很大辐射教你五种对付方法
- ScrollView与ListView的焦点问题,互相滑动互不影响
- 关于ajax解析
- Java实现RC4加解密
- Oracle 获取当前日期及日期格式
- 复利计算评论博客地址
- 图片截取插件Cropper
- 小小图片爬虫
- bzoj 4499: 线性函数
- js中“-”符号的问题
- CFRunLoopObserverRef
- 创建CFRunLoopObserverRef
- error: linker command failed with exit code 1 (use -v to see invocation)
- 使用苹果airport路由器的WDS功能扩展wifi覆盖范围的设置方法
- 翻页特效
- Ubuntu 12.10 安装JDK7
- 【bzoj3790】【神奇项链】【manacher+dp+树状数组】
- jQuery获取cookie值及删除cookie用法实例