[noip模拟题]合理种植

【问题描述】

   大COS在氯铯石料场干了半年,受尽了劳苦,终于决定辞职。他来到表弟小cos的寒树中学,找到方克顺校长,希望寻个活干。

      于是他如愿以偿接到了一个任务……

美丽寒树中学种有许多寒树。方克顺希望校园无论从什么角度看都是满眼寒树,因此他不希望有三棵甚至更多寒树种在一条直线上。现在他把校园里n棵寒树的坐标都给了大COS,让他数出存在多少多树共线情况。(若一条直线上有三棵或以上的树,则算出现一个多树共线情况。)

【输入】

      输入文件名为plant.in。

      第1行一个正整数n,表示寒树棵数。

      接下来n行,每行两个非负整数x、y,表示一颗寒树的坐标。没有两颗寒树在同一位置。

【输出】

      输出文件名为plant.out。

      输出一个整数,表示存在多少多树共线情况。

 

【输入输出样例】

plant.in

plant.out

6

0 0

1 1

2 2

3 3

0 1

1 0

1

 

【数据范围】

      对于30%的数据,有n≤10;

      对于50%的数据,有n≤100;

      对于100%的数据,有n≤1,000,0≤x,y≤10,000。


  这道题纯属数学题,先O(n2)跑一道,把任意两点之间的斜率求出来,再用O(n3)判断三条个点是否共线,至于判重嘛。。就自己想一下了

  (论常数的重要性)

Code

 1 #include<iostream>
 2 #include<fstream>
 3 #include<cstring>
 4 using namespace std;
 5 ifstream fin("plant.in");
 6 ofstream fout("plant.out");
 7 typedef bool boolean;
 8 typedef class Point{
 9     public:
10         int x;
11         int y;    
12 }Point;
13 istream& operator >>(istream& in,Point& p){
14     in>>p.x>>p.y;
15 }
16 Point *ps;
17 int n;
18 int _x,_y;
19 long long ks[1001][1001];
20 boolean vis[1001][1001];
21 long long result = 0;
22 int main(){
23     fin>>n;
24     memset(vis, false, sizeof(vis));
25     ps = new Point[(const int)(n + 1)];
26     for(int i = 1;i <= n;i++)
27         fin>>ps[i];
28     for(int i = 1;i <= n;i++){
29         for(int j = i + 1;j <= n;j++){
30             _x = ps[j].x - ps[i].x;
31             _y = ps[j].y - ps[i].y;
32             if(_x == 0)    ks[i][j] = 1;
33             else ks[i][j] = (_y * 100000000LL)/_x;
34         }
35     }
36     for(int i = 1;i < n - 1;i++){
37         for(int j = i + 1; j < n;j++){
38             for(int k = j + 1; k <= n;k++){
39                 if(ks[i][j] == ks[j][k] && ks[i][j] == ks[i][k]){
40                     if(vis[i][j]||vis[j][k]||vis[i][k]){
41                         vis[i][j] = vis[j][k] = vis[i][k] = true;
42                     }else{
43                         vis[i][j] = vis[j][k] = vis[i][k] = true;
44                         result++;
45                     }
46                 }
47             }
48         }
49     }
50     fout<<result;
51     return 0;
52 }

 

posted @ 2016-07-15 20:20  阿波罗2003  阅读(304)  评论(0编辑  收藏  举报