zoukankan      html  css  js  c++  java
  • CodeForces 589D Boulevard (数学,相遇)

    题意:给定 n 个的在 x 轴上的坐标,和开始时间,结束坐标,从起点向终点走,如果和其他人相遇,就互相打招乎,问你每人打招乎的次数。

    析:其实这一个数学题,由于 n 比较小,我们就可以两两暴力,这两个我们先让他们同时出现,也就是让先出现的,先走着,走到和后来的同一时间,

    然后判方向,如果方向不是相对,或者是坐标一样,那么就是不可能相遇,然后如果是相遇,那么就可以相对速度来算,先两者的距离,再算相遇的时间,

    最后判不是走过终点了即可。

    代码如下:

    #pragma comment(linker, "/STACK:1024000000,1024000000")
    #include <cstdio>
    #include <string>
    #include <cstdlib>
    #include <cmath>
    #include <iostream>
    #include <cstring>
    #include <set>
    #include <queue>
    #include <algorithm>
    #include <vector>
    #include <map>
    #include <cctype>
    #include <stack>
    using namespace std ;
    
    typedef long long LL;
    typedef pair<int, int> P;
    const int INF = 0x3f3f3f3f;
    const double inf = 0x3f3f3f3f3f3f;
    const double PI = acos(-1.0);
    const double eps = 1e-8;
    const int maxn = 1e3 + 5;
    const int mod = 1e9 + 7;
    const char *mark = "+-*";
    const int dr[] = {-1, 0, 1, 0};
    const int dc[] = {0, 1, 0, -1};
    int n, m;
    inline bool is_in(int r, int c){
        return r >= 0 && r < n && c >= 0 && c < m;
    }
    struct node{
        LL s, f, t;
    };
    node a[maxn];
    int ans[maxn];
    
    int main(){
        while(scanf("%d", &n) == 1){
            memset(ans, 0, sizeof(ans));
            for(int i = 0; i < n; ++i)
                scanf("%I64d %I64d %I64d", &a[i].t, &a[i].s, &a[i].f);
            for(int i = 0; i < n; ++i){
                int li = a[i].s <= a[i].f ? 1 : -1;
                for(int j = i+1; j < n; ++j){
                    int lj = a[j].s <= a[j].f ? 1 : -1;
                    node u = a[i], v = a[j];
                    if(u.t < v.t)  u.s += (LL)(v.t-u.t) * li;//匹配时间
                    else if(u.t > v.t)  v.s += (LL)(u.t-v.t) * lj;
                    if((u.s - u.f) * (a[i].s - a[i].f) < 0)  continue;
                    if((v.s - v.f) * (a[j].s - a[j].f) < 0)  continue;
    
                    if(u.s == v.s)  ++ans[i], ++ans[j];//判断是不是能相遇
                    else{
                        if(li * lj > 0)  continue;
                        if(u.s < v.s && li == -1)  continue;
                        if(u.s > v.s && li == 1)  continue;
                        LL tt = abs(u.s-v.s);
                        if(tt & 1){
                            LL ti = tt / 2;
                            if(li * (u.s + ti * li - u.f) >= 0) continue;
                            if(lj * (v.s + ti * lj - v.f) >= 0) continue;
                            ++ans[i], ++ans[j];
                        }
                        else{
                            LL ti = tt / 2;
                            if(li * (u.s + ti * li - u.f) > 0) continue;
                            if(lj * (v.s + ti * lj - v.f) > 0) continue;
                            ++ans[i], ++ans[j];
                        }
                    }
                }
            }
            for(int i = 0; i < n; ++i){
                if(i)  putchar(' ');
                printf("%d", ans[i]);
            }
            printf("
    ");
        }
        return 0;
    }
    
  • 相关阅读:
    49. 字母异位词分组
    73. 矩阵置零
    Razor语法问题(foreach里面嵌套if)
    多线程问题
    Get json formatted string from web by sending HttpWebRequest and then deserialize it to get needed data
    How to execute tons of tasks parallelly with TPL method?
    How to sort the dictionary by the value field
    How to customize the console applicaton
    What is the difference for delete/truncate/drop
    How to call C/C++ sytle function from C# solution?
  • 原文地址:https://www.cnblogs.com/dwtfukgv/p/5757971.html
Copyright © 2011-2022 走看看