codeforces1468F Full Turn

题意

给定(n)个点(p_i=(x_i, y_i)),第(i)个点初始时朝向((tx_i, ty_i))。每个点的朝向以相同的角速度转动,转满一圈停止。

(A)和点(B)对视,即(B)在点(A)朝向的方向上,且(A)在点(B)朝向的方向上。两点中间有其他点不会影响对视。

问满足某一时刻两点对视的点对数。

解法

性质1:若某一时刻,(A)(B)对视,则此时两点朝向的夹角为(180)度。

性质2:因为角速度相同,所以任意时刻,两点朝向的夹角等于初始时的夹角。

根据这两点性质,可以推出满足某一时刻两点对视的点对数即为初始时朝向夹角为(180)度的点对数。

然后因为可能会有精度问题,所以并不能直接用三角函数来算夹角。

令第(p_i)的方向向量为(vec d_i = (tx_i - x_i, ty_i - y_i) = (dx_i, dy_i))。若将(vec d_i)标准化,则(p_i)(p_j)初始朝向夹角为(180)度当且仅当(vec d_i + vec d_j = vec 0)

标准化的方式就是,若(vec d_i)水平或者竖直,就令(vec d_i = frac{vec d_i}{|vec d_i|});否则,就让(vec d_i = (frac{dx_i}{g}, frac{dy_i}{g})),其中(g = GCD(dx_i, dy_i))

然后可以用一个map乱搞完事了。

AC代码

#include <bits/stdc++.h>
using namespace std;
 
using ll = int64_t;
using ull = uint64_t;
using uint = uint32_t;
using VI = vector<int>;
using VL = vector<ll>;
using VVI = vector<vector<int>>;
using VVL = vector<vector<ll>>;
using PII = pair<int,int>;
using PLL = pair<ll, ll>;
 
#define REP(i, _, __) for (int i = (_); i < (__); ++i)
#define PER(i, _, __) for (int i = (_-1); i >= (__); --i)
#define FOR(i, _, __) for (int i = (_); i <= (__); ++i)
#define ROF(i, _, __) for (int i = (_); i >= (__); --i)
#define FC(v, V) for (const auto& v: V)
#define FE(v, V) for (auto& v: V)

#define EB emplace_back
#define PB push_back
#define MP make_pair
#define FI first
#define SE second
#define SZ(x) (int((x).size()))
#define ALL(x) (x).begin(),(x).end()
#define LLA(x) (x).rbegin(),(x).rend()

const double PI = acos(-1.0);
   
namespace Backlight {
    const int __BUFFER_SIZE__ = 1 << 20;
    bool NEOF = 1;
    int __top;
    char __buf[__BUFFER_SIZE__], *__p1 = __buf, *__p2 = __buf, __stk[996];

    template<typename T>
    T MIN(T a, T b) { return min(a, b); }

    template<typename First, typename... Rest>
    First MIN(First f, Rest... r) { return min(f, MIN(r...)); }

    template<typename T>
    T MAX(T a, T b) { return max(a, b); }

    template<typename First, typename... Rest>
    First MAX(First f, Rest... r) { return max(f, MAX(r...)); }

    template<typename T>
    void updMin(T& a, T b) { if (a > b) a = b; }

    template<typename T>
    void updMax(T& a, T b) { if (a < b) a = b; }

    inline char nc() {
        return __p1 == __p2 && NEOF && (__p2 = (__p1 = __buf) + fread(__buf, 1, __BUFFER_SIZE__, stdin), __p1 == __p2) ? (NEOF = 0, EOF) : *__p1++;
    }
   
    template<typename T>
    inline bool read(T &x) {
        char c = nc();
        bool f = 0; x = 0;
        while (!isdigit(c)) c == '-' && (f = 1), c = nc();
        while (isdigit(c)) x = (x << 3) + (x << 1) + (c ^ 48), c = nc();
        if (f) x = -x;
        return NEOF;
    }

