codeforces(背包)

本文介绍了一种解决CodeForces竞赛中动态调整分数问题的算法。通过计算每道题目的时间价值比来确定解题顺序,并采用0-1背包算法优化得分策略。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

codeforces

发布时间: 2017年5月13日 22:40   最后更新: 2017年5月14日 16:39   时间限制: 1000ms   内存限制: 128M

Clipboard Image.png

LYD loves codeforces since there are many Russian contests. In an contest lasting for T minutes there are n problems, and for the ith problem you can get ai−di∗ti points, where ai indicates the initial points, di indicates the points decreased per minute (count from the beginning of the contest), and ti stands for the passed minutes when you solved the problem (count from the beginning of the contest).
Now you know LYD can solve the ith problem in ci minutes. He can't perform as a multi-core processor, so he can think of only one problem at a moment. Can you help him get as many points ashe can?

The first line contains two integers n,T(0≤n≤2000,0≤T≤5000).
The second line contains n integers a1,a2,..,an(0<ai≤6000).
The third line contains n integers d1,d2,..,dn(0<di≤50).
The forth line contains n integers c1,c2,..,cn(0<ci≤400).

Output an integer in a single line, indicating the maximum points LYD can get.

 复制
3 10
100 200 250
5 6 7
2 4 10
思路:对于每个题,都会随时降分,设两个题 a,b , 则对于da  db  ta  tb ,如果能做,都能做完,该先做那个哪? 我是这样判断的:如果先做a那么总的浪费就是(at + bt) * db   +  at * da     若先做 b题那么总的浪费将是 (at+bt)*da + bt * db      令1式<2式,得到 at/da  < bt/db   那么就得到了优先级,即
t/d小的优先,排完序后,按照0,1背包的思路判断每个题做还是不做,得到答案。
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
struct node{
    int score;
    int d;
    int t;
    double op;
    bool operator <(const node nd)const
    {
        return op < nd.op;
    }
};
node a[100005];
int cash[50000];
int dp[100050];
int main()
{
    int n,T;
    scanf("%d%d",&n,&T);
    for(int i = 1; i <= n; i++)
        scanf("%d",&a[i].score);
    for(int i = 1; i <= n; i++)
        scanf("%d",&a[i].d);
    for(int i = 1; i <= n; i++)
    {
        scanf("%d",&a[i].t);
        a[i].op = a[i].t*1.0 / a[i].d;
    }
    sort(a+1,a+1+n);
    int max_ = -1;
    for(int i = 1; i <= n; i++)
    {
        for(int j = T; j >= a[i].t; j--)
        {
            dp[j] = max(dp[j],dp[j-a[i].t] + max(0,a[i].score - j*a[i].d));
            max_ = max(max_,dp[j]);
        }
    }
    printf("%d\n",max_);
    return 0;
}



### 关于Codeforces平台上的动态规划问题 在Codeforces这样的编程竞赛平台上,动态规划(Dynamic Programming, DP)是一类非常重要的算法技术。这类题目通常涉及优化子结构和重叠子问题两个特性。 #### 动态规划示例解析 考虑一个典型的DP问题,在给定条件下求解最优方案的数量或具体路径等问题。例如,在某些情况下,可能需要计算达到特定状态所需的最少步数或是最大收益等[^1]。 对于具体的例子而言,假设有一个序列`a[]`,目标是从左到右遍历此序列并决定是否选取当前元素加入集合中,最终目的是让所选元素之和尽可能大而不超过某个上限值M。这个问题可以通过定义二维数组dp[i][j]表示从前i个物品里挑选若干件放入容量为j的背包可以获得的最大价值来建模: - 如果不取第i项,则`dp[i][j]=dp[i−1][j]`; - 若选择第i项且其重量w不超过剩余空间j,则更新为`max(dp[i−1][j], dp[i−1][j-w]+v)`其中v代表该项的价值; 最后的结果保存在`dp[n][m]`处(n为总项目数量,m为目标体积)[^2]。 ```cpp #include <iostream> using namespace std; const int N = 1e3 + 5; int w[N]; // weights of items int v[N]; // values of items long long f[N][N]; void knapsack(int n, int m){ for (int i = 1; i <= n; ++i) { for (int j = 0; j <= m; ++j) { f[i][j] = f[i - 1][j]; if(j >= w[i]) f[i][j] = max(f[i][j],f[i - 1][j - w[i]] + v[i]); } } } ``` 上述代码展示了如何利用记忆化搜索的方式实现简单的0/1背包问题解决方案,这同样适用于其他形式更复杂的动态规划挑战。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值