爱摸鱼的zhrt 01背包

版权声明:欢迎转载,不要求署名~~~ https://blog.csdn.net/shadandeajian/article/details/82533836

来源:长沙理工大学2018区域赛个人选拔赛1

题目:

描述:
爱摸鱼的LMJ最近喜欢上了B站的鬼畜视频,为了拉人一起摸鱼,他还喊上了zhrt一起看。
现在zhrt关注了一个UP主,这个UP主投稿了N个视频,每个视频都有一个点击量 V i ​,zhrt喜欢看点击量高的视频,可是爱学习的zhrt每天又规定了刷题时间,
于是每天只能看T秒视频(没错,要按秒计算~),设第i个视频的时长为​ t i 秒(精明的zhrt不会计算切换视频的时间)。
zhrt想使所有视频的总点击量尽量高,并且要求你告诉他最高的总点击量是多少(一个视频只能看一次)。

输入:
第一行输入一个C C 100 )表示输入有C组。
对于每一组,第一行输入2个整数NT N , T 100 ),表示UP主有N个鬼畜视频,zhrt今天能看T秒视频。
接下来N行,每行输入2个整数 V i t i V i , t i 100 ),表示第i个视频点击量为 V i ,时长为 t i 秒。

输出:
对于每组数据,输出占一行,输出视频的总点击量。

样例输入:
1
2 4
1 3
2 3

样例输出:
2

题解:

01背包

AC代码:

#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#define debug(x) cout<<#x<<" = "<<x<<endl;
#define INF 0x3f3f3f3f
using namespace std;

const int maxn = 110;
int val[maxn], ttime[maxn];
int dp[maxn][maxn];
int main(void) {
    int T;
    cin >> T;
    while (T--) {
        memset(dp, 0, sizeof dp);
        int n, TIME;
        cin >> n >> TIME;
        for (int i = 1; i <= n; i++)
            cin >> val[i] >> ttime[i];
        for (int i = 1; i <= n; i++)
            for (int j = 1; j <= TIME; j++) {
                if (j >= ttime[i])
                    dp[i][j] = max(dp[i - 1][j], dp[i - 1][j - ttime[i]] + val[i]);
                else
                    dp[i][j] = dp[i - 1][j];
            }
        cout << dp[n][TIME] << endl;
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/shadandeajian/article/details/82533836