Advertisement
Abrar_Al_Samit

Stuck in a Rut (USACO Silver Dec 2020) (ugly)

Jul 1st, 2021
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.96 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. #include <ext/pb_ds/assoc_container.hpp>
  3. #include <ext/pb_ds/tree_policy.hpp>
  4.  
  5. using namespace std;
  6. using namespace __gnu_pbds;
  7.  
  8.  
  9. #define debug(x) cerr << '[' << (#x) << "] = " << x << '\n';
  10.  
  11. template<class T> using ordered_set = tree<T, null_type , less<T> , rb_tree_tag , tree_order_statistics_node_update> ;
  12.  
  13. const int maxn = 1005;
  14.  
  15. vector<pair<pair<int,int>,char>>cows;
  16. vector<int>ans(maxn, -1), will_go(maxn, 1e9), stopper(maxn, -2);
  17. vector<int>g[maxn], sub(maxn, 0);
  18. int n;
  19. void dfs(int node) {
  20.     if(ans[node]!=-1) return;
  21.     for(auto it : g[node]) {
  22.         dfs(it);
  23.         sub[node] += sub[it];
  24.     }
  25.     ans[node] = sub[node];
  26.     ++sub[node];
  27. }
  28. bool possible_stopper(int i, int j) {
  29.     if(cows[i].second==cows[j].second) return false;
  30.     if(cows[i].second=='E') {
  31.         if(cows[j].first.second > cows[i].first.second) return false;
  32.         if(cows[j].first.first < cows[i].first.first) return false;
  33.         if(cows[j].first.first - cows[i].first.first <= cows[i].first.second - cows[j].first.second)
  34.             return false;
  35.     } else {
  36.         if(cows[j].first.first > cows[i].first.first) return false;
  37.         if(cows[j].first.second < cows[i].first.second) return false;
  38.         if(cows[j].first.second - cows[i].first.second <= cows[i].first.first - cows[j].first.first)
  39.             return false;
  40.     }
  41.     return true;
  42. }
  43. void AddEdge(int u, int v) {
  44.     g[u].push_back(v);
  45. }
  46. void check(int i) {
  47.     for(int j=0; j<n; ++j) if(i!=j) {
  48.         if(possible_stopper(i, j)) {
  49.             if(stopper[j]==-2) check(j);
  50.             if(cows[i].second=='E') {
  51.                 if(cows[i].first.second - cows[j].first.second < will_go[j]) {
  52.                     if(will_go[i] > cows[j].first.first - cows[i].first.first)
  53.                         will_go[i] = cows[j].first.first - cows[i].first.first, stopper[i] = j;
  54.                 }
  55.             } else {
  56.                 if(cows[j].first.first + will_go[j] > cows[i].first.first) {
  57.                     if(will_go[i] > cows[j].first.second - cows[i].first.second)
  58.                         will_go[i] = cows[j].first.second - cows[i].first.second, stopper[i] = j;
  59.                 }
  60.             }
  61.         }
  62.     }
  63.     if(stopper[i]==-2) stopper[i] = -1;
  64.     if(stopper[i]!=-1) AddEdge(stopper[i], i);
  65. }
  66. void PlayGround() {
  67.     cin >> n;
  68.     for(int i=0; i<n; ++i) {
  69.         char d;
  70.         int x, y; cin >> d >> x >> y;
  71.         cows.push_back(make_pair(make_pair(x, y), d));
  72.     }
  73.     for(int i=0; i<n; ++i) {
  74.         if(stopper[i]==-2) check(i);
  75.     }
  76.  
  77.     for(int i=0; i<n; ++i) {
  78.         dfs(i);
  79.         cout << ans[i] << '\n';
  80.     }
  81.  
  82.     #ifndef ONLINE_JUDGE
  83.         cerr << "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " s.\n";
  84.     #endif
  85. }
  86. int main() {
  87.     ios_base::sync_with_stdio(0);
  88.     cin.tie(0);
  89.     cout.tie(0);
  90.     //#ifndef ONLINE_JUDGE
  91.       //  freopen("input.txt", "r", stdin);
  92.     //#endif
  93.     PlayGround();
  94.  
  95.     return 0;
  96. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement