Submission #581154

# Submission time Handle Problem Language Result Execution time Memory
581154 2022-06-22T09:47:09 Z MohammadAghil Rainforest Jumps (APIO21_jumps) C++17
4 / 100
883 ms 5136 KB
      #include <bits/stdc++.h>
      #include "jumps.h"
//   #pragma GCC optimize ("Ofast,unroll-loops")
// #pragma GCC target ("avx2")
    using namespace std;
  typedef long long ll;
   typedef pair<int, int> pp;
    #define er(args ...) cerr << __LINE__ << ": ", err(new istringstream(string(#args)), args), cerr << endl
      #define per(i,r,l) for(int i = (r); i >= (l); i--)
        #define rep(i,l,r) for(int i = (l); i < (r); i++)
           #define all(x) x.begin(), x.end()
              #define sz(x) (int)(x).size()
                  #define pb push_back
                      #define ss second
                           #define ff first
                                   void err(istringstream *iss){}template<typename T,typename ...Args> void err(istringstream *iss,const T &_val, const Args&...args){string _name;*iss>>_name;if(_name.back()==',')_name.pop_back();cerr<<_name<<" = "<<_val<<", ",err(iss,args...);}
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
const ll mod = 1e9 + 7, maxn = 5e5 + 5, lg = 22, inf = ll(1e9) + 5;
ll pw(ll a,ll b,ll md=mod){if(!b)return 1;ll k=pw(a,b>>1ll);return k*k%md*(b&1ll?a:1)%md;}

int h[maxn], lx[maxn], rx[maxn], n;
bool ok;

int query(int a, int b){
     if(h[a] > h[b]) return inf;
     // int cr = a, cnt = -1;
     // while(true){
     //      cnt++;
     //      if(cr == b) return cnt;
     //      bool okl = h[lx[cr]] <= h[b], okr = h[rx[cr]] <= h[b];
     //      if(okl && okr) cr = h[lx[cr]] > h[rx[cr]]? lx[cr]: rx[cr];
     //      else if(okl) cr = lx[cr];
     //      else if(okr) cr = rx[cr];
     //      else return inf;
     // }
     // return inf;
     deque<int> q{a};
     vector<int> dist(n + 2, inf); dist[a] = 0;
     while(sz(q)){
          int r = q.front(); q.pop_front();
          if(h[r] > h[b]) continue;
          if(r == b) return dist[r]; 
          if(lx[r] > 0 && dist[lx[r]] == inf) q.push_back(lx[r]), dist[lx[r]] = dist[r] + 1;
          if(rx[r] < n+1 && dist[rx[r]] == inf) q.push_back(rx[r]), dist[rx[r]] = dist[r] + 1;
     }
     return inf;
}

int minimum_jumps(int a, int b, int c, int d){
     if(ok) return c - b;
     int ans = inf;
     rep(i,a,b+1) rep(j,c,d+1) ans = min(ans, query(i, j));
     return ans == inf? -1: ans;
     // return c - b;
}

void init(int n, vector<int> H){ ::n = n, ok = true;
     rep(i,0,n) h[i+1] = H[i], ok &= H[i] == i+1; h[0] = h[n+1] = inf;
     vector<int> st = {0};
     rep(i,1,n + 1){
          while(h[st.back()] < h[i]) st.pop_back();
          lx[i] = st.back(), st.pb(i);
     } st = {n+1};
     per(i,n,1){
          while(h[st.back()] < h[i]) st.pop_back();
          rx[i] = st.back(), st.pb(i);
     }
}

// int main(){
//      cin.tie(0) -> sync_with_stdio(0);
//      freopen("in.in", "r", stdin);
//      freopen("out.out", "w", stdout);
//      int n; cin >> n; ::n = n;
//      int q; cin >> q;
//      rep(i,1,n+1) cin >> h[i]; h[0] = h[n+1] = inf;
//      vector<int> st = {0};
//      rep(i,1,n + 1){
//           while(h[st.back()] < h[i]) st.pop_back();
//           lx[i] = st.back(), st.pb(i);
//      } st = {n+1};
//      per(i,n,1){
//           while(h[st.back()] < h[i]) st.pop_back();
//           rx[i] = st.back(), st.pb(i);
//      }
//      while(q--){
//           int a, b, c, d; cin >> a >> b >> c >> d; 
//           cout << minimum_jumps(++a, ++b, ++c, ++d) << '\n';
//      }
//      return 0; 
// }

Compilation message

jumps.cpp: In function 'void init(int, std::vector<int>)':
jumps.cpp:10:28: warning: this 'for' clause does not guard... [-Wmisleading-indentation]
   10 |         #define rep(i,l,r) for(int i = (l); i < (r); i++)
      |                            ^~~
jumps.cpp:58:6: note: in expansion of macro 'rep'
   58 |      rep(i,0,n) h[i+1] = H[i], ok &= H[i] == i+1; h[0] = h[n+1] = inf;
      |      ^~~
jumps.cpp:58:51: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'for'
   58 |      rep(i,0,n) h[i+1] = H[i], ok &= H[i] == i+1; h[0] = h[n+1] = inf;
      |                                                   ^
# Verdict Execution time Memory Grader output
1 Correct 0 ms 208 KB Output is correct
2 Correct 0 ms 208 KB Output is correct
3 Correct 96 ms 4368 KB Output is correct
4 Correct 883 ms 5056 KB Output is correct
5 Correct 743 ms 2756 KB Output is correct
6 Correct 699 ms 5040 KB Output is correct
7 Correct 581 ms 4040 KB Output is correct
8 Correct 759 ms 5136 KB Output is correct
# Verdict Execution time Memory Grader output
1 Incorrect 0 ms 208 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 0 ms 208 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 1 ms 208 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 296 KB Output is correct
2 Correct 1 ms 228 KB Output is correct
3 Correct 0 ms 208 KB Output is correct
4 Incorrect 818 ms 2016 KB Output isn't correct
5 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 296 KB Output is correct
2 Correct 1 ms 228 KB Output is correct
3 Correct 0 ms 208 KB Output is correct
4 Incorrect 818 ms 2016 KB Output isn't correct
5 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 208 KB Output is correct
2 Correct 0 ms 208 KB Output is correct
3 Correct 96 ms 4368 KB Output is correct
4 Correct 883 ms 5056 KB Output is correct
5 Correct 743 ms 2756 KB Output is correct
6 Correct 699 ms 5040 KB Output is correct
7 Correct 581 ms 4040 KB Output is correct
8 Correct 759 ms 5136 KB Output is correct
9 Incorrect 0 ms 208 KB Output isn't correct
10 Halted 0 ms 0 KB -