zojDakar Rally(01背包)
01背包 加上每次更新解题数目最多 总用时最少 因为要保证用时最少,要先把时长由小到大排序。
没排序 WA了几小时。。链接
1 #include <iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<algorithm> 5 #include<stdlib.h> 6 #include<vector> 7 #include<cmath> 8 #include<queue> 9 #include<set> 10 #include<map> 11 using namespace std; 12 #define N 1010 13 #define LL long long 14 #define INF 0xfffffff 15 const double eps = 1e-8; 16 const double pi = acos(-1.0); 17 const double inf = ~0u>>2; 18 int dp[N*50][5]; 19 struct node 20 { 21 int ti,v; 22 }p[N]; 23 bool cmp(node x,node y) 24 { 25 return x.ti<y.ti; 26 } 27 int main() 28 { 29 int i,j,t,n,nn; 30 cin>>nn; 31 while(nn--) 32 { 33 memset(dp,0,sizeof(dp)); 34 cin>>t>>n; 35 for(i = 1; i <= n; i++) 36 cin>>p[i].ti; 37 for(i = 1; i <= n; i++) 38 cin>>p[i].v; 39 sort(p+1,p+n+1,cmp); 40 for(i = 1 ; i <= n ; i++) 41 { 42 for(j = t ; j >= p[i].ti ; j--) 43 { 44 if(dp[j-p[i].ti][0]+p[i].v>dp[j][0]) 45 { 46 dp[j][0] = dp[j-p[i].ti][0]+p[i].v; 47 dp[j][1] = dp[j-p[i].ti][1]+1; 48 dp[j][2] = dp[j-p[i].ti][2]+j; 49 } 50 else if(dp[j-p[i].ti][0]+p[i].v==dp[j][0]&&dp[j-p[i].ti][1]+1>=dp[j][1]) 51 { 52 if(dp[j-p[i].ti][1]+1>dp[j][1]||dp[j][2]>dp[j-p[i].ti][2]+j) 53 { 54 dp[j][0] = dp[j-p[i].ti][0]+p[i].v; 55 dp[j][1] = dp[j-p[i].ti][1]+1; 56 dp[j][2] = dp[j-p[i].ti][2]+j; 57 } 58 } 59 } 60 } 61 int ans = 0,x=0,y=0; 62 for(i = t ; i >=0 ;i--) 63 { 64 if(dp[i][0]>=ans) 65 { 66 if(dp[i][0]==ans&&dp[i][1]>=x) 67 { 68 if(dp[i][1]>x||dp[i][2]<y) 69 { 70 ans = dp[i][0]; 71 x = dp[i][1]; 72 y = dp[i][2]; 73 } 74 } 75 if(dp[i][0]>ans) 76 { 77 ans = dp[i][0]; 78 x = dp[i][1]; 79 y = dp[i][2]; 80 } 81 } 82 } 83 cout<<ans<<" "<<x<<" "<<y<<endl; 84 } 85 return 0; 86 }