传送原题
#include<iostream>
#include<stdio.h>
#include<algorithm>
#include<queue>
#include<math.h>
#include<string.h>
#include<sstream>
using namespace std;
int a[] = {150,200,350};
int dp[10005];
int main() {
ios::sync_with_stdio(false);
int t, money;
cin >> t;
while(t --){
memset(dp,0,sizeof(dp));
cin >> money;
for(int i = 0; i < 3; i ++)
for(int j = a[i]; j <= money; j ++)
dp[j] = max(dp[j], dp[j-a[i]] + a[i]);
cout << money - dp[money] << endl;
}
return 0;
}
- 然后我做了一个小优化,速度可能快了50倍,也可能没什么软用
#include<iostream>
#include<stdio.h>
#include<algorithm>
#include<queue>
#include<math.h>
#include<string.h>
#include<sstream>
using namespace std;
int a[] = {150,200,350};
int dp[10005];
int main() {
ios::sync_with_stdio(false);
int t, money;
cin >> t;
while(t --){
memset(dp,0,sizeof(dp));
int Max = 0;
cin >> money;
for(int i = 0; i < 3; i ++)
for(int j = a[i]; j <= money; j += 50){
dp[j] = max(dp[j], dp[j-a[i]] + a[i]);
Max = max(Max,dp[j]);
}
cout << money - Max << endl;
}
return 0;
}