[Codeforces] 478 D. Ghosts

題目連結:http://codeforces.com/contest/975/problem/D
  列方程式然後解可得:a = (v1-v2)/(w1-w2) => av1-w1 = av2-w2,這樣就好做了,不過要判掉v1 == v2 && w1 == w2的case。


#include<bits/stdc++.h>
#define debug(a) cout << #a << " = " << a << endl;
#define pb push_back
#define all(v) (v).begin(),(v).end()
#define int long long
using namespace std;
const int N = 200000+9;
int n,a,b,ans;
int v[N], w[N]; 
pair<int,int> h[N];
int k[N];
main()
{
    cin >> n >> a >> b;
    for(int i = 0; i < n; i++) cin >> b >> v[i] >> w[i];
    for(int i = 0; i < n; i++) h[i] = {v[i],w[i]};
    for(int i = 0; i < n; i++) k[i] = v[i]*a-w[i];
    sort(k,k+n);
    int cnt = 0;
    if(n == 1)
    {
        cout << 0 << endl;
        return 0;
    }
    sort(h,h+n);
    for(int i = 0; i < n; i++) cnt += upper_bound(h+i+1,h+n,h[i])-lower_bound(h+i+1,h+n,h[i]);
    //for(int i = 0; i < n; i++) cout << h[i].first << " " << h[i].second << endl;
    for(int i = 0; i < n; i++) ans += upper_bound(k+i+1,k+n,k[i])-lower_bound(k+i+1,k+n,k[i]);
    //debug(ans); debug(cnt);
    cout << max(0ll,2*(ans-cnt)) << endl;
}

留言

這個網誌中的熱門文章

Shellshock.io從入門到上手(針對單狙)(沒有圖片、影片版本)

[TIOJ] 1007燈泡問題