Codeforces 552D Vanya and Triangles【暴力枚举+思维】
2017-03-15 13:18
393 查看
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个点,让你找到面积不为0的三角形的个数。
思路:
显然直接枚举三个点出来判断是否为三角形时间复杂度为O(n^3),即使是CF的评测机,4s也肯定接受不了8e9的操作数量。
那么我们考虑思维优化。
观察到点的范围处于【-100,100】之间,那么我们可以先枚举出来两个点,已知一共有n个点,那么与这两个点处于同一直线上的点去掉,其他点都可以与这两个点构成一个三角形。
那么我们O(n^2)去枚举两个点,很显然,通过两点法:(x-x1)/(x2-x1)=(y-y1)/(y2-y1)能够很容易通过枚举一个x来求得一个y.(而且输入都是整数,所以要求求y的时候 ,一定要能够整除才行);同时,对于这个点(x,y)进行去除。那么不和这两个点处于同一直线的其他点的个数就得以统计了。
ans=sum/3(一个三角形肯定会重复三次被统计).
注意几点:
①枚举出来的两点处于一条直线上。
②枚举出来的x所求的y可能出现数组越界的情况。
③处理好数组大小,以及负数点的表达。
Ac代码:
#include<stdio.h>
#include<string.h>
using namespace std;
int a[2200][2];
int vis[300][300];
int main()
{
int n;
while(~scanf("%d",&n))
{
int mid=150;
memset(vis,0,sizeof(vis));
for(int i=0;i<n;i++)
{
int x,y;
scanf("%d%d",&x,&y);
vis[mid+x][mid+y]++;
a[i][0]=x;a[i][1]=y;
}
__int64 output=0;
for(int i=0;i<n;i++)
{
for(int j=i+1;j<n;j++)
{
int sum=n;
int x1=a[i][0],y1=a[i][1],x2=a[j][0],y2=a[j][1];
if(x1==x2||y1==y2)
{
if(x1==x2)
{
int x=x1;
for(int y=-100;y<=100;y++)
{
sum-=vis[x+mid][y+mid];
}
}
if(y1==y2)
{
int y=y1;
for(int x=-100;x<=100;x++)
{
sum-=vis[x+mid][y+mid];
}
}
}
else
{
for(int x=-100;x<=100;x++)
{
int fenzi=(x-x1)*(y2-y1);
int fenmu=(x2-x1);
if(fenmu==0)continue;
if(fenzi%fenmu==0)
{
int y=fenzi/fenmu+y1;
if(y>=-100&&y<=100)
sum-=vis[x+mid][y+mid];
}
}
}
output+=sum;
}
}
printf("%I64d\n",output/3);
}
}
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个点,让你找到面积不为0的三角形的个数。
思路:
显然直接枚举三个点出来判断是否为三角形时间复杂度为O(n^3),即使是CF的评测机,4s也肯定接受不了8e9的操作数量。
那么我们考虑思维优化。
观察到点的范围处于【-100,100】之间,那么我们可以先枚举出来两个点,已知一共有n个点,那么与这两个点处于同一直线上的点去掉,其他点都可以与这两个点构成一个三角形。
那么我们O(n^2)去枚举两个点,很显然,通过两点法:(x-x1)/(x2-x1)=(y-y1)/(y2-y1)能够很容易通过枚举一个x来求得一个y.(而且输入都是整数,所以要求求y的时候 ,一定要能够整除才行);同时,对于这个点(x,y)进行去除。那么不和这两个点处于同一直线的其他点的个数就得以统计了。
ans=sum/3(一个三角形肯定会重复三次被统计).
注意几点:
①枚举出来的两点处于一条直线上。
②枚举出来的x所求的y可能出现数组越界的情况。
③处理好数组大小,以及负数点的表达。
Ac代码:
#include<stdio.h>
#include<string.h>
using namespace std;
int a[2200][2];
int vis[300][300];
int main()
{
int n;
while(~scanf("%d",&n))
{
int mid=150;
memset(vis,0,sizeof(vis));
for(int i=0;i<n;i++)
{
int x,y;
scanf("%d%d",&x,&y);
vis[mid+x][mid+y]++;
a[i][0]=x;a[i][1]=y;
}
__int64 output=0;
for(int i=0;i<n;i++)
{
for(int j=i+1;j<n;j++)
{
int sum=n;
int x1=a[i][0],y1=a[i][1],x2=a[j][0],y2=a[j][1];
if(x1==x2||y1==y2)
{
if(x1==x2)
{
int x=x1;
for(int y=-100;y<=100;y++)
{
sum-=vis[x+mid][y+mid];
}
}
if(y1==y2)
{
int y=y1;
for(int x=-100;x<=100;x++)
{
sum-=vis[x+mid][y+mid];
}
}
}
else
{
for(int x=-100;x<=100;x++)
{
int fenzi=(x-x1)*(y2-y1);
int fenmu=(x2-x1);
if(fenmu==0)continue;
if(fenzi%fenmu==0)
{
int y=fenzi/fenmu+y1;
if(y>=-100&&y<=100)
sum-=vis[x+mid][y+mid];
}
}
}
output+=sum;
}
}
printf("%I64d\n",output/3);
}
}
相关文章推荐
- Codeforces 466B Wonder Room【思维+暴力枚举】直觉题
- Codeforces 426D Sereja and Table【思维+暴力枚举】好题!
- Codeforces 769D k-Interesting Pairs Of Integers【思维+预处理+暴力枚举】
- Hrbust 1789 通信道路【思维+暴力枚举】
- codeforces 380B Sereja and Tree 暴力枚举
- Codeforces 425A Sereja and Swaps(暴力枚举)
- HDU 3835R(N)(加点思维的暴力枚举)
- CodeForces 379D 暴力 枚举
- codeforces 324# E. Anton and Ira (暴力枚举+贪心)
- Codeforces 782C Andryusha and Colored Balloons【思维+Dfs暴力染色】
- Codeforces 425A Sereja and Swaps(暴力枚举)
- Codeforces Round #378 (Div. 2) && codeforces 733D(思维枚举)
- Codeforces 355C Vasya and Robot 暴力枚举
- codeforces 898B. Proper Nutrition(暴力+思维)
- Codeforces 673C Bear and Colors【暴力枚举】
- problem/868/C Qualification Rounds 思维题 状态压缩 暴力枚举
- CodeForces 724B(暴力枚举)
- Codeforces 3 B. Lorry 暴力 排序 枚举 贪心
- codeforces 898B. Proper Nutrition(暴力+思维)
- COdeforces 479A - Expression (暴力枚举即可)