QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#752460#9564. Hey, Have You Seen My Kangaroo?hos_lyricWA 285ms29760kbC++145.7kb2024-11-16 05:01:022024-11-16 05:01:02

Judging History

你现在查看的是最新测评结果

  • [2024-11-16 05:01:02]
  • 评测
  • 测评结果:WA
  • 用时:285ms
  • 内存:29760kb
  • [2024-11-16 05:01:02]
  • 提交

answer

#include <cassert>
#include <cmath>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <functional>
#include <iostream>
#include <limits>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

using namespace std;

using Int = long long;

template <class T1, class T2> ostream &operator<<(ostream &os, const pair<T1, T2> &a) { return os << "(" << a.first << ", " << a.second << ")"; };
template <class T> ostream &operator<<(ostream &os, const vector<T> &as) { const int sz = as.size(); os << "["; for (int i = 0; i < sz; ++i) { if (i >= 256) { os << ", ..."; break; } if (i > 0) { os << ", "; } os << as[i]; } return os << "]"; }
template <class T> void pv(T a, T b) { for (T i = a; i != b; ++i) cerr << *i << " "; cerr << endl; }
template <class T> bool chmin(T &t, const T &f) { if (t > f) { t = f; return true; } return false; }
template <class T> bool chmax(T &t, const T &f) { if (t < f) { t = f; return true; } return false; }
#define COLOR(s) ("\x1b[" s "m")


struct Functional {
  int n;
  vector<int> par;
  int cyclesLen;
  vector<int> lens;
  vector<vector<int>> cycles;
  // cycle id or -1
  vector<int> on;
  // forest
  vector<vector<int>> graph;
  int zeit;
  vector<int> dis, fin, dep;
  // root is cycle[k][l]
  vector<int> ks, ls;
  
  vector<int> hei;
  
  Functional() {}
  Functional(const vector<int> &par_) : par(par_) {
    n = par.size();
    for (int u = 0; u < n; ++u) {
      assert(0 <= par[u]); assert(par[u] < n);
    }
    cycles.clear();
    vector<int> vis(n, -1);
    for (int s = 0; s < n; ++s) {
      int u = s;
      for (; !~vis[u]; u = par[u]) {
        vis[u] = s;
      }
      if (vis[u] == s) {
        vector<int> cycle;
        for (int v = u; ; ) {
          cycle.push_back(v);
          if ((v = par[v]) == u) break;
        }
        cycles.push_back(cycle);
      }
    }
    cyclesLen = cycles.size();
    lens.resize(cyclesLen);
    on.assign(n, -1);
    for (int k = 0; k < cyclesLen; ++k) {
      lens[k] = cycles[k].size();
      for (const int u : cycles[k]) {
        on[u] = k;
      }
    }
    graph.assign(n, {});
    for (int u = 0; u < n; ++u) if (!~on[u]) {
      graph[par[u]].push_back(u);
    }
    zeit = 0;
    dis.assign(n, -1);
    fin.assign(n, -1);
    dep.assign(n, 0);
    ks.assign(n, -1);
    ls.assign(n, -1);
    hei.assign(n, 0);
    for (int k = 0; k < cyclesLen; ++k) {
      for (int l = 0; l < lens[k]; ++l) {
        dfs(k, l, cycles[k][l]);
      }
    }
  }
  void dfs(int k, int l, int u) {
    dis[u] = zeit++;
    ks[u] = k;
    ls[u] = l;
    for (const int v : graph[u]) {
      dep[v] = dep[u] + 1;
      dfs(k, l, v);
      chmax(hei[u], 1 + hei[v]);
    }
    fin[u] = zeit;
  }
  
  // min d s.t. f^d(u) = v, or -1
  int dist(int u, int v) const {
    if (ks[u] != ks[v]) return -1;
    if (~on[v]) {
      int dl = ls[v] - ls[u];
      if (dl < 0) dl += lens[ks[u]];
      return dep[u] + dl;
    }
    return (dis[v] <= dis[u] && dis[u] < fin[v]) ? (dep[u] - dep[v]) : -1;
  };
};

////////////////////////////////////////////////////////////////////////////////


constexpr int DX[4] = {+1, 0, -1, 0};
constexpr int DY[4] = {0, +1, 0, -1};
const string DRUL = "DRUL";

