题目
题目传送门
解题思路
每头奶牛只会被一头奶牛阻拦,所以我们可以在最开始的时候判断出可能相交的奶牛,然后把他们按照时间排序。当有奶牛被其它奶牛提前阻拦的时候,我们就可以打上标记,并传递答案,最后,再输出n行答案即可。
参考代码
#include <bits/stdc++.h>
using namespace std;
typedef long long ll; const int N = 1005;
struct cow {
ll x, y, num;
} a[N], b[N], c[N];
struct node {
ll x, y, numx, numy;
bool operator < (const node h) const {
if (this -> x == h.x) {
return this -> y < h.y;
}
return this -> x < h.x;
}
} p[N * N];
ll ans[N]; bool vis[N];
int main() {
ll n, cntn = 0, cnte = 0, cntp = 0; cin >> n;
for (ll i = 1; i <= n; ++i) {
char x; cow tmp; cin >> x >> tmp.x >> tmp.y;
tmp.num = i;
if (x == 'N') {
a[i] = b[++cntn] = tmp;
} else {
a[i] = c[++cnte] = tmp;
}
}
for (ll i = 1; i <= cntn; ++i) {
for (ll j = 1; j <= cnte; ++j) {
if (b[i].x > c[j].x && b[i].y < c[j].y) {
p[++cntp] = {b[i].x, c[j].y, c[j].num, b[i].num};
}
}
}
sort(p + 1, p + cntp + 1);
for (ll i = 1; i <= cntp; ++i) {
if (vis[p[i].numx] || vis[p[i].numy]) {
continue;
}
ll nx = p[i].x - a[p[i].numx].x, ny = p[i].y - a[p[i].numy].y;
if (nx < ny) {
vis[p[i].numy] = true;
ans[p[i].numx] = ans[p[i].numx] + ans[p[i].numy] + 1;
} else if (nx > ny) {
vis[p[i].numx] = true;
ans[p[i].numy] = ans[p[i].numx] + ans[p[i].numy] + 1;
}
}
for (ll i = 1; i <= n; ++i) {
cout << ans[i] << '\n';
}
return 0;
}