Alex_tz307

USACO 2018 February Contest, Gold Problem 1. Snow Boots

Dec 4th, 2020
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.39 KB | None | 0 0
  1. // http://www.usaco.org/index.php?page=viewproblem2&cpid=813
  2. #include <bits/stdc++.h>
  3.  
  4. using namespace std;
  5.  
  6. ifstream fin("snowboots.in");
  7. ofstream fout("snowboots.out");
  8.  
  9. vector<int> f, snow, dist, did, bid;
  10.  
  11. bool cmp1(int a, int b) {
  12.     return f[a] < f[b];
  13. }
  14.  
  15. bool cmp2(int a, int b) {
  16.     return snow[a] < snow[b];
  17. }
  18.  
  19. void max_self(int &a, int b) {
  20.     a = max(a, b);
  21. }
  22.  
  23. int main() {
  24.     int N, B;
  25.     fin >> N >> B;
  26.     f.resize(N), did.resize(N);
  27.     for(int i = 0; i < N; ++i) {
  28.         fin >> f[i];
  29.         did[i] = i;
  30.     }
  31.     snow.resize(B), dist.resize(B), bid.resize(B);
  32.     for(int i = 0; i < B; ++i) {
  33.         fin >> snow[i] >> dist[i];
  34.         bid[i] = i;
  35.     }
  36.     sort(did.begin(), did.end(), cmp1);
  37.     sort(bid.begin(), bid.end(), cmp2);
  38.     vector<int> next(N), prev(N);
  39.     for(int i = 0; i < N; ++i) {
  40.         next[i] = i + 1;
  41.         prev[i] = i - 1;
  42.     }
  43.     int j = N - 1, maxStep = 1;
  44.     vector<bool> ans(B);
  45.     for(int i = B - 1; i >= 0; --i) {
  46.         int boot = bid[i];
  47.         while(j >= 0 && f[did[j]] > snow[boot]) {
  48.             int curr = did[j];
  49.             next[prev[curr]] = next[curr];
  50.             prev[next[curr]] = prev[curr];
  51.             max_self(maxStep, next[curr] - prev[curr]);
  52.             --j;
  53.         }
  54.         ans[boot] = (maxStep <= dist[boot]);
  55.     }
  56.     for(int i = 0; i < B; ++i)
  57.         fout << ans[i] << '\n';
  58. }
  59.  
Advertisement
Add Comment
Please, Sign In to add comment