uvaLive 6748 2D-Solar System

題目連接:https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=4760

題目描述:有n 個圓在一條直線上,直線無限長,每個圓有一個速度,可能往左,也可能往右,保證所有的圓在一開始的時候都相離。問能不能所有的圓永遠都不相撞,如果相撞,輸出相撞的時間。每個圓有3個量,半徑 r,速度 a, 起始位置 b。

解題思路:

錯解(我一開始的思路):按照起始位置排序,考慮相鄰的兩個是否相撞,如果相撞,則更新答案(其實是有問題的,第一個圓可以跳過第二個圓與第三個圓相撞!)

正解:思路有點類似斜率優化dp。要維護一個next數組,next[i]表示i 左邊第一個比i 的半徑大的圓的位置。同樣,是要先按照起始位置排序!!

複雜度是O(n)的,證明類似斜率優化dp的證明(每個位置只會入隊一次,出隊一次)。。。

ps:我實在太弱了。。。

詳見代碼。。

//#pragma comment(linker,"/STACK:102400000,102400000")
#include<stdio.h>
#include<iostream>
#include<string.h>
#include<math.h>
#include<algorithm>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<string>
#define ll long long
#define db double
#define PB push_back
using namespace std;

const int N = 50005;
const db INF = 1e50;
const db eps = 1e-7;

ll getll()
{
    char c=getchar();
    while(!(c>='0'&&c<='9')) c=getchar();
    ll res=c-'0';
    while((c=getchar())>='0'&&c<='9') res=res*10+c-'0';
    return res;
}

struct node
{
    ll r,a,b;
    void input()
    {
        scanf("%lld%lld%lld",&r,&a,&b);
    }
    bool operator < (const node &t) const
    {
        return b<t.b;
    }
} p[N];
int next[N];

int main()
{
#ifdef PKWV
    freopen("in.in","r",stdin);
#endif // PKWV
    int n;
    while(scanf("%d",&n)&&n)
    {
        for(int i=0;i<n;i++) p[i].input();
        sort(p,p+n);
        db ans=INF;
        for(int i=0;i<n;i++)
        {
            int j=i-1;
            while(j>=0)
            {
                db t=p[i].b-p[j].b-sqrt((db)((p[i].r+p[j].r)*(p[i].r+p[j].r)-abs(p[i].r-p[j].r)*abs(p[i].r-p[j].r)));
                t/=(db)(p[j].a-p[i].a);
                if(t>eps) ans=min(ans,t);
                if(p[j].r>p[i].r) break;
                j=next[j];
            }
            next[i]=j;
        }
        if(fabs(ans-INF)<1e-7)
        {
            printf("Collision-Free System\n");
        }else printf("%.2f\n",ans);
    }
    return 0;
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章