結果

問題 No.949 飲酒プログラミングコンテスト
ユーザー iiljjiiljj
提出日時 2020-02-08 11:27:05
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 252 ms / 2,500 ms
コード長 8,678 bytes
コンパイル時間 1,913 ms
コンパイル使用メモリ 179,440 KB
実行使用メモリ 4,552 KB
最終ジャッジ日時 2023-10-26 00:47:52
合計ジャッジ時間 6,218 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 2 ms
4,348 KB
testcase_02 AC 2 ms
4,348 KB
testcase_03 AC 2 ms
4,348 KB
testcase_04 AC 2 ms
4,348 KB
testcase_05 AC 2 ms
4,348 KB
testcase_06 AC 2 ms
4,348 KB
testcase_07 AC 2 ms
4,348 KB
testcase_08 AC 4 ms
4,348 KB
testcase_09 AC 3 ms
4,348 KB
testcase_10 AC 3 ms
4,348 KB
testcase_11 AC 132 ms
4,348 KB
testcase_12 AC 189 ms
4,348 KB
testcase_13 AC 132 ms
4,348 KB
testcase_14 AC 19 ms
4,348 KB
testcase_15 AC 91 ms
4,348 KB
testcase_16 AC 198 ms
4,552 KB
testcase_17 AC 26 ms
4,348 KB
testcase_18 AC 216 ms
4,552 KB
testcase_19 AC 96 ms
4,348 KB
testcase_20 AC 20 ms
4,348 KB
testcase_21 AC 5 ms
4,348 KB
testcase_22 AC 8 ms
4,348 KB
testcase_23 AC 2 ms
4,348 KB
testcase_24 AC 130 ms
4,348 KB
testcase_25 AC 209 ms
4,552 KB
testcase_26 AC 223 ms
4,552 KB
testcase_27 AC 194 ms
4,552 KB
testcase_28 AC 252 ms
4,552 KB
testcase_29 AC 130 ms
4,552 KB
testcase_30 AC 47 ms
4,552 KB
testcase_31 AC 22 ms
4,552 KB
testcase_32 AC 204 ms
4,552 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

/* #region Head */

#include <bits/stdc++.h>
using namespace std;

using ll = long long;
using ull = unsigned long long;
using pll = pair<ll, ll>;
using vll = vector<ll>;
using vvll = vector<vll>;
using vd = vector<double>;
using vvd = vector<vd>;
using vs = vector<string>;
using vvs = vector<vs>;

#define REP(i, m, n) for (ll i = (m), i##_len = (ll)(n); i < i##_len; ++(i))
#define REPM(i, m, n) for (ll i = (m), i##_max = (ll)(n); i <= i##_max; ++(i))
#define REPR(i, m, n) for (ll i = (m), i##_min = (ll)(n); i >= i##_min; --(i))
#define REPD(i, m, n, d) for (ll i = (m), i##_len = (ll)(n); i < i##_len; i += (d))
#define REPMD(i, m, n, d) for (ll i = (m), i##_max = (ll)(n); i <= i##_max; i += (d))
#define REPI(itr, ds) for (auto itr = ds.begin(); itr != ds.end(); itr++)
#define ALL(x) begin(x), end(x)
#define SIZE(x) ((ll)(x).size())

constexpr ll INF = 1'010'000'000'000'000'017LL;
constexpr ll MOD = 1'000'000'007LL; // 1e9 + 7
constexpr double EPS = 1e-12;
constexpr double PI = 3.14159265358979323846;

// vector入力
template <typename T>
istream &operator>>(istream &is, vector<T> &vec)
{
    for (T &x : vec)
        is >> x;
    return is;
}

// vector出力
template <typename T>
ostream &operator<<(ostream &os, vector<T> &vec)
{
    ll len = SIZE(vec);
    os << "{";
    for (int i = 0; i < len; i++)
        os << vec[i] << (i == len - 1 ? "" : ", ");
    os << "}";
    return os;
}

// pair入力
template <typename T, typename U>
istream &operator>>(istream &is, pair<T, U> &pair_var)
{
    is >> pair_var.first >> pair_var.second;
    return is;
}

// pair出力
template <typename T, typename U>
ostream &operator<<(ostream &os, pair<T, U> &pair_var)
{
    os << "(" << pair_var.first << ", " << pair_var.second << ")";
    return os;
}

// map出力
template <typename T, typename U>
ostream &operator<<(ostream &os, map<T, U> &map_var)
{
    os << "{";
    REPI(itr, map_var)
    {
        os << *itr;
        itr++;
        if (itr != map_var.end())
            os << ", ";
        itr--;
    }
    os << "}";
    return os;
}

// set 出力
template <typename T>
ostream &operator<<(ostream &os, set<T> &set_var)
{
    os << "{";
    REPI(itr, set_var)
    {
        os << *itr;
        itr++;
        if (itr != set_var.end())
            os << ", ";
        itr--;
    }
    os << "}";
    return os;
}

// dump
#define DUMPOUT cerr
void dump_func()
{
    DUMPOUT << endl;
}
template <class Head, class... Tail>
void dump_func(Head &&head, Tail &&... tail)
{
    DUMPOUT << head;
    if (sizeof...(Tail) > 0)
    {
        DUMPOUT << ", ";
    }
    dump_func(move(tail)...);
}

// _max の終端.
template <class Head>
ll _max(Head &&head)
{
    return head;
}
// 最大値を求める.max({}) の代わり.これにしないと vscode が壊れる.
template <class Head, class... Tail>
ll _max(Head &&head, Tail &&... tail)
{
    return max((ll)head, _max((tail)...));
}

