天天看點

Codeforces 1477C/1478F - Nezzar and Nice Beatmap (幾何/暴力)

Codeforces Round #698 (Div.1) - C

Codeforces Round #698 (Div.2) - F

題意

給定\(n\)個點,要求構造一個點的排列,連成一條路線後使得每個\(\ang ABC\lt 90°\)(每個拐角需要拐\(\gt 90°\))

如下圖左為可行路線,右為不可行路線

Codeforces 1477C/1478F - Nezzar and Nice Beatmap (幾何/暴力)

保證不存在重合的點,不存在方案輸出\(-1\)

限制

\(3\le n\le 5000\)

思路

假設我們現在找到了一段合法路線,其中\(A,B\)為目前這條合法路線的最後兩點

結論:

我們隻需要找出還不存在于路線中的點\(C\),使得\(|BC|\)最大,這樣點\(C\)就能成為下一個點

接下來就繼續找點\(D\)滿足\(|CD|\)最大即可

最後輸出點序即可,是以不存在\(-1\)的情況

至于第一個點,實際上取任意一點均可(保險起見我取的是排序後第一個點)

時間複雜度\(O(n^2)\)

證明:

如果我們接下來找的點\(C\)能夠滿足\(|BC|\)最大(或并列大)

以\(B\)為圓心,\(|BC|\)為半徑作一個圓,剩下的所有點肯定都落在這個圓内(或圓上)

由于不存在重合的點,明顯對于任意剩下的點\(D\),\(\ang BCD\lt 90°\)恒成立

那麼對于\(\ang ABC\),假設\(\ang ABC\ge 90°\),明顯可以發現\(|AC|\gt|AB|\)成立,那麼\(A\)的下一個點必不可能是\(B\),與我們的假設“其中\(A,B\)為目前這條合法路線的最後兩點”不符,是以可得\(\ang ABC\lt 90°\)成立

程式

(140ms/2000ms)

// StelaYuri
//#include<ext/pb_ds/assoc_container.hpp>
//#include<ext/pb_ds/hash_policy.hpp>
#include<bits/stdc++.h>
#define closeSync ios::sync_with_stdio(0);cin.tie(0);cout.tie(0)
#define multiCase int T;cin>>T;for(int t=1;t<=T;t++)
#define rep(i,a,b) for(int i=(a);i<=(b);i++)
#define repp(i,a,b) for(int i=(a);i<(b);i++)
#define per(i,a,b) for(int i=(a);i>=(b);i--)
#define perr(i,a,b) for(int i=(a);i>(b);i--)
#define pb push_back
#define eb emplace_back
#define mst(a,b) memset(a,b,sizeof(a))
using namespace std;
//using namespace __gnu_pbds;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> P;
const int INF=0x3f3f3f3f;
const ll LINF=0x3f3f3f3f3f3f3f3f;
const double eps=1e-12;
const double PI=acos(-1.0);
const double angcst=PI/180.0;
const ll mod=998244353;
ll gcd(ll a,ll b){return b==0?a:gcd(b,a%b);}
ll qmul(ll a,ll b){ll r=0;while(b){if(b&1)r=(r+a)%mod;b>>=1;a=(a+a)%mod;}return r;}
ll qpow(ll a,ll n){ll r=1;while(n){if(n&1)r=(r*a)%mod;n>>=1;a=(a*a)%mod;}return r;}
ll qpow(ll a,ll n,ll p){ll r=1;while(n){if(n&1)r=(r*a)%p;n>>=1;a=(a*a)%p;}return r;}

struct point
{
    ll x,y;
    int id;
    bool vis;
    bool operator <(const point& a) const
    {
        if(x^a.x)
            return x<a.x;
        return y<a.y;
    }
}ar[5050];

ll distance(int a,int b)
{
    return (ar[a].x-ar[b].x)*(ar[a].x-ar[b].x)+(ar[a].y-ar[b].y)*(ar[a].y-ar[b].y);
}

void solve()
{
    int n;
    cin>>n;
    rep(i,1,n)
        cin>>ar[i].x>>ar[i].y,ar[i].id=i,ar[i].vis=false;
    sort(ar+1,ar+1+n);
    
    ar[1].vis=true;
    cout<<ar[1].id;
    int p=1;
    
    rep(i,2,n)
    {
        ll d=0;
        int nxt=0;
        
        rep(j,1,n)
            if(!ar[j].vis) //沒用過的點
            {
                if(!nxt)
                {
                    nxt=j;
                    d=distance(p,j);
                }
                else
                {
                    ll dd=distance(p,j);
                    if(dd>d) //取距離最大的點
                    {
                        nxt=j;
                        d=dd;
                    }
                }
            }
        
        ar[nxt].vis=true;
        p=nxt;
        cout<<' '<<ar[nxt].id;
    }
}
int main()
{
    closeSync;
    //multiCase
    {
        solve();
    }
    return 0;
}