結果

問題 No.1029 JJOOII 3
ユーザー hirono999hirono999
提出日時 2020-04-18 21:12:07
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
RE  
実行時間 -
コード長 5,082 bytes
コンパイル時間 2,184 ms
コンパイル使用メモリ 190,032 KB
実行使用メモリ 8,960 KB
最終ジャッジ日時 2024-10-04 00:19:24
合計ジャッジ時間 15,866 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 19 ms
8,704 KB
testcase_01 AC 13 ms
8,832 KB
testcase_02 AC 26 ms
8,704 KB
testcase_03 RE -
testcase_04 RE -
testcase_05 RE -
testcase_06 WA -
testcase_07 RE -
testcase_08 RE -
testcase_09 RE -
testcase_10 RE -
testcase_11 RE -
testcase_12 RE -
testcase_13 RE -
testcase_14 RE -
testcase_15 AC 248 ms
8,832 KB
testcase_16 RE -
testcase_17 AC 236 ms
8,960 KB
testcase_18 RE -
testcase_19 RE -
testcase_20 RE -
testcase_21 RE -
testcase_22 RE -
testcase_23 RE -
testcase_24 RE -
testcase_25 RE -
testcase_26 RE -
testcase_27 AC 286 ms
8,904 KB
testcase_28 RE -
testcase_29 RE -
testcase_30 AC 290 ms
8,832 KB
testcase_31 RE -
testcase_32 AC 370 ms
8,704 KB
testcase_33 AC 364 ms
8,576 KB
testcase_34 AC 364 ms
8,704 KB
testcase_35 AC 366 ms
8,704 KB
testcase_36 AC 365 ms
8,832 KB
testcase_37 AC 12 ms
8,572 KB
testcase_38 AC 20 ms
8,704 KB
testcase_39 RE -
testcase_40 RE -
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function 'int main()':
main.cpp:109:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17' [-Wc++17-extensions]
  109 |         auto [s, v] = S[i];
      |              ^
main.cpp:126:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17' [-Wc++17-extensions]
  126 |         auto [s1, v1] = S[i];
      |              ^
main.cpp:127:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17' [-Wc++17-extensions]
  127 |         auto [s2, v2] = S[j];
      |              ^

ソースコード

diff #

