• 牛客练习赛65


    牛客练习赛65 - C 二维动点 (几何,思维)

    链接:https://ac.nowcoder.com/acm/contest/5961/C
    来源:牛客网

    题目描述

    一个二维平面上有n个点(ai,bi)(a_i,b_i)(ai,bi),在一次移动中,你可以选择一个不和当前所在位置重叠的点,然后可以移动到当前所在位置和选择的点构成的直线上的任何一个位置;每次询问一个点(x,y),求出从(0,0)到达(x,y)所需要的最少移动次数,无解输出-1

    输入描述:

    第一行为两个数n,q
    接下来n行,每行两个数ai,bia_i,b_iai,bi,表示一个点的坐标
    接下来q行,每行两个数xi,yix_i,y_ixi,yi,表示询问一个目标点
    

    输出描述:

    共q行,每行一个整数,表示到达目标点所需要的的最少移动次数,无解输出-1
    

    思路:

    主要是思维类的分类讨论题目,

    因为所有点都在第一象限,所以将坐标((x,y))除以他们的gcd(最大公约数)后,可以唯一的表示:该点与((0,0))连接后直线的斜率。

    我们用(set,map)之类的容器将所有可能的斜率存起来。

    并且读入点时将((0,0))剔除,且维护剩余的(mathit n),因为点((0,0))在这个问题里没有价值。

    接下来对于给定的询问点((x,y)),我们分类讨论:

    1、 当点为原点时,输出0

    2、当点与原点连接后直线的斜率存在时,可以一步达到,输出1

    3、如果不满足2,且n个点只有一种斜率,则答案为-1

    4、不只有一种斜率,那么剩下情况是一定可以到达目标点的:

    当仅剩下2个点时,如果这2个点与原点和询问点构成了一个平行四边形,那么答案为3,

    否则答案为2. (可以通过画样例来了解为什么)

    代码:

    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <algorithm>
    #include <cmath>
    #include <queue>
    #include <stack>
    #include <map>
    #include <set>
    #include <vector>
    #include <iomanip>
    #include <sstream>
    #include <bitset>
    #include <unordered_map>
    // #include <bits/stdc++.h>
    #define ALL(x) (x).begin(), (x).end()
    #define sz(a) int(a.size())
    #define rep(i,x,n) for(int i=x;i<n;i++)
    #define repd(i,x,n) for(int i=x;i<=n;i++)
    #define pii pair<int,int>
    #define pll pair<long long ,long long>
    #define gbtb ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
    #define MS0(X) memset((X), 0, sizeof((X)))
    #define MSC0(X) memset((X), '', sizeof((X)))
    #define pb push_back
    #define mp make_pair
    #define fi first
    #define se second
    #define eps 1e-6
    #define chu(x)  if(DEBUG_Switch) cout<<"["<<#x<<" "<<(x)<<"]"<<endl
    #define du3(a,b,c) scanf("%d %d %d",&(a),&(b),&(c))
    #define du2(a,b) scanf("%d %d",&(a),&(b))
    #define du1(a) scanf("%d",&(a));
    using namespace std;
    typedef long long ll;
    ll gcd(ll a, ll b) {return b ? gcd(b, a % b) : a;}
    ll lcm(ll a, ll b) {return a / gcd(a, b) * b;}
    ll powmod(ll a, ll b, ll MOD) { if (a == 0ll) {return 0ll;} a %= MOD; ll ans = 1; while (b) {if (b & 1) {ans = ans * a % MOD;} a = a * a % MOD; b >>= 1;} return ans;}
    ll poww(ll a, ll b) { if (a == 0ll) {return 0ll;} ll ans = 1; while (b) {if (b & 1) {ans = ans * a ;} a = a * a ; b >>= 1;} return ans;}
    void Pv(const vector<int> &V) {int Len = sz(V); for (int i = 0; i < Len; ++i) {printf("%d", V[i] ); if (i != Len - 1) {printf(" ");} else {printf("
    ");}}}
    void Pvl(const vector<ll> &V) {int Len = sz(V); for (int i = 0; i < Len; ++i) {printf("%lld", V[i] ); if (i != Len - 1) {printf(" ");} else {printf("
    ");}}}
    inline long long readll() {long long tmp = 0, fh = 1; char c = getchar(); while (c < '0' || c > '9') {if (c == '-') fh = -1; c = getchar();} while (c >= '0' && c <= '9') tmp = tmp * 10 + c - 48, c = getchar(); return tmp * fh;}
    inline int readint() {int tmp = 0, fh = 1; char c = getchar(); while (c < '0' || c > '9') {if (c == '-') fh = -1; c = getchar();} while (c >= '0' && c <= '9') tmp = tmp * 10 + c - 48, c = getchar(); return tmp * fh;}
    void pvarr_int(int *arr, int n, int strat = 1) {if (strat == 0) {n--;} repd(i, strat, n) {printf("%d%c", arr[i], i == n ? '
    ' : ' ');}}
    void pvarr_LL(ll *arr, int n, int strat = 1) {if (strat == 0) {n--;} repd(i, strat, n) {printf("%lld%c", arr[i], i == n ? '
    ' : ' ');}}
    const int maxn = 1000010;
    const int inf = 0x3f3f3f3f;
    /*** TEMPLATE CODE * * STARTS HERE ***/
    #define DEBUG_Switch 0
    struct vec
    {
        ll x, y;
        vec() {}
        vec(ll xx, ll yy)
        {
            x = xx;
            y = yy;
            ll g = gcd(x, y);
            if (g != 0)
            {
                x /= g;
                y /= g;
            }
        }
        bool operator < ( const vec & bb )const
        {
            if (bb.x != x)
            {
                return x < bb.x;
            } else
            {
                return y < bb.y;
            }
        }
    };
    int n, q;
    ll a[maxn];
    ll b[maxn];
    set<vec> st;
    int main()
    {
    #if DEBUG_Switch
        freopen("C:\code\input.txt", "r", stdin);
    #endif
        //freopen("C:\code\output.txt","r",stdin);
        n = readint();
        q = readint();
        repd(i, 1, n)
        {
            a[i] = readint();
            b[i] = readint();
            if (a[i] == 0 && b[i] == 0) {
                --i; --n; continue;
            }
            st.insert(vec(a[i], b[i]));
        }
        int cnt = sz(st);
        while (q--)
        {
            ll x = readll();
            ll y = readll();
            if (x == 0 && y == 0)
            {
                printf("0
    ");
                continue;
            }
            if (st.count(vec(x, y)) == 1)
            {
                printf("1
    ");
                continue;
            }
            if ( cnt <= 1)
            {
                printf("-1
    ");
            } else if (n == 2)
            {
                if (1ll * a[1] + a[2] == x && 1ll * b[1] + b[2] == y)
                {
                    printf("3
    ");
                } else
                {
                    printf("2
    ");
                }
            } else
            {
                printf("2
    ");
            }
        }
        return 0;
    }
    
    
    
    
    本博客为本人原创,如需转载,请必须声明博客的源地址。 本人博客地址为:www.cnblogs.com/qieqiemin/ 希望所写的文章对您有帮助。
  • 相关阅读:
    12
    11
    10
    9
    8
    7
    6
    5
    4
    3
  • 原文地址:https://www.cnblogs.com/qieqiemin/p/13110940.html
Copyright © 2020-2023  润新知