    inline bool need(char c) { return (c != '
') && (c != ' '); }

    inline bool read(char& a) {
        while ((a = nc()) && need(a) && NEOF) ;
        return NEOF;
    }

    inline bool read(char *a) {
        while ((*a = nc()) && need(*a) && NEOF) ++a; 
        *a = '';
        return NEOF;
    }

    inline bool read(double &x) {
        bool f = 0; char c = nc(); x = 0;
        while (!isdigit(c))  { f |= (c == '-'); c = nc(); }
        while (isdigit(c)) { x = x * 10.0 + (c ^ 48); c = nc(); }
        if (c == '.') {
            double temp = 1; c = nc();
            while (isdigit(c)) { temp = temp / 10.0; x = x + temp * (c ^ 48); c = nc(); }
        }
        if (f) x = -x;
        return NEOF;
    }

    template<typename First, typename... Rest>
    inline bool read(First &f, Rest &... r) {
        read(f);
        return read(r...);
    }

    template<typename T>
    inline void print(T x) {
        if (x < 0) putchar('-'), x = -x;
        if (x == 0) { putchar('0'); return; }
        __top = 0;
        while(x) {
            __stk[++__top] = x % 10 + '0';
            x /= 10;
        }
        while(__top) {
            putchar(__stk[__top]);
            --__top;
        }
    }

    template<typename First, typename... Rest>
    inline void print(First f, Rest... r) {
        print(f); putchar(' ');
        print(r...);
    }

    template<typename T>
    inline void println(T x) {
        print(x); 
        putchar('
');
    }

    template<typename First, typename... Rest>
    inline void println(First f, Rest... r) {
        print(f); putchar(' ');
        println(r...);
    }

    template<typename T>
    inline void _dbg(const char *format, T value) { cerr << format << '=' << value << endl; }
   
    template<typename First, typename... Rest>
    inline void _dbg(const char *format, First f, Rest... r) {
        while(*format != ',') cerr << *format++;
        cerr << '=' << f << ", ";
        _dbg(format + 1, r...);
    }
      
    template<typename T>
    ostream &operator<<(ostream& os, vector<T> V) {
        os << "[ "; for (auto v : V) os << v << ","; return os << " ]";
    }
   
    template<typename T>
    ostream &operator<<(ostream& os, set<T> V) {
        os << "[ "; for (auto v : V) os << v << ","; return os << " ]";
    }

    template<typename T>
    ostream &operator<<(ostream& os, multiset<T> V) {
        os << "[ "; for (auto v : V) os << v << ","; return os << " ]";
    }
 
    template<typename T1, typename T2>
    ostream &operator<<(ostream& os, map<T1, T2> V) {
        os << "[ "; for (auto v : V) os << v << ","; return os << " ]";
    }
  
    template<typename L, typename R>
    ostream &operator<<(ostream &os, pair<L, R> P) {
        return os << "(" << P.first << "," << P.second << ")";
    }

    #ifdef BACKLIGHT
    #define debug(...) cerr << "33[31m" << "[" << __LINE__ << "] : "; _dbg(#__VA_ARGS__, __VA_ARGS__); cerr << "33[0m";
    // #define debug(...) _dbg(#__VA_ARGS__, __VA_ARGS__); 
    #else
    #define debug(...)
    #endif
}

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
int rnd(int l, int r) { return l + rng() % (r - l + 1); }

using namespace Backlight;
const int N = 3e5 + 5;
const int M = 3e6 + 5;
const int K = 1e7 + 5;
const int MOD = 1e9 + 7;              // 998244353 1e9 + 7
const int INF = 0x3f3f3f3f;             // 1e9 + 7 0x3f3f3f3f
const ll LLINF = 0x3f3f3f3f3f3f3f3f;    // 1e18 + 9 0x3f3f3f3f3f3f3f3f
const double eps = 1e-8;

struct point {
    int x, y;
    point operator + (const point& p) const {
        return (point){x + p.x, y + p.y};
    }
    bool operator < (const point& p) const {
        return x < p.x || (x == p.x && y < p.y);
    }
    void input() {
        int sx, sy, tx, ty;
        read(sx, sy);
        read(tx, ty);
        x = tx - sx; y = ty - sy;
        if (x == 0 && y == 0);
        else if (x == 0) y = y / abs(y);
        else if (y == 0) x = x / abs(x);
        else {
            int g = abs(__gcd(x, y));
            x /= g;
            y /= g;
        }

        debug(x, y);
    }
    point inv() {
        return (point){-x, -y};
    }
};
// true if alphta - eta = 180 degree

point p[N];
void solve(int Case) { // printf("Case #%d: ", Case);
    int n;
    read(n);

    FOR(i, 1, n) p[i].input();
    
    ll ans = 0;

    map<int, int> cx, cy;
    map<point, int> cp;
    FOR(i, 1, n) {
        ans += cp[p[i].inv()];

        ++cp[p[i]];

        debug(cx, cy);
    }

    println(ans);
}


int main() {
#ifdef BACKLIGHT
    freopen("in.txt", "r", stdin);
    // freopen("out.txt", "w", stdout);
    auto begin = std::chrono::steady_clock::now();
#endif

    // ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
    int T = 1;
    read(T);
    for (int _ = 1; _ <= T; _++) solve(_);

#ifdef BACKLIGHT
    auto end = std::chrono::steady_clock::now();
    auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(end - begin);
    cerr << "33[32mTime Elasped: " << duration.count() << " ms33[0m" << endl;
#endif
    return 0;
}
原文地址:https://www.cnblogs.com/zengzk/p/14192112.html