結果

問題 No.1405 ジグザグロボット
ユーザー SSRSSSRS
提出日時 2020-12-11 23:21:30
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 4,576 bytes
コンパイル時間 2,158 ms
コンパイル使用メモリ 189,916 KB
実行使用メモリ 40,068 KB
最終ジャッジ日時 2023-10-16 23:51:29
合計ジャッジ時間 24,933 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
40,068 KB
testcase_01 AC 2 ms
4,348 KB
testcase_02 AC 2 ms
4,348 KB
testcase_03 AC 3 ms
4,348 KB
testcase_04 AC 2 ms
4,348 KB
testcase_05 AC 3 ms
4,348 KB
testcase_06 AC 3 ms
4,348 KB
testcase_07 AC 3 ms
4,348 KB
testcase_08 AC 34 ms
4,420 KB
testcase_09 AC 122 ms
5,444 KB
testcase_10 AC 251 ms
6,448 KB
testcase_11 WA -
testcase_12 WA -
testcase_13 AC 264 ms
6,572 KB
testcase_14 WA -
testcase_15 WA -
testcase_16 AC 242 ms
6,324 KB
testcase_17 AC 249 ms
6,428 KB
testcase_18 AC 263 ms
6,572 KB
testcase_19 AC 273 ms
6,672 KB
testcase_20 AC 263 ms
6,488 KB
testcase_21 AC 298 ms
6,912 KB
testcase_22 AC 256 ms
6,504 KB
testcase_23 AC 281 ms
6,712 KB
testcase_24 AC 277 ms
6,696 KB
testcase_25 AC 195 ms
5,632 KB
testcase_26 AC 235 ms
6,284 KB
testcase_27 AC 320 ms
7,212 KB
testcase_28 AC 293 ms
7,108 KB
testcase_29 AC 267 ms
6,676 KB
testcase_30 AC 350 ms
7,588 KB
testcase_31 AC 2,801 ms
27,636 KB
testcase_32 AC 2,800 ms
27,636 KB
testcase_33 AC 622 ms
9,828 KB
testcase_34 AC 2 ms
4,348 KB
testcase_35 TLE -
testcase_36 -- -
testcase_37 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
const int INF = 1000000;
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();
  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 {
      /*
      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;
          }
        }
      }
      /*
      for (int i = 0; i < Y + 2; i++){
        cout << P[i] << ' ';
      }
      cout << endl;
      */
      vector<string> T2(Y + 1);
      for (int i = 0; i < Y + 1; i++){
        for (int j = P[i]; j < P[i + 1]; j++){
          T2[i] += T[j];
          if (j < P[i + 1] - 1){
            T2[i] += 'S';
          }
        }
      }
      /*
      for (int i = 0; i <= Y; i++){
        cout << T2[i] << ' ';
      }
      cout << endl;
      */
      int D = dp[Y + 1][M] - X;
      vector<pair<int, int>> obs;
      int cx = 0, cy = 0;
      for (int i = 0; i <= Y; i++){
        //cout << T2[i] << endl;
        if (cx == X){
          //cout << "i = " << i << endl;
          int cy = i;
          bool ok = false;
          for (int j = i; j <= Y; j++){
            int sz = T2[j].size();
            for (int k = 0; k < sz; k++){
              if (cy == Y){
                ok = true;
                break;
              }
              if (T2[j][k] == 'L'){
                obs.push_back(make_pair(X - 1, cy));
              }
              if (T2[j][k] == 'R'){
                obs.push_back(make_pair(X + 1, cy));
              }
              if (T2[j][k] == 'S'){
                cy++;
              }
            }
            cy++;
            if (ok){
              break;
            }
          }
          obs.push_back(make_pair(X - 1, Y));
          obs.push_back(make_pair(X + 1, Y));
          obs.push_back(make_pair(X, Y + 1));
          break;
        }
        int sz = T2[i].size();
        int x2 = cx;
        int mx = x2;
        int left = -INF;
        int right = INF;
        for (int j = 0; j < sz; j++){
          if (T2[i][j] == 'L'){
            x2--;
            if (x2 < cx){
              x2 = cx;
              left = cx;
            }
          }
          if (T2[i][j] == 'R'){
            x2++;
            mx = max(mx, x2);
          }
        }
        if (left != -INF){
          obs.push_back(make_pair(left - 1, i));
          //cout << "x = " << left - 1 << ", y = " << i << endl;
        }
        if (x2 > X){
          right = mx - (x2 - X);
          obs.push_back(make_pair(right + 1, i));
          //cout << "x = " << right + 1 << ", y = " << i << endl;
          x2 = X;
        }
        //cout << left << ' ' << right << endl;
        int x3 = cx; 
        for (int j = 0; j < sz; j++){
          if (T2[i][j] == 'L'){
            x3--;
            x3 = max(x3, left);
          }
          if (T2[i][j] == 'R'){
            x3++;
            x3 = min(x3, right);
          }
          if (T2[i][j] == 'S'){
            obs.push_back(make_pair(x3, i + 1));
            //cout << "x = " << x3 << ", y = " << i + 1 << endl;
          }
        }
        cx = x2;
      }
      sort(obs.begin(), obs.end());
      obs.erase(unique(obs.begin(), obs.end()), obs.end());
      int K = obs.size();
      cout << K << endl;
      for (int i = 0; i < K; i++){
        cout << obs[i].first << ' ' << obs[i].second << endl;
      }
    }
  }
}
0