(Easy Edition)

https://codeforces.com/contest/1163/problem/C1

(Hard Edition)

https://codeforces.com/contest/1163/problem/C2

题解:

C++版本一

/*
*@Author:   STZG
*@Language: C++
*/
#include <bits/stdc++.h>
#include<iostream>
#include<algorithm>
#include<cstdlib>
#include<cstring>
#include<cstdio>
#include<string>
#include<vector>
#include<bitset>
#include<queue>
#include<deque>
#include<stack>
#include<cmath>
#include<list>
#include<map>
#include<set>
//#define DEBUG
#define RI register int
#define endl "\n"
using namespace std;
typedef long long ll;
//typedef __int128 lll;
const int N=1000+10;
const int M=100000+10;
const int MOD=1e9+7;
const double PI = acos(-1.0);
const double EXP = 1E-8;
const int INF = 0x3f3f3f3f;
int t,n,m,k,p,l,r,u,v;
ll ans,cnt,flag,temp,sum;
struct node{
    double x,y;
}a[N];
double c[N<<10];
map<double,int>mp[N];
map<double,int>mmp;
int main()
{
#ifdef DEBUG
	freopen("input.in", "r", stdin);
	//freopen("output.out", "w", stdout);
#endif
    //ios::sync_with_stdio(false);
    //cin.tie(0);
    //cout.tie(0);
    //scanf("%d",&t);
    //while(t--){
    cin>>n;
    ll cnt=0,cct=0;;
    for(int i=1;i<=n;i++){
        scanf("%lf%lf",&a[i].x,&a[i].y);
        for(int j=1;j<i;j++){
            if(a[i].x==a[j].x){
                if(!mmp.count(a[i].x))
                cct++,mmp[a[i].x]=1;
            }else{
                double line=(a[i].y-a[j].y)/(a[i].x-a[j].x);
                if(!mp[i].count(line)&&!mp[j].count(line))
                    c[cnt++]=line;
                mp[i][line]=1;
                mp[j][line]=1;
            }
            //cout<<a[i].x<<' '<<a[j].x<<' '<<i<<' '<<j<<' '<<cnt<<' '<<cct<<endl;
        }
    }
    //cout<<cnt<<' '<<cct<<endl;
    sort(c,c+cnt);
    ll num=0;
    ll cot=1;
    for(int i=1;i<cnt;i++){
        if(c[i]!=c[i-1]){
            num+=(cot-1)*cot/2;
            cot=1;
        }else cot++;
    }
    num+=(cot-1)*cot/2;
    ans=(cnt)*(cnt-1)/2+(cct)*(cnt);
    cout<<ans-num<<endl;
    //}

#ifdef DEBUG
	printf("Time cost : %lf s\n",(double)clock()/CLOCKS_PER_SEC);
#endif
    //cout << "Hello world!" << endl;
    return 0;
}

C++版本二

#include<bits/stdc++.h>
using namespace std;
#define LL long long
#define PII pair<double,double>
#define PB push_back
#define POP pop_back
#define FI first
#define SE second
const int N=1e3+7,mod=1e9+7,INF=1e9;
int n,m;
struct s{
    double x,y;
}a[N];
double c[N<<10];
LL b[N<<10];
double d[N<<10];
bool cmp(s p,s q){
    if(p.x==q.x)return p.y<q.y;
    return p.x<q.x;
}
map<double,int>mp[N<<10];
map<double,int>mmp;
map<double,int>mmmp;
int main()
{
    cin>>n;
    LL cnt=0,cct=0;;
    for(int i=1;i<=n;i++){
        scanf("%lf%lf",&a[i].x,&a[i].y);
    }
    for(int i=1;i<=n;i++){
        for(int j=1;j<=n;j++){
            if(a[j].x==a[i].x&&a[j].y==a[i].y)continue;
            if(a[i].x==a[j].x){
                if(!mmp.count(a[i].x))
                cct++,mmp[a[i].x]=1;
            }
            else{
                if(!mp[i].count((a[i].y-a[j].y)/(a[i].x-a[j].x))&&!mp[j].count((a[i].y-a[j].y)/(a[i].x-a[j].x)))
                c[cnt++]=(a[i].y-a[j].y)/(a[i].x-a[j].x);
                mp[i][(a[i].y-a[j].y)/(a[i].x-a[j].x)]=1;
                mp[j][(a[i].y-a[j].y)/(a[i].x-a[j].x)]=1;
            }

            //cout<<a[i].x<<' '<<a[j].x<<' '<<i<<' '<<j<<' '<<cnt<<' '<<cct<<endl;
        }
    }
    //cout<<cnt<<' '<<cct<<endl;
    sort(c,c+cnt);
    int k=0;
    b[0]=1;d[0]=c[0];
    LL num=0;
    LL cot=1;
    for(int i=1;i<cnt;i++){
        if(c[i]!=c[i-1]){
            b[++k]=1;
            num+=(cot-1)*cot/2;
            cot=1;
        }
        else b[k]++,cot++;
    }
    num+=(cot-1)*cot/2;
    LL ans=(cnt)*(cnt-1)/2+(cct)*(cnt);
    /*
    for(int i=0;i<=k;i++){
        num+=b[k]*(b[k]-1)/2;
    }
    */
    cout<<ans-num;
    return 0;
}