zoukankan      html  css  js  c++  java
  • CodeForce 677D Boulevard

    Boulevard

    Welcoming autumn evening is the best for walking along the boulevard and n people decided to do so.

    The boulevard can be represented as the axis Ox. For every person there are three parameters characterizing the behavior: ti, si, fi — the moment of time when the i-th person starts walking, the start point and the end point of the walk respectively. Each person moves in a straight line along the boulevard from si to fi with a constant speed of either 1 or  - 1 depending on the direction.

    When the i-th person appears on the boulevard at the point si she immediately starts walking towards the point fi.

    If two or more persons meet at the boulevard (they are at the same point at the same time, no matter which directions they are going) they all greet each other. Like in the normal life, every pair of people greet each other at most once.

    You task is to calculate for every person how many people she greets while walking along the boulevard.

    Please, pay attention to the fact that i-th person may meet and greet any other person at points si and fi. After a person achieves the destination point fi she moves out of the boulevard and cannot greet anyone else. The same rule applies to the start of the walk: a person cannot greet anyone until she appears on the boulevard.

    Input

    In the first line there is an integer n (2 ≤ n ≤ 1000) — the number of people who decided to go for a walk.

    The following n lines contain parameters for n people. In the i-th line there are three positive integers ti, si, fi (1 ≤ ti, si, fi ≤ 106,  si ≠ fi), where ti, si, fi — the moment of time when the i-th person starts walking, the start point and the end point of the walk respectively.

    Output
     

    The single line of the output should contain a sequence of n integers r1, r2, ..., rn separated by a space, where ri denotes the number which the i-th person greets other people while walking along the boulevard.

    Examples
    Input
     
    3
    1 1 10
    5 8 2
    9 9 10
    Output
     
    2 1 1 
    Input
     
    3
    3 2 4
    4 3 4
    3 6 4
    Output
     
    2 2 2 

    题意:
      给你n个人,这n个人在数轴上,给出t(开始移动的时间),s(开始的位置),f(目标位置),
      求出他们每个人能打招呼的人的个数,当两个人同一时间在同一位置,那么就打招呼。
    思路:
      暴力枚举,重点是判断两个是否相遇,这个有点坑, 用线性函数表示,判断有没有焦点就可以了。

    AC代码:
     1 # include <iostream>
     2 # include <vector>
     3 # include <cmath>
     4 using namespace std;
     5 
     6 struct P
     7 {
     8     int t;
     9     int s;
    10     int f;
    11     int k;
    12 };
    13 int ins(int x1,int y1,int x2,int y2)
    14 {
    15    if(x2 <= x1 && y1 <= y2) return 1;
    16    if(x1 <= x2 && x2 <= y1) return 1;
    17    if(x1 <= y2 && y2 <= y1) return 1;
    18    return 0;
    19 }
    20 bool ok(P a, P b)
    21 {
    22     //a   (f > s) y = x + a.s - a.t  else y = -x + a.t + a.s (a.t --- t + abs(a.f - a.s))
    23     //b   (f > s) y = x + b.s - b.t  else y = -x + b.t + b.s (b.t --- t + abs(b.f - b.s))
    24     if(a.f >= a.s && b.f >= b.s)
    25     {
    26         if((a.s - a.t) == (b.s - b.t) && ins(a.t, a.t + a.f - a.s, b.t, b.t + b.f - b.s))
    27             return true;
    28         return false;
    29     }
    30     else if(a.f < a.s && b.f < b.s)
    31     {
    32         if((a.s + a.t) == (b.s + b.t) && ins(a.t, a.t + a.s - a.f, b.t, b.t + b.s - b.f))
    33             return true;
    34         return false;
    35     }
    36     else if(a.f >= a.s && b.f < b.s)
    37     {
    38         double x = (b.t - a.s + a.t + b.s) / 2.0;
    39         if(x >= a.t && x <= (a.t + abs(a.f - a.s)) && x >= b.t && x <= (b.t + abs(b.f - b.s)))
    40             return true;
    41         return false;
    42     }
    43     else if(a.f < a.s && b.f >= b.s)
    44     {
    45         double x = (b.t + a.s + a.t - b.s) / 2.0;
    46         if(x >= a.t && x <= (a.t + abs(a.f - a.s)) && x >= b.t && x <= (b.t + abs(b.f - b.s)))
    47                 return true;
    48         return false;
    49     }
    50 }
    51 
    52 int main()
    53 {
    54     /* P a, b;
    55     a.t = 3;a.s = 3;a.f = 1;a.k = 0;
    56     b.t = 3;b.s = 3;b.f = 5;b.k = 0;
    57     
    58     cout << ok(a, b) << endl; */
    59     
    60     int n;
    61     cin >> n;
    62     vector <P> v;
    63     for(int i = 0; i < n; i++)
    64     {    
    65         P p;
    66         cin >> p.t >> p.s >> p.f;
    67         p.k = 0;
    68         v.push_back(p);
    69     }
    70     for(int i = 0; i < v.size(); i++)
    71     {
    72         for(int j = i + 1; j < v.size(); j++)
    73         {
    74             if(ok(v[i], v[j]))
    75             {
    76                 v[i].k++;
    77                 v[j].k++;
    78             }
    79         }
    80     }
    81     for(int i = 0; i < v.size(); i++)
    82     {
    83         if(!i)
    84             cout << v[i].k;
    85         else
    86             cout << " " << v[i].k;
    87     }
    88     cout << endl;
    89     
    90     return 0;
    91 }
    View Code


    # include <iostream>
    # include <vector>
    # include <cmath>
    using namespace std;

    struct P
    {
        int t;
        int s;
        int f;
        int k;
    };
    int ins(int x1,int y1,int x2,int y2)
    {
       if(x2 <= x1 && y1 <= y2) return 1;
       if(x1 <= x2 && x2 <= y1) return 1;
       if(x1 <= y2 && y2 <= y1) return 1;
       return 0;
    }
    bool ok(P a, P b)
    {
        //a   (f > s) y = x + a.s - a.t  else y = -x + a.t + a.s (a.t --- t + abs(a.f - a.s))
        //b   (f > s) y = x + b.s - b.t  else y = -x + b.t + b.s (b.t --- t + abs(b.f - b.s))
        if(a.f >= a.s && b.f >= b.s)
        {
            if((a.s - a.t) == (b.s - b.t) && ins(a.t, a.t + a.f - a.s, b.t, b.t + b.f - b.s))
                return true;
            return false;
        }
        else if(a.f < a.s && b.f < b.s)
        {
            if((a.s + a.t) == (b.s + b.t) && ins(a.t, a.t + a.s - a.f, b.t, b.t + b.s - b.f))
                return true;
            return false;
        }
        else if(a.f >= a.s && b.f < b.s)
        {
            double x = (b.t - a.s + a.t + b.s) / 2.0;
            if(x >= a.t && x <= (a.t + abs(a.f - a.s)) && x >= b.t && x <= (b.t + abs(b.f - b.s)))
                return true;
            return false;
        }
        else if(a.f < a.s && b.f >= b.s)
        {
            double x = (b.t + a.s + a.t - b.s) / 2.0;
            if(x >= a.t && x <= (a.t + abs(a.f - a.s)) && x >= b.t && x <= (b.t + abs(b.f - b.s)))
                    return true;
            return false;
        }
    }

    int main()
    {
        /* P a, b;
        a.t = 3;a.s = 3;a.f = 1;a.k = 0;
        b.t = 3;b.s = 3;b.f = 5;b.k = 0;
        
        cout << ok(a, b) << endl; */
        
        int n;
        cin >> n;
        vector <P> v;
        for(int i = 0; i < n; i++)
        {    
            P p;
            cin >> p.t >> p.s >> p.f;
            p.k = 0;
            v.push_back(p);
        }
        for(int i = 0; i < v.size(); i++)
        {
            for(int j = i + 1; j < v.size(); j++)
            {
                if(ok(v[i], v[j]))
                {
                    v[i].k++;
                    v[j].k++;
                }
            }
        }
        for(int i = 0; i < v.size(); i++)
        {
            if(!i)
                cout << v[i].k;
            else
                cout << " " << v[i].k;
        }
        cout << endl;
        
        return 0;
    }

    生命不息,奋斗不止,这才叫青春,青春就是拥有热情相信未来。
  • 相关阅读:
    Atitit api标准化法 it法学之 目录 1. 永远的痛点:接口与协议的标准化 1 2. 标准化优点 1 3. 标准化组织 2 3.1. 应当处理标准化委员会 2 3.2. 标准化提案与表决
    Atitit 远程存储与协议 mtp ptp rndis midi nfs smb webdav ftp hdfs v3 Atitit mtp ptp rndis midi协议的不同区别
    Atitit redis使用场合总结 使用场景 目录 1.1. 3. Session 存储 1 1、 配置数据查询 1 2. 排行榜应用,取TOP N操作 1 1.2.     1、查找最
    Atitit Java制作VCARD vcf 以上就是关于vCard的基本介绍,维基百科(英文)https://en.wikipedia.org/wiki/VCard写的比较全,可惜我看不懂。
    Atitit 存储方法大总结 目录 1. 存储方式分类 2 1.1. 按照数据分类为 结构化 半结构化 非结构化 2 1.2. 按照内外部可分类 内部存储和外部存储持久化 2 1.3. 按照本地远
    Atiitt 自我学习法流程 1.预先阶段 1.1.目标搜索 资料搜索 1.2. 1.3.通过关联关键词 抽象 等领域 拓展拓宽体系树 1.4. 2.分析整理阶段 2.1.找出重点 压缩要学会
    Atitit 编程 序列化技术点 概念原理v2 1. 序列化: 1 2. 序列化的目的 1 2.1. 为了传输 或者存储 1 3. 应用场合 1 3.1. Form提交url 1 3.2. For
    Atitit session的概念总结
    atitit 面试问题表 侧重于项目和业务描述方面.v2 良好的标准:: 1.回答问题比较流畅,较少出现停顿现象,较少犹豫 2.回答有条理清晰 不杂乱 3.回答较为丰富内容 4.精神状态紧张
    Atitit hibernste5 注解方式开发总结 目录 1. 映入hb5的jar 建立项目 1 1.1. 建表tab1 ,这里使用了sqlite数据库 1 1.2. 建立映射实体类tab1
  • 原文地址:https://www.cnblogs.com/lyf-acm/p/5791503.html
Copyright © 2011-2022 走看看