D - Boulevard
Time Limit:2000MS Memory Limit:524288KB 64bit IO Format:%I64d & %I64u
Submit Status Practice CodeForces 589D
Description
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.
Sample Input
Input
31 1 105 8 29 9 10
Output
2 1 1
Input
33 2 44 3 43 6 4
Output
2 2 2 傻逼模拟题。。。。我的思路是先处理出方向啊,运行的时间啊,然后在线段上消失的时刻。按照出现的时间升序排序。 然后分别考虑同向和异向(错误得以为同正和同反可以一起考虑。。。。这是WA了好多发的根源。。。。)需要注意的地方见注释吧。。。。主要就是分好情况以及判断的时候是否需要在同一时刻。
1 /************************************************************************* 2 > File Name: code/hust/20151025/DDD.cpp 3 > Author: 111qqz 4 > Email: [email protected] 5 > Created Time: 2015年10月26日 星期一 20时16分47秒 6 ************************************************************************/ 7 8 9 10 #include<iostream> 11 #include<iomanip> 12 #include<cstdio> 13 #include<algorithm> 14 #include<cmath> 15 #include<cstring> 16 #include<string> 17 #include<map> 18 #include<set> 19 #include<queue> 20 #include<vector> 21 #include<stack> 22 #include<cctype> 23 24 #define yn hez111qqz 25 #define j1 cute111qqz 26 #define ms(a,x) memset(a,x,sizeof(a)) 27 using namespace std; 28 const int dx4[4]={1,0,0,-1}; 29 const int dy4[4]={0,-1,1,0}; 30 typedef long long LL; 31 typedef double DB; 32 const int inf = 0x3f3f3f3f; 33 const int N=1E3+7; 34 int n; 35 36 struct Q 37 { 38 int t,s,f; 39 int fint; 40 int cost; 41 int id; 42 int dir; 43 44 }q[N]; 45 46 int cnt[N]; 47 48 49 bool cmp(Q a,Q b) 50 { 51 return a.t<b.t; 52 } 53 int main() 54 { 55 #ifndef ONLINE_JUDGE 56 freopen("in.txt","r",stdin); 57 #endif 58 59 scanf("%d",&n); 60 for ( int i = 0 ; i < n ;i++) 61 { 62 scanf("%d %d %d",&q[i].t,&q[i].s,&q[i].f); 63 q[i].id= i; 64 if (q[i].s<q[i].f) 65 { 66 q[i].dir = 1; 67 q[i].cost = q[i].f-q[i].s; 68 } 69 else 70 { 71 q[i].dir = 0; 72 q[i].cost = q[i].s - q[i].f; 73 } 74 q[i].fint = q[i].t + q[i].cost; 75 } 76 77 sort(q,q+n,cmp); 78 ms(cnt,0); 79 for ( int i = 0 ; i < n ; i++) 80 for ( int j = i+1 ; j < n ; j++) 81 { 82 if (q[i].dir==q[j].dir) 83 { 84 // if (q[i].fint<q[j].t) continue; 85 // cout<<"i:"<<q[i].id<<" j:"<<q[j].id<<endl; 86 if (q[j].t-q[i].t+q[i].s==q[j].s&&q[i].fint>=q[j].t&&q[i].dir==1) 87 { 88 cnt[q[i].id]++; 89 cnt[q[j].id]++; 90 } 91 if (q[i].s-(q[j].t-q[i].t)==q[j].s&&q[i].fint>=q[j].t&&q[i].dir==0) //wa了31次之后 92 //发现。。。除了写错之外。 93 //只有两个地方没考虑清楚。。 94 //一个是误以为同方向()的可以一块考虑。 95 //实际上不能。因为这个wa了五六次。 96 //另一个是相向判相遇的时候,条件要判 97 //同一时刻的。。。比如j出现时,i在q[i].s+(q[j].t-q[i].s)(以i正向为例)。。。。 98 //热泪盈眶。。。。 99 //WA 了这么多次还有一个原因是。。。忘记前后的答案是相互影响的。。。我把前面修改后反而对的点更少了。。写成错的反而对得多 100 //差点怀疑人生了都。。。其实是因为后面还有写错。。。前面多算。。后面少算。。答案碰巧对了而已2333 101 { 102 cnt[q[i].id]++; 103 cnt[q[j].id]++; 104 } 105 } 106 else 107 { 108 109 if (q[i].dir==1) 110 { 111 int limt = min(q[i].fint,q[j].fint); 112 int nowi = q[i].s+(limt-q[i].t); 113 int nowj = q[j].s-(limt-q[j].t); 114 // cout<<"limt:"<<limt<<endl; 115 // cout<<"i:"<<q[i].id<<" j:"<<q[j].id<<"nowi:"<<nowi<<" nowj:"<<nowj<<endl; 116 if (nowi>=nowj&&q[i].s+(q[j].t-q[i].t)<=q[j].s) 117 { 118 cnt[q[i].id]++; 119 cnt[q[j].id]++; 120 } 121 } 122 else 123 { 124 int limt = min(q[i].fint,q[j].fint); 125 int nowi = q[i].s-(limt-q[i].t); 126 int nowj = q[j].s+(limt-q[j].t); 127 // cout<<"lim:"<<limt<<endl; 128 // cout<<"ii:"<<q[i].id<<" j:"<<q[j].id <<"nowi:"<<nowi<<" nowj"<<nowj<<endl; 129 if (nowj>=nowi&&q[i].s-(q[j].t-q[i].t)>=q[j].s) 130 { 131 cnt[q[i].id]++; 132 cnt[q[j].id]++; 133 } 134 } 135 } 136 } 137 for ( int i = 0 ; i < n ; i++) 138 { 139 if (i!=n-1) 140 { 141 printf("%d ",cnt[i]); 142 } 143 else 144 { 145 printf("%d\n",cnt[i]); 146 } 147 } 148 149 150 #ifndef ONLINE_JUDGE 151 fclose(stdin); 152 #endif 153 return 0; 154 }