結果

問題 No.54 Happy Hallowe'en
ユーザー codershifthcodershifth
提出日時 2015-08-21 00:10:44
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 169 ms / 5,000 ms
コード長 2,443 bytes
コンパイル時間 1,409 ms
コンパイル使用メモリ 166,624 KB
実行使用メモリ 6,944 KB
最終ジャッジ日時 2024-04-23 16:42:31
合計ジャッジ時間 2,726 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,816 KB
testcase_01 AC 1 ms
6,940 KB
testcase_02 AC 1 ms
6,944 KB
testcase_03 AC 1 ms
6,940 KB
testcase_04 AC 24 ms
6,940 KB
testcase_05 AC 41 ms
6,944 KB
testcase_06 AC 62 ms
6,944 KB
testcase_07 AC 94 ms
6,944 KB
testcase_08 AC 124 ms
6,940 KB
testcase_09 AC 168 ms
6,944 KB
testcase_10 AC 1 ms
6,940 KB
testcase_11 AC 1 ms
6,944 KB
testcase_12 AC 169 ms
6,940 KB
testcase_13 AC 163 ms
6,940 KB
testcase_14 AC 1 ms
6,940 KB
testcase_15 AC 1 ms
6,944 KB
testcase_16 AC 2 ms
6,944 KB
testcase_17 AC 2 ms
6,940 KB
testcase_18 AC 1 ms
6,944 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

typedef long long ll;
typedef unsigned long long ull;

#define FOR(i,a,b) for(int (i)=(a);i<(int)(b);i++)
#define REP(i,n) FOR(i,0,n)
#define RANGE(vec) (vec).begin(),(vec).end()

using namespace std;


class HappyHalloween {
public:
    void solve(void) {
            int N;
            cin>>N;

            int maxT = 0;
            int maxV = 0;
            vector<pair<int,int>> candy;
            REP(i,N)
            {
                int v,t;
                cin>>v>>t;
                maxT = max(maxT,t);
                maxV = max(maxV,v);
                candy.emplace_back(v,t);
            }
            // 現在の candy の個数を C とするとき
            // C < T[a], C < T[b] として家 a, b を訪れるケースを考える。
            // a,b どちらが先でもキャンディをもらえるならどちらの順でもよいが、
            // そうでないケースを考える
            //
            // 1. a -> b 順でキャンディがもらえるとき C+V[a] < T[b]
            // 2. b -> a 順ではキャンディがもらえないとい C+V[b] >= T[a]
            //
            // 1,2 が同時に成り立つときは
            //
            // V[a]+T[a] < V[b]+T[b]
            //
            // となる。よって V[i]+T[i] でソートした順訪れれば良い。
            //

            // O(N*log(N))
            sort(RANGE(candy), [](const pair<int,int> &a, const pair<int,int> &b){
                    return a.first+a.second < b.first+b.second;
                });

            // dp[C] := キャンディを C 個もらえるか?
            vector<bool> dp(maxV+maxT+1,false);

            // O(N*maxT)
            dp[0] = true;
            REP(i,N)
            {
                int v = candy[i].first;
                // i のときの dp 更新を伝播させないために逆順にやる
                // C < T[i] のときしかもらえないので T[i]-1 から
                for (int C = candy[i].second-1; C >= 0; --C)
                    dp[v+C] = dp[v+C] | dp[C];
            }

            int C;
            for (C = dp.size()-1; C >= 0; --C)
                if (dp[C])
                    break;
            cout<<C<<endl;
    }
};

#if 1
int main(int argc, char *argv[])
{
        ios::sync_with_stdio(false);
        auto obj = new HappyHalloween();
        obj->solve();
        delete obj;
        return 0;
}
#endif
0