编辑器存在大小不一的情况,更好的阅读体验可戳WizNote:动态规划
#include
#include
#include
#include
using namespace std;
const int N = 1e3 + 10;
int dp[N][N];
int w[N], v[N];
int main() {
int t;
scanf("%d", &t);
while(t--) {
memset(dp, 0, sizeof(dp));
int n, V;
scanf("%d %d", &n, &V);
for(int i = 1; i <= n; i++) {
scanf("%d", &v[i]);
}
for(int i = 1; i <= n; i++) {
scanf("%d", &w[i]);
}
for(int i = 1; i <= n; i++) {
for(int j = 0; j <= V; j++){
if(j < w[i]){
dp[i][j] = dp[i - 1][j];
}
else dp[i][j] = max(dp[i - 1][j], dp[i - 1][j - w[i]] + v[i]);
}
}
printf("%d\n", dp[n][V]);
}
}
#include
#include
#include
#include
using namespace std;
const int N = 1e3 + 10;
char s[N], t[N];
int dp[N][N];
int main(){
while(~scanf("%s %s", s, t)){
memset(dp, sizeof(dp), 0);
int n = strlen(s);
int m = strlen(t);
for(int i = 0; i < n; i++){
for(int j = 0; j < m; j++){
if(s[i] == t[j]){
dp[i+1][j+1] = dp[i][j] + 1;
}
else{
dp[i+1][j+1] = max(dp[i][j+1], dp[i+1][j]);
}
}
}
printf("%d\n", dp[n][m]);
}
}
#include
#include
#include
#include
using namespace std;
const int N = 1e4 + 10;
int dp[5][N];
int main(){
int w[4] = {0, 150, 200, 350};
int t,W;
scanf("%d", &t);
while(t--){
memset(dp, 0, sizeof(dp));
scanf("%d", &W);
for(int i = 1; i <= 3; i++){
for(int j = 0; j <= W ;j++){
if(j < w[i]){
dp[i][j] = dp[i - 1][j];
}
else dp[i][j] = max(dp[i - 1][j], dp[i][j - w[i]] + w[i]);
}
}
printf("%d\n", W - dp[3][W]);
}
}
int dp[MAX_K + 1];
void solve() {
memset(dp, -1, sizeof(dp));
dp[0] = 0;
for (int i = 0; i < n; i++){
for (int j = 0; j < W; j++){
if (dp[j] >= 0) dp[j] = m[i];
else if (j < a[i] || dp[j - a[i]] <= 0) dp[j] = -1;
else dp[j] = dp[j - a[i]] - 1;
}
}
if(dp[K] >= 0) puts("YES");
else puts("NO");
}
#include
#include
#include
#include
using namespace std;
const int N = 1e5 + 10;
const int INF = 0x3f3f3f3f;
int a[N], b[N];
int dp[N];
int main(){
int t;
scanf("%d", &t);
while (t--){
int n;
scanf("%d", &n);
memset(dp, INF, sizeof(dp));
for (int i = 1; i <= n; i++){
scanf("%d", &a[i]);
}
for (int i = 1; i <= n; i++){
*lower_bound(dp, dp + n, a[i]) = a[i];
b[i]=lower_bound(dp, dp + n, a[i]) - dp + 1;
}
for (int i = 1; i < n; i++){
printf("%d ", b[i]);
}
printf("%d\n", b[n]);
}
}
int f(int n,int idx){
if(n==0) return 1;
if(idx<=0) return 0;
return f(n-idx,idx)+f(n,idx-1);
}