Algoogle

Algorithm for Programming Contest

AOJ 2263 The First Acceptance

Category: AOJ Tag: dp

The First Acceptance

問題概要


解法


まずBについて(B,A)を昇順ソートする
dpで解く
dp[i][j] := i問目を解くまでにj問解いている場合の最速タイム
dp[i+1][j] = min(dp[i+1][j], dp[i][j])
if(dp[i][j] + A[i] <= B[i]) dp[i+1][j+1] = min(dp[i+1][j+1], dp[i][j] + A[i])

コード


(2263.cpp) download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include <bits/stdc++.h>
using namespace std;
#define rep(i,a) for(int i = 0;i < (a); i++)
#define repd(i,a,b) for(int i = (a); i >= (b); i--)
#define all(u) (u).begin(),(u).end()
#define pb push_back
#define INF 1e9
typedef pair<int,int> pii;

vector<pii> p;
int n;

int main(){
    cin >> n;
    rep(i,n){
        int a, b;
        cin >> a >> b;
        p.pb(pii(b,a));
    }
    sort(all(p));
    int dp[1024][1024];
    rep(i,1024)rep(j,1024) dp[i][j] = INF;
    dp[0][0] = 0;

    rep(i,n)rep(j,i+1){
        dp[i+1][j] = min(dp[i+1][j], dp[i][j]);
        if(dp[i][j] + p[i].second <= p[i].first)
            dp[i+1][j+1] = min(dp[i+1][j+1], dp[i][j] + p[i].second);
    }
    int ans = 0;
    repd(i,n,0) if(dp[n][i] != INF){
        ans = i;
        break;
    }
    cout << ans << endl;
    return 0;
}

Comments