王者荣耀
https://ac.nowcoder.com/acm/contest/318/J
題解:
時間可以任意安排,只需要考慮時長即可
本題為一個裸的01背包,背包的大小為T-S。
第i 個時間段可以看成一個物品,這個物品的體積w[i]=b[i]-a[i],價值v[i] 為熟練度c。那么dp[i][j]就表示著在前i 個時間段里花費j 的時間練英雄能得到的最大熟練度。
狀態轉移方程為:
當然也可以考慮用滾動數組來優化一下空間復雜度,此時的狀態轉移方程為:
?
C++版本一?
/* *@Author: STZG *@Language: C++ */ #include <bits/stdc++.h> #include<iostream> #include<algorithm> #include<cstdlib> #include<cstring> #include<cstdio> #include<string> #include<vector> #include<bitset> #include<queue> #include<deque> #include<stack> #include<cmath> #include<list> #include<map> #include<set> //#define DEBUG #define RI register int using namespace std; typedef long long ll; //typedef __int128 lll; const int N=1000+10; const int MOD=1e9+7; const double PI = acos(-1.0); const double EXP = 1E-8; const int INF = 0x3f3f3f3f; int t,n,m,k,q,s; int w[N]; int v[N]; ll dp[N]; int main() { #ifdef DEBUGfreopen("input.in", "r", stdin);//freopen("output.out", "w", stdout); #endifwhile(~scanf("%d%d%d",&n,&s,&t)){int a,b;memset(dp,0,sizeof(dp));for(int i=1;i<=n;i++){scanf("%d%d%d",&a,&b,&w[i]);v[i]=b-a;for(int j=t-s;j>=v[i];j--){dp[j]=max(dp[j],dp[j-v[i]]+w[i]);}}printf("%lld\n",dp[t-s]);}//cout << "Hello world!" << endl;return 0; }C++版本二
#include<stdio.h> #include<iostream> #include<algorithm> #include<string.h> #include<string> #include<math.h> #include<cstdlib> #include<stdlib.h> #include<queue> #include<map> #include<set> #include<stack> #include<time.h> #include<deque> #define bug printf("*********\n"); #define mem0(a) memset(a, 0, sizeof(a)); #define mem1(a) memset(a, -1, sizeof(a)); #define finf(a, n) fill(a, a+n, INF); #pragma comment(linker, "/STACK:102400000,102400000") using namespace std; typedef long long LL; typedef unsigned long long uLL; typedef pair<LL, pair<int, LL> > LLppar; typedef pair<int, int> par; typedef pair<LL, int> LLpar; const LL mod = 1e9+7; const LL INF = 1e9+7; const int base = 131; const double pi = 3.1415926; const double eps = 0.000000001;int n, S, T; LL dp[1010], w[1010], v[1010];int main() { #ifndef ONLINE_JUDGE//freopen("in.txt", "r", stdin);//freopen("out.txt", "w", stdout); #endifint a, b;while(~scanf("%d%d%d", &n, &S, &T)) {mem0(dp);int sum = T-S;for(int i = 0; i < n; i ++) {scanf("%d%d%d", &a, &b, &v[i]);w[i] = b-a;}for(int i = 0; i < n; i ++) {for(int j = sum; j >= w[i]; j --) {dp[j] = max(dp[j], dp[j-w[i]] + v[i]);}}printf("%lld\n", dp[sum]);} #ifndef ONLINE_JUDGEcout <<"It costs " <<clock() <<" ms\n"; #endifreturn 0; }?
與50位技術專家面對面20年技術見證,附贈技術全景圖總結
- 上一篇: LLLYYY的数字思维
- 下一篇: 彪神666