码迷,mamicode.com
首页 > 其他好文 > 详细

CodeForce 677D Boulevard

时间:2016-08-21 00:38:54      阅读:344      评论:0      收藏:0      [点我收藏+]

标签:

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;
}

CodeForce 677D Boulevard

标签:

原文地址:http://www.cnblogs.com/lyf-acm/p/5791503.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!