char buf[200'010];

int M, N, K;
char S[210];
vector<string> A;

int U;
vector<vector<int>> id;
int nxt[200'010][4];
Functional F;

int main() {
  for (; ~scanf("%d%d%d", &M, &N, &K); ) {
    scanf("%s", S);
    A.resize(M);
    for (int x = 0; x < M; ++x) {
      scanf("%s", buf);
      A[x] = buf;
    }
    
    U = 0;
    id.assign(M, vector<int>(N, -1));
    for (int x = 0; x < M; ++x) for (int y = 0; y < N; ++y) if (A[x][y] == '1') {
      id[x][y] = U++;
    }
    for (int x = 0; x < M; ++x) for (int y = 0; y < N; ++y) if (A[x][y] == '1') {
      const int u = id[x][y];
      for (int dir = 0; dir < 4; ++dir) {
        const int xx = x + DX[dir];
        const int yy = y + DY[dir];
        nxt[u][dir] = (0 <= xx && xx < M && 0 <= yy && yy < N && A[xx][yy] == '1') ? id[xx][yy] : u;
      }
    }
#define M do_not_use
#define N do_not_use
#define A do_not_use
    
    vector<int> par(U);
    for (int s = 0; s < U; ++s) {
      int u = s;
      for (int k = 0; k < K; ++k) {
        const int dir = DRUL.find(S[k]);
        u = nxt[u][dir];
      }
      par[s] = u;
    }
    F = Functional(par);
// cerr<<"cycles = "<<F.cycles<<", graph = "<<F.graph<<endl;;
    auto hs = F.hei;
    for (int u = 0; u < U; ++u) if (~F.on[u]) hs[u] = U;
// cerr<<"hs = "<<hs<<endl;
    
    vector<int> ans;
    vector<int> ss(U);
    for (int s = 0; s < U; ++s) ss[s] = s;
    for (int k = 0; k < K; ++k) {
      const int dir = DRUL.find(S[k]);
      vector<int> ts(U, -1);
      for (int u = 0; u < U; ++u) {
        int s = ss[u];
        if (~s) {
          const int v = nxt[u][dir];
          int &t = ts[v];
          if (~t) {
            if (hs[s] < hs[t]) swap(s, t);
// cerr<<"merge "<<s<<" "<<t<<"; hs[t] = "<<hs[t]<<endl;
            for (int h = 0; h <= hs[t]; ++h) {
              ans.push_back(h * K + k + 1);
            }
          } else {
            t = s;
          }
        }
      }
// cerr<<"k = "<<k<<", ts = "<<ts<<endl;
      ss.swap(ts);
    }
    
#undef M
#undef N
    sort(ans.begin(), ans.end());
    ans.resize(U - 1, -1);
    reverse(ans.begin(), ans.end());
    ans.resize(M * N, 0);
    for (const int t : ans) {
      printf("%d\n", t);
    }
  }
  return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 0ms
memory: 3760kb

input:

3 3 6
ULDDRR
010
111
010

output:

-1
4
2
1
0
0
0
0
0

result:

ok 9 numbers

Test #2:

score: 0
Accepted
time: 0ms
memory: 4112kb

input:

3 3 6
ULDDRR
010
111
011

output:

7
4
2
1
1
0
0
0
0

result:

ok 9 numbers

Test #3:

score: 0
Accepted
time: 0ms
memory: 4136kb

input:

1 5 1
R
11111

output:

4
3
2
1
0

result:

ok 5 number(s): "4 3 2 1 0"

Test #4:

score: -100
Wrong Answer
time: 285ms
memory: 29760kb

input:

1 200000 200
RDRLDRULURDLDRULLRULLRRULRULRDLLDLRUDDLRURLURLULDRUUURDLUDUDLLLLLURRDURLUDDRRLRRURUUDDLLDDUUUDUULRLRUDULRRUURUDDDDLULULLLLLLLLLLLUDURLURLRLLRRRURUURLUDULDUULRRLULLRUDRDRUUDDRUDRDLLDLURDDDURLUULDRRDLDD
11111111111111111111111111111111111111111111111111111111111111111111111111111111111111...

output:

-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
...

result:

wrong answer 1st numbers differ - expected: '3999923', found: '-1'