// _min の終端.
template <class Head>
ll _min(Head &&head)
{
    return head;
}
// 最小値を求める.min({}) の代わり.これにしないと vscode が壊れる.
template <class Head, class... Tail>
ll _min(Head &&head, Tail &&... tail)
{
    return min((ll)head, _min((tail)...));
}

// chmax (更新「される」かもしれない値が前)
template <typename T, typename U, typename Comp = less<>>
bool chmax(T &xmax, const U &x, Comp comp = {})
{
    if (comp(xmax, x))
    {
        xmax = x;
        return true;
    }
    return false;
}

// chmin (更新「される」かもしれない値が前)
template <typename T, typename U, typename Comp = less<>>
bool chmin(T &xmin, const U &x, Comp comp = {})
{
    if (comp(x, xmin))
    {
        xmin = x;
        return true;
    }
    return false;
}

// container 内の最初の element のインデックスを探す
template <class Container>
ll indexof(const Container &container, const typename Container::value_type &element)
{
    auto iter = find(container.begin(), container.end(), element);
    size_t index = distance(container.begin(), iter);
    if (index == container.size())
    {
        index = -1;
    }
    return index;
}

// ローカル用
#define DEBUG_

#ifdef DEBUG_
#define DEB
#define dump(...)                                                       \
    DUMPOUT << "  " << string(#__VA_ARGS__) << ": "                     \
            << "[" << to_string(__LINE__) << ":" << __FUNCTION__ << "]" \
            << endl                                                     \
            << "    ",                                                  \
        dump_func(__VA_ARGS__)
#else
#define DEB if (false)
#define dump(...)
#endif

struct AtCoderInitialize
{
    static constexpr int IOS_PREC = 15;
    static constexpr bool AUTOFLUSH = false;

    AtCoderInitialize()
    {
        ios_base::sync_with_stdio(false);
        cin.tie(nullptr);
        cout.tie(nullptr);
        cout << fixed << setprecision(IOS_PREC);
        if (AUTOFLUSH)
            cout << unitbuf;
    }
} ATCODER_INITIALIZE;

/* #endregion */

// 整数変数 n (left<=n<=right) について命題 p(n) を満たす最大の n を返す.
// そのような n が存在しない場合は -1 を返す.
template <class tProposition>
ll binarySearchIntMax(ll left, ll right, tProposition p)
{
    if (right < left)
        return -1;
    ll mid, fn;
    while (left + 1 < right)
    {
        mid = (left + right) / 2;
        if (p(mid))
            left = mid;
        else // fn > 0
            right = mid - 1;
    }

    if (p(right))
        return right;
    else if (p(left))
        return left;
    else
        return -1;
}

/**
ken さんと koo さんは、2 人でチームを組んで
「飲酒競技プログラミングコンテスト (Inshu Competitive Programming Contest; ICPC)」に参加することにしました。
ICPC は N 問の問題からなるコンテストで、以下の行動を何回か行うことでより多くの問題を解くことを目指します。

・
チームメンバーの誰か一人が飲酒をする。そして、まだ解いていない問題を 1 つ選び、その問題を解く。

各チームメンバーには貢献度が決まっており、ken さんが i 回 (0 <= i <= N) 飲酒をした後の貢献度は A_i、
koo さんが i 回 (0 <= i <= N) 飲酒をした後の貢献度は B_i です。
また各問題には難易度が決まっており、i 番目 (1 <= i <= N) の問題の難易度は D_i です。
難易度が D_i の問題は、その時点でのチームメンバーの貢献度の総和が D_i 以上ならば解くことができます。
各チームメンバーの貢献度と各問題の難易度がわかっているとき、最適な戦略を取った場合最大で何問の問題を解けるかを求めてください。
 */
void solve()
{
    ll n;
    cin >> n;
    vll a(n + 1), b(n + 1), d(n);
    cin >> a >> b >> d;

    sort(ALL(d)); // 難易度を小さい順にソート
    // 解く問題数 M が決まっているなら,それらのうち難易度の高いものから解くのが最適 => [0, M-1]
    // もし,その「難易度の高い問題」が解けないなら,そもそも M 問を解ききることはできない
    // dp[i][j]: ken i 問,koo j 問を解くことが可能か T/F
    // これで  dp[0][M], dp[1][M-1], ..., dp[M][0] のいずれかが true なら M 問解くことができる
    // M について二分探索すれば, M の最大値が求められる
    ll ret = binarySearchIntMax(0, n, [&](ll m) {
        // ll m = 4;
        vector<vector<bool>> dp(n + 1, vector<bool>(n + 1, false));
        REPM(i, 0, m)
        {
            REPM(j, 0, m - i)
            {
                if (i == 0)
                {
                    if (j == 0)
                        dp[0][0] = true; // a[0] + b[0] >= d[m - 1];
                    else
                        dp[0][j] = dp[0][j - 1] && (a[0] + b[j] >= d[m - j]);
                }
                else
                {
                    if (j == 0)
                        dp[i][0] = dp[i - 1][0] && (a[i] + b[0] >= d[m - i]);
                    else
                        dp[i][j] = (dp[i - 1][j] || dp[i][j - 1]) && (a[i] + b[j] >= d[m - i - j]);
                }
            }
        }
        bool capable = false;
        REPM(i, 0, m)
        {
            if (dp[i][m - i])
            {
                capable = true;
                break;
            }
        }
        // cout << capable << endl;
        return capable;
    });
    cout << ret << endl;
}

/**
 * エントリポイント.
 * @return 0.
 */
int main()
{
    solve();
    return 0;
}
0