3 条题解
-
1邓梓昊 LV 7 @ 2024-12-17 13:46:32
#include <stdio.h> #include <iostream> using namespace std; inline int read(string n){ int x = 0, f = 1; printf("%s", n.c_str()); char c = getchar(); while(c < '0' || c > '9'){ if(c == '-') f = -1; c = getchar(); } while(c >= '0' && c <= '9'){ x = x * 10 + c - 48; c = getchar(); } return x * f; } inline float input(string n){ float x = 0, f = 1, x2 = 0, cnt = 0, i = 0; printf("%s", n.c_str()); char c = getchar(); while(c < '0' || c > '9'){ if(c == '-') f = -1; c = getchar(); } while(c >= '0' && c <= '9'){ x = x * 10 + c - 48; c = getchar(); } c = getchar(); while(c >= '0' && c <= '9'){ x2 = x2 * 10 + c - 48; cnt++; c = getchar(); } for(; i < cnt; i++) x2 /= 10.0; return (x + x2) * f; } void write(int n) { if(n < 0){ putchar('-'); n = -n; } if(n > 9) write(n / 10); putchar(n % 10 + '0'); return; } void print(float n){ printf("%lf\n", n); return; } int m, n, f[20010], w[40], i, j; int main(){ m = read(""); n = read(""); for(i = 1; i <= n; i++) w[i] = read(""); for(i = 1; i <= n; i++) for(j = m; j >= w[i]; j--) if(f[j] < f[j - w[i]] + w[i]) f[j] = f[j - w[i]] + w[i]; write(m-f[m]); return 0; }
-
02023-5-19 19:17:49@
👍
#include<iostream> using namespace std; int n, v; int t[35]; int dp[40000]; int main() { cin >> n >> v; for(int i = 1;i <= n;i++) cin >> t[i]; for(int i = 1;i <= n;i++) { for(int j = v;j >= t[i];j--) { dp[j] = max(dp[j],dp[j - t[i]] + t[i]); } } cout << v - dp[v] << endl; return 0; }
-
-12023-9-3 12:06:14@
#include <iostream> using namespace std; const int maxn=1e6+10; int w[maxn],dp[maxn]; int main(){ int n,v;cin>>n>>v; for(int i=1;i<=n;i++)cin>>w[i]; for(int i=1;i<=n;i++){ for(int j=v;j>=w[i];j--){ dp[j]=max(dp[j],dp[j-w[i]]+w[i]); } } cout<<v-dp[v]<<endl; return 0; }
- 1
信息
- ID
- 652
- 时间
- 1000ms
- 内存
- 128MiB
- 难度
- 7
- 标签
- 递交数
- 127
- 已通过
- 32
- 上传者