• Checkout Assistant CodeForces


    题意:

    给你n个物品,每个物品有一个价格ci和一个支付时间ti,在这个ti时间内,你可以免费拿ti个物品。问你想要带走这n个物品最小需要多少钱

    题解:

    原本还想着贪心去写,但是好像贪心写不了,,,不属于贪心
     
    因为题目上说了要求把n个商品都买下所付出的最小的钱
    因为买了第i件商品可以免费拿出来ti个,可以相当于一共拿出来ti+1个
    那么这就相当于01背包了,n当作背包体积。但是要注意,如果背包剩余空间不够当前操作导致无法求出最优解呢?
    所以背包剩余体积就算不够也可以放进去(具体见代码)
     
     
    代码:
     1 #include<stdio.h>
     2 #include<string.h>
     3 #include<iostream>
     4 #include<algorithm>
     5 #include<queue>
     6 #include<map>
     7 #include<vector>
     8 #include<math.h>
     9 #define mem(a,x) memset(a,x,sizeof(a))
    10 using namespace std;
    11 typedef long long ll;
    12 const int maxn=2000+10;
    13 const int mod=26;
    14 const int INF=0x3f3f3f3f;
    15 const int Times = 10;
    16 const int N = 5500;
    17 const long long int MAX=(long long int)1<<62;
    18 ll dp[maxn];
    19 struct shudui
    20 {
    21     ll t,c;
    22 }m[maxn];
    23 int main()
    24 {
    25     ll n;
    26     scanf("%I64d",&n);
    27     for(ll i=0;i<n;++i)
    28     {
    29         scanf("%I64d%I64d",&m[i].t,&m[i].c);
    30         m[i].t+=1;
    31     }
    32     for(int i=0;i<=n;++i)
    33         dp[i]=MAX;  //这里要用MAX而不能用INF
    34     dp[0]=0;
    35     for(ll i=0;i<n;++i)
    36     {
    37         for(ll j=n;j>0;--j)  //注意这个for循环j得终止条件是j<=0
    38         {
    39             ll ans;
    40             //这个就是背包溢出的处理
    41             if(j<m[i].t) ans=0;
    42             else ans=j-m[i].t;
    43             dp[j]=min(dp[j],dp[ans]+m[i].c);
    44         }
    45     }
    46     printf("%I64d
    ",dp[n]);
    47     return 0;
    48 }
  • 相关阅读:
    Javascript事件处理进阶
    Restful API设计指南
    Git&GitHub
    Linux补充
    堡垒机
    Python发送邮件
    js获取当前页面url网址信息
    高并发的秒杀系统
    CMDB开发
    Tornado
  • 原文地址:https://www.cnblogs.com/kongbursi-2292702937/p/12691124.html
Copyright © 2020-2023  润新知