結果

問題 No.775 tatyamと素数大富豪(hard)
ユーザー PachicobuePachicobue
提出日時 2018-12-22 17:38:48
言語 C++17(clang)
(17.0.6 + boost 1.83.0)
結果
TLE  
実行時間 -
コード長 1,905 bytes
コンパイル時間 1,431 ms
コンパイル使用メモリ 95,996 KB
実行使用メモリ 7,780 KB
最終ジャッジ日時 2023-08-20 11:32:47
合計ジャッジ時間 7,302 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 2 ms
4,384 KB
testcase_04 AC 5 ms
4,380 KB
testcase_05 AC 11 ms
4,384 KB
testcase_06 AC 92 ms
4,388 KB
testcase_07 AC 447 ms
4,384 KB
testcase_08 AC 441 ms
4,380 KB
testcase_09 TLE -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

#pragma GCC optimize("O3")
#pragma GCC target("tune=native")
#pragma GCC target("avx")
#include <algorithm>
#include <vector>
#include <iostream>
#include <numeric>
using ll = long long;
int main()
{
    int N, K;
    std::cin >> N >> K;
    std::vector<std::string> a(N);
    for (int i = 0; i < N; i++) { std::cin >> a[i]; }
    auto comp = [](const std::string& s1, const std::string& s2) { return s1 + s2 == s2 + s1 ? s1.size() > s2.size() : s1 + s2 > s2 + s1; };
    std::sort(a.begin(), a.end(), comp);
    std::vector<int> ind(N);
    std::iota(ind.begin(), ind.end(), 0);
    auto next = [&]() -> bool {
        std::string now;
        for (int i = 0; i < N; i++) { now += a[ind[i]]; }
        std::string max;
        std::vector<int> maxind;
        for (int i = N - 2, icnt = 0; i >= 0; i--) {
            if (ind[i] >= ind[i + 1]) { continue; }
            icnt++;
            for (int j = N - 1, jcnt = 0; j > i; j--) {
                if (ind[j] <= ind[i]) { continue; }
                jcnt++;
                std::swap(ind[i], ind[j]);
                std::reverse(ind.begin() + i + 1, ind.end());
                for (int j = N - 1; j > i; j--) {
                    std::swap(ind[i + 1], ind[j]);
                    std::string s;
                    for (int i = 0; i < N; i++) { s += a[ind[i]]; }
                    if (s < now and max < s) { max = s, maxind = ind; }
                    std::swap(ind[i + 1], ind[j]);
                }
                std::reverse(ind.begin() + i + 1, ind.end());
                std::swap(ind[i], ind[j]);
                if (jcnt > 20) { break; }
            }
            if (icnt > 20) { break; }
        }
        return ind = maxind, (max != "");
    };
    for (int i = 0; i < K; i++) {
        for (int i = 0; i < N; i++) { std::cout << a[ind[i]]; }
        std::cout << std::endl;
        if (not next()) { break; }
    }
    return 0;
}
0