結果

問題 No.775 tatyamと素数大富豪(hard)
ユーザー ei1333333ei1333333
提出日時 2018-12-22 01:01:53
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 1,487 bytes
コンパイル時間 3,070 ms
コンパイル使用メモリ 223,524 KB
実行使用メモリ 96,672 KB
最終ジャッジ日時 2024-04-22 02:45:25
合計ジャッジ時間 6,829 ms
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
6,816 KB
testcase_01 AC 2 ms
6,940 KB
testcase_02 AC 2 ms
6,944 KB
testcase_03 AC 2 ms
6,940 KB
testcase_04 AC 2 ms
6,944 KB
testcase_05 AC 3 ms
6,940 KB
testcase_06 AC 9 ms
6,944 KB
testcase_07 AC 103 ms
9,368 KB
testcase_08 AC 153 ms
9,368 KB
testcase_09 AC 1,417 ms
91,592 KB
testcase_10 AC 1,452 ms
96,672 KB
testcase_11 AC 2 ms
6,944 KB
testcase_12 AC 2 ms
6,940 KB
testcase_13 AC 9 ms
6,940 KB
testcase_14 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;

int N, K;

int main() {
  cin >> N >> K;
  int Z = K;
  vector< string > P(N);
  for(int i = 0; i < N; i++) {
    cin >> P[i];
  }

  vector< int > ord(N);
  iota(begin(ord), end(ord), 0);
  sort(begin(ord), end(ord), [&](const int &a, const int &b) {
    if(P[a] + P[b] == P[b] + P[a]) return P[a].size() > P[b].size();
    return P[a] + P[b] > P[b] + P[a];
  });

  string concat;
  for(auto &p : ord) concat += P[p];
  priority_queue< pair< string, vector< int > > > que;
  que.emplace(concat, ord);
  unordered_set< string > memo;
  memo.emplace(concat);

  auto normalize = [&](vector< int > v) {
    for(int i = 1; i < v.size(); i++) {
      for(int j = i; j > 0; j--) {
        if(P[v[j]] + P[v[j - 1]] == P[v[j - 1]] + P[v[j]]) {
          if(P[v[j]].size() > P[v[j - 1]].size()) {
            swap(v[j], v[j - 1]);
          } else {
            break;
          }
        }
      }
    }
    return v;
  };

  while(que.size() && K > 0) {
    auto p = que.top().second;

    cout << que.top().first << endl;
    --K;

    que.pop();

    for(int i = 0; i < ord.size(); i++) {
      for(int j = i - 1; j >= 0; j--) {
        if(P[p[i]] + P[p[j]] >= P[p[j]] + P[p[i]]) continue;
        swap(p[i], p[j]);
        string con;
        for(auto &q : p) con += P[q];
        if(memo.count(con)) continue;
        memo.emplace(con);
        que.emplace(con, normalize(p));
        swap(p[i], p[j]);
        break;
      }
    }
  }
}


0