洛谷 P1417 烹调方案 (01背包拓展)
生活随笔
收集整理的這篇文章主要介紹了
洛谷 P1417 烹调方案 (01背包拓展)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
一看到這道題就是01背包
但是我注意到價值和當前的時間有關。
沒有想太多,直接寫,0分
然后發現輸入方式不對……
改了之后只有25分
我知道wa是因為時間會影響價值,但不知道怎么做。
后來看了題解,發現我對01背包理解不夠透徹
普通01背包做下來放入物品的順序是1到n的
因為這個時候順序沒有關系,所以可以直接做
但是這道題后面放的物品價值小,所以價值有關系
所以就要提前排好序。
排序的依據就判斷相鄰兩個物品先后放的價值,
然后化簡可以推出一個式子。
這里其實是一個貪心。
然后就做01背包就好了
然后注意開long long?
#include<cstdio> #include<algorithm> #include<cstring> #define REP(i, a, b) for(int i = (a); i < (b); i++) #define _for(i, a, b) for(int i = (a); i <= (b); i++) using namespace std;typedef long long ll; const int MAXN = 112; const int MAXM = 112345; ll f[MAXM]; struct node {ll a, b, c;bool operator < (const node& rhs) const{return c * rhs.b < rhs.c * b;} }s[MAXN]; int t, n;int main() {scanf("%d%d", &t, &n);REP(i, 0, n) scanf("%lld", &s[i].a);REP(i, 0, n) scanf("%lld", &s[i].b);REP(i, 0, n) scanf("%lld", &s[i].c);sort(s, s + n);ll ans = 0;REP(i, 0, n)for(int j = t; j >= s[i].c; j--){f[j] = max(f[j], f[j-s[i].c] + s[i].a - j * s[i].b);ans = max(ans, f[j]);} printf("%lld\n", ans);return 0; }?
轉載于:https://www.cnblogs.com/sugewud/p/9819383.html
總結
以上是生活随笔為你收集整理的洛谷 P1417 烹调方案 (01背包拓展)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 前端学习(3333):ant desig
- 下一篇: LoRa协议在Arduino上的应用——