#include <bits/stdc++.h>
const long long INF = 1LL << 60;
const long long MOD = 1000000007;
#define rep(i, n) for (ll i = 0; i < (n); ++i)
#define rep1(i, n) for (ll i = 1; i <= (n); ++i)
#define rrep(i, n) for (ll i = (n - 1); i >= 0; --i)
#define perm(c) sort(ALL(c));for(bool c##p=1;c##p;c##p=next_permutation(ALL(c)))
#define ALL(obj) (obj).begin(), (obj).end()
#define RALL(obj) (obj).rbegin(), (obj).rend()
#define pb push_back
#define to_s to_string
#define len(v) (ll)v.size()
#define UNIQUE(v) v.erase(unique(v.begin(), v.end()), v.end())
#define print(x) cout << (x) << '\n'
#define drop(x) cout << (x) << '\n', exit(0)
#define debug(x) cout << #x << ": " << (x) << '\n'
using namespace std;
using ll = long long;
typedef pair<ll, ll> P;
typedef vector<ll> vec;
typedef vector<vector<ll>> vec2;
typedef vector<vector<vector<ll>>> vec3;
template<class S, class T> inline bool chmax(S &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class S, class T> inline bool chmin(S &a, const T &b) { if (b<a) { a=b; return 1; } return 0; }
inline ll msb(ll v) { return 1 << (31 - __builtin_clzll(v)); }
inline ll devc(ll x, ll y) { return (x + y - 1) / y; }

struct IoSetup {
    IoSetup() {
        cin.tie(nullptr);
        ios::sync_with_stdio(false);
        cout << fixed << setprecision(10);
        cerr << fixed << setprecision(10);
    }
} iosetup;

template< typename T1, typename T2 >
ostream &operator << (ostream &os, const pair< T1, T2 > &p) {
    os << p.first << " " << p.second;
    return os;
}

template< typename T1, typename T2 >
istream &operator >> (istream &is, pair< T1, T2 > &p) {
    is >> p.first >> p.second;
    return is;
}

template< typename T >
ostream &operator << (ostream &os, const vector< T > &v){
    for (int i = 0; i < (int)v.size(); ++i) {
        os << v[i] << (i + 1 != v.size() ? " " : "");
    }
    return os;
}

template< typename T >
istream &operator >>  (istream &is, vector< T > &v){
    for(T &in : v) is >> in;
    return is;
}

/*---------------------------------    Tools    ------------------------------------------*/
template< typename T >
vector<T> cumsum(const vector<T> &X){
    vector<T> res(X.size() + 1, 0);
    for(int i = 0; i < X.size(); ++i) res[i + 1] += res[i] + X[i];
    return res;
}

//f : 単調性の関数
template< typename S, typename T, typename F>
pair<T, T> bisearch(S left, T right, F f) {
    while(abs(right - left) > 1){
        T mid = (right + left) / 2;
        if(f(mid)) right = mid;
        else left = mid;
    }
    return {left, right};
}

template< typename S, typename T, typename F>
double trisearch(S left, T right, F f, double precision = 1e-10){
    double low = left, high = right;
    while(high - low > precision){
        double mid_left = high / 3 + low * 2 / 3;
        double mid_right = high * 2 / 3 + low / 3;
        if(f(mid_left) >= f(mid_right)) low = mid_left;
        else high = mid_right;
    }
    return high;
}


/*------------------------------- Main Code Here -----------------------------------------*/

int main()
{
    ll N, K;
    cin >> N >> K;
    vector<pair<string, ll>> S(N);
    cin >> S;

    vec2 f(1e5 + 1, vec(3, INF)); //0 : J, 1 : O, 2 : I
    f[0][0] = f[0][1] = f[0][2] = 0;

    rep(i, N){
        map<char, ll> mp;
        auto [s, v] = S[i];
        for(auto c : s) ++mp[c];

        for (ll j = 0; j <= 1e5; ++j){
            if(j >= mp['J']) chmin(f[j][0], f[j - mp['J']][0] + v);
            if(j >= mp['O']) chmin(f[j][1], f[j - mp['O']][1] + v);
            if(j >= mp['I']) chmin(f[j][2], f[j - mp['I']][2] + v);
        }
        for (ll j = 1e5; j >= 1; --j) rep(k, 3) chmin(f[j - 1][k], f[j][k]);
    }

    if(f[K][1] == INF or f[K][2] == INF or f[K][3] == INF) drop(-1);


    ll ans = f[K][0] + f[K][1] + f[K][2];

    rep(i, N) rep(j, N){
        auto [s1, v1] = S[i];
        auto [s2, v2] = S[j];
        ll l1 = len(s1), l2 = len(s2);

        vec J1(l1), O1(l1), I1(l1);
        vec J2(l2), O2(l2), I2(l2);

        rep(k, l1){
            J1[k] = s1[k] == 'J';
            O1[k] = s1[k] == 'O';
            I1[k] = s1[k] == 'I';
        }
        rep(k, l2){
            J2[k] = s2[k] == 'J';
            O2[k] = s2[k] == 'O';
            I2[k] = s2[k] == 'I';
        }
        vec j1, o1, i1, j2, o2, i2;
        j1 = cumsum(J1);
        o1 = cumsum(O1);
        i1 = cumsum(I1);
        j2 = cumsum(J2);
        o2 = cumsum(O2);
        i2 = cumsum(I2);

        rep(x, l1 + 1) rep(y, l2 + 1){
            ll ctj = j1[x];
            ll cto = o1.back() - o1[x] + o2[y];
            ll cti = i2.back() - i2[y];

            chmin(ans, v1 + v2 + f[max(K - ctj, 0LL)][0] + f[max(K - cto, 0LL)][1] + f[max(K - cti, 0LL)][2]);
        }

        if(j == 0){
            rep(x, l1 + 1) rep(y, l2 + 1) if(x < y) {
                ll ctj = j1[x];
                ll cto = o1[y] - o1[x];
                ll cti = i1.back() - i1[y];

                chmin(ans, v1 + f[max(K - ctj, 0LL)][0] + f[max(K - cto, 0LL)][1] + f[max(K - cti, 0LL)][2]);
            }
        }
    }

    print(ans);

    return 0;
}
0