【codeforces 793C】Mice problem

【题目链接】:http://codeforces.com/contest/793/problem/C

【题意】

给你每个点x轴移动速度,y轴移动速度;
问你有没有某个时刻,所有的点都“严格”在所给的矩形内

【题解】

把二维的问题转化成一维的问题;
那样问题就转换成
x1..x2是目标的线段(一维的)
然后有若干个xi
问你从xi变化到到x1..x2这个区间最短和最长时间(分别对应第一次进入这个区间和出这个区间的时间);
yi同理;
然后求时间的交集就可以了;
对于都刚好在边框上的情况;
用精度来填补
精度调越小越好!
vx<0的情况
可以把它都转成负数;
然后对称着做;
注意vx=0的特判;
以及vx的正负和所需要的方向不同的判断;
数据往里面传的时候,先传Int的会快很多

【Number Of WA

6

【完整代码】

#include <bits/stdc++.h>
using namespace std;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define LL long long
#define rep1(i,a,b) for (int i = a;i <= b;i++)
#define rep2(i,a,b) for (int i = a;i >= b;i--)
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define ms(x,y) memset(x,y,sizeof x)

typedef pair<int,int> pii;
typedef pair<LL,LL> pll;

const int dx[9] = {0,1,0,-1,0,-1,-1,1,1};
const int dy[9] = {0,0,-1,0,1,-1,1,-1,1};
const double pi = acos(-1.0);
const int N = 1e5+100;
const double eps = 3e-15;

int n;
int x1,y1,x2,y2,x0,y0,vx,vy;
double zuo = 0,you = 1e9;

void o()
{
    cout << -1 << endl;
    exit(0);
}

void up_data(int x,int vx,int l,int r)
{
    if (vx==0)
    {
        if (l < x && x < r)
            return;
        else
            o();
    }
    if (vx<0)
    {
        x = -x,vx = -vx;
        l=-l,r = -r;
        swap(l,r);
    }
    if (x>=r) o();
    you = min(you,(r-x)/double(vx));
    if (x<=l) zuo = max(zuo,(l-x)/double(vx));
}

int main()
{
    //freopen("F:\rush.txt","r",stdin);
    ios::sync_with_stdio(false),cin.tie(0);//scanf,puts,printf not use
    cin >> n;
    cin >> x1 >> y1 >> x2 >> y2;
    rep1(i,1,n)
    {
        cin >> x0 >> y0 >> vx >> vy;
        up_data(x0,vx,x1,x2);
        up_data(y0,vy,y1,y2);
    }
    if (you>=zuo*(1+eps))
        cout << fixed << setprecision(12) << zuo << endl;
    else
        cout << -1 << endl;
    return 0;
}
原文地址:https://www.cnblogs.com/AWCXV/p/7626387.html