結果

問題 No.1405 ジグザグロボット
ユーザー SSRSSSRS
提出日時 2021-02-18 01:03:53
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
TLE  
実行時間 -
コード長 7,822 bytes
コンパイル時間 2,707 ms
コンパイル使用メモリ 193,608 KB
実行使用メモリ 40,832 KB
最終ジャッジ日時 2024-09-16 23:36:32
合計ジャッジ時間 26,478 ms
ジャッジサーバーID
(参考情報)
judge1 / judge6
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
10,624 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 6 ms
5,376 KB
testcase_04 AC 7 ms
5,376 KB
testcase_05 AC 6 ms
5,376 KB
testcase_06 AC 6 ms
5,376 KB
testcase_07 AC 7 ms
5,376 KB
testcase_08 AC 73 ms
5,376 KB
testcase_09 AC 231 ms
5,504 KB
testcase_10 AC 460 ms
6,400 KB
testcase_11 AC 342 ms
6,272 KB
testcase_12 AC 363 ms
6,400 KB
testcase_13 AC 387 ms
6,400 KB
testcase_14 AC 363 ms
6,400 KB
testcase_15 AC 367 ms
6,400 KB
testcase_16 AC 268 ms
6,272 KB
testcase_17 AC 275 ms
6,144 KB
testcase_18 AC 295 ms
6,400 KB
testcase_19 AC 305 ms
6,528 KB
testcase_20 AC 285 ms
6,272 KB
testcase_21 AC 332 ms
6,784 KB
testcase_22 AC 288 ms
6,528 KB
testcase_23 AC 313 ms
6,656 KB
testcase_24 AC 311 ms
6,528 KB
testcase_25 AC 211 ms
5,504 KB
testcase_26 AC 261 ms
6,272 KB
testcase_27 AC 355 ms
7,040 KB
testcase_28 AC 325 ms
7,040 KB
testcase_29 AC 296 ms
6,528 KB
testcase_30 AC 389 ms
7,552 KB
testcase_31 TLE -
testcase_32 TLE -
testcase_33 AC 750 ms
9,856 KB
testcase_34 AC 4 ms
5,376 KB
testcase_35 TLE -
testcase_36 -- -
testcase_37 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
const int INF = 1000000;
vector<int> dy = {1, 0, 0};
vector<int> dx = {0, 1, -1};
int main(){
  int N, X, Y;
  cin >> N >> X >> Y;
  string S;
  cin >> S;
  vector<string> T(1);
  for (int i = 0; i < N; i++){
    if (S[i] == 'S'){
      T.push_back("");
    } else {
      T.back() += S[i];
    }
  }
  int M = T.size();
  /*
  cout << "M = " << M << endl;
  cout << "T:" << endl;
  for (int i = 0; i < M; i++){
    cout << T[i] << ' ';
  }
  cout << endl;
  */
  if (Y >= M){
    cout << -1 << endl;
  } else {
    vector<vector<int>> a(M + 1, vector<int>(M + 1, 0));
    for (int i = 0; i < M; i++){
      for (int j = i + 1; j <= M; j++){
        for (int k = i; k < j; k++){
          int sz = T[k].size();
          for (int l = 0; l < sz; l++){
            if (T[k][l] == 'L'){
              a[i][j]--;
            } else {
              a[i][j]++;
            }
            a[i][j] = max(a[i][j], 0);
          }
        }
      }
    }
    vector<vector<int>> dp(Y + 2, vector<int>(M + 1, -INF));
    dp[0][0] = 0;
    for (int i = 0; i <= Y; i++){
      for (int j = 0; j < M; j++){
        for (int k = j + 1; k <= M; k++){
          dp[i + 1][k] = max(dp[i + 1][k], dp[i][j] + a[j][k]);
        }
      }
    }
    if (dp[Y + 1][M] < X){
      cout << -1 << endl;
    } else {
      /*
      cout << "dp:" << endl;
      for (int i = 0; i < Y + 2; i++){
        for (int j = 0; j <= M; j++){
          cout << dp[i][j] << ' ';
        }
        cout << endl;
      }
      */
      vector<int> P(Y + 2);
      P[Y + 1] = M;
      for (int i = Y; i >= 0; i--){
        for (int j = 0; j < P[i + 1]; j++){
          if (dp[i + 1][P[i + 1]] == dp[i][j] + a[j][P[i + 1]]){
            P[i] = j;
            break;
          }
        }
      }
      /*
      cout << "P: ";
      for (int i = 0; i < Y + 2; i++){
        cout << P[i] << ' ';
      }
      cout << endl;
      */
      int s = dp[Y + 1][M];
      vector<int> L(Y + 1);
      for (int i = 0; i <= Y; i++){
        L[i] = dp[i][P[i]];
      }
      /*
      cout << "L: ";
      for (int i = 0; i <= Y; i++){
        cout << L[i] << ' ';
      }
      cout << endl;
      */
      vector<string> T2(Y + 1);
      for (int i = 0; i <= Y; i++){
        for (int j = P[i]; j < P[i + 1]; j++){
          T2[i] += T[j];
          T2[i] += 'S';
        }
      }
      T2[Y].pop_back();
      /*
      cout << "T2: ";
      for (int i = 0; i <= Y; i++){
        cout << T2[i] << endl;
      }
      */
      vector<int> R = L;
      for (int i = 0; i <= Y; i++){
        int x = L[i];
        int l = T2[i].size();
        for (int j = 0; j < l; j++){
          if (T2[i][j] == 'L'){
            x--;
          }
          if (T2[i][j] == 'R'){
            x++;
          }
          x = max(x, L[i]);
          R[i] = max(R[i], x);
        }
      }
      /*
      cout << "R: ";
      for (int i = 0; i <= Y; i++){
        cout << R[i] << ' ';
      }
      cout << endl;
      */
      set<pair<int, int>> st;
      for (int i = 0; i <= Y; i++){
        for (int j = L[i]; j <= R[i]; j++){
          st.insert(make_pair(i, j));
        }
      }
      vector<vector<int>> ans(Y + 2);
      for (auto P : st){
        int y = P.first;
        int x = P.second;
        for (int i = 0; i < 3; i++){
          int y2 = y + dy[i];
          int x2 = x + dx[i];
          if (st.count(make_pair(y2, x2)) == 0){
            ans[y2].push_back(x2);
          }
        }
      }
      for (int i = 0; i < Y + 2; i++){
        sort(ans[i].begin(), ans[i].end());
        ans[i].erase(unique(ans[i].begin(), ans[i].end()), ans[i].end());
      }
      /*
      cout << "ans: ";
      for (int i = 0; i < Y + 2; i++){
        int cnt = ans[i].size();
        for (int j = 0; j < cnt; j++){
          cout << "(" << ans[i][j] << "," << i << ") ";
        }
      }
      cout << endl;
      cout << endl;
      */
      while (true){
        set<pair<int, int>> st2;
        for (int i = 0; i < Y + 2; i++){
          int cnt = ans[i].size();
          for (int j = 0; j < cnt; j++){
            st2.insert(make_pair(i, ans[i][j]));
          }
        }
        int cx = 0, cy = 0;
        vector<int> up(Y + 1);
        vector<int> id(Y);
        for (int i = 0; i < N; i++){
          int x2 = cx, y2 = cy;
          if (S[i] == 'L'){
            x2--;
          }
          if (S[i] == 'R'){
            x2++;
          }
          if (S[i] == 'S'){
            y2++;
          }
          if (st2.count(make_pair(y2, x2)) == 1){
            x2 = cx;
            y2 = cy;
          }
          if (y2 != cy){
            id[cy] = i;
            up[cy] = x2;
          }
          cx = x2;
          cy = y2;
        }
        up[Y] = cx;
        if (cx == X){
          break;
        }
        /*
        cout << "up: ";
        for (int j = 0; j <= Y; j++){
          cout << up[j] << ' ';
        }
        cout << endl;
        cout << "id: ";
        for (int j = 0; j <= Y; j++){
          cout << id[j] << ' ';
        }
        cout << endl;
        */
        vector<int> d(Y + 1);
        d[0] = up[0];
        for (int j = 1; j <= Y; j++){
          d[j] = up[j] - up[j - 1];
        }
        /*
        cout << "d: ";
        for (int j = 0; j <= Y; j++){
          cout << d[j] << ' ';
        }
        cout << endl;
        */
        int p = 0;
        while (d[p] == 0){
          p++;
        }
        //cout << "p = " << p << endl;
        ans[p].back()--;
        /*
        cout << "ans: ";
        for (int i = 0; i < Y + 2; i++){
          int cnt = ans[i].size();
          for (int j = 0; j < cnt; j++){
            cout << "(" << ans[i][j] << "," << i << ") ";
          }
        }
        cout << endl;
        */
        set<pair<int, int>> st3;
        for (int i = 0; i < Y + 2; i++){
          int cnt = ans[i].size();
          for (int j = 0; j < cnt; j++){
            st3.insert(make_pair(i, ans[i][j]));
          }
        }
        vector<int> px(N + 1), py(N + 1);
        px[0] = 0;
        py[0] = 0;
        for (int i = 0; i < N; i++){
          px[i + 1] = px[i];
          py[i + 1] = py[i];
          if (S[i] == 'L'){
            px[i + 1]--;
          }
          if (S[i] == 'R'){
            px[i + 1]++;
          }
          if (S[i] == 'S'){
            py[i + 1]++;
          }
          if (st3.count(make_pair(py[i + 1], px[i + 1])) == 1 || py[i + 1] > p){
            px[i + 1] = px[i];
            py[i + 1] = py[i];
          }
        }
        /*
        cout << "px: ";
        for (int i = 0; i <= N; i++){
          cout << px[i] << ' ';
        }
        cout << endl;
        cout << "py: ";
        for (int i = 0; i <= N; i++){
          cout << py[i] << ' ';
        }
        cout << endl;
        cout << "px[id] = " << px[id[p]] << ", up[p] = " << up[p] << endl;
        */
        if (px[id[p]] != up[p]){
          for (int i = p + 1; i < Y + 2; i++){
            int cnt = ans[i].size();
            for (int j = 0; j < cnt; j++){
              ans[i][j]--;
            }
          }
          /*
          cout << "ans:";
          for (int i = 0; i < Y + 2; i++){
            int cnt = ans[i].size();
            for (int j = 0; j < cnt; j++){
              cout << "(" << ans[i][j] << "," << i << ") ";
            }
          }
          cout << endl;
          */
        }
        //cout << endl;
      }
      for (int i = 0; i < Y + 2; i++){
        ans[i].erase(unique(ans[i].begin(), ans[i].end()), ans[i].end());
      }
      int M = 0;
      for (int i = 0; i < Y + 2; i++){
        M += ans[i].size();
      }
      cout << M << endl;
      for (int i = 0; i < Y + 2; i++){
        int cnt = ans[i].size();
        for (int j = 0; j < cnt; j++){
          cout << ans[i][j] << ' ' << i << endl;
        }
      }
    }
  }
}
0