Description
In Zhejiang University Programming Contest, a team is called “couple team” if it consists of only two students loving each other. In the contest, the team will get a lovely balloon with unique color for each problem they solved. Since the girl would prefer pink balloon rather than black balloon, each color is assigned a value to measure its attractiveness. Usually, the boy is good at programming while the girl is charming. The boy wishes to solve problems as many as possible. However, the girl cares more about the lovely balloons. Of course, the boy’s primary goal is to make the girl happy rather than win a prize in the contest.
Suppose for each problem, the boy already knows how much time he needs to solve it. Please help him make a plan to solve these problems in strategic order so that he can maximize the total attractiveness value of balloons they get before the contest ends. Under this condition, he wants to solve problems as many as possible. If there are many ways to achieve this goal, he needs to minimize the total penalty time. The penalty time of a problem is equal to the submission time of the correct solution. We assume that the boy is so clever that he always submit the correct solution.
Input
The first line of input is an integer N (N < 50) indicating the number of test cases. For each case, first there is a line containing 2 integers T (T <= 1000) and n (n <= 50) indicating the contest length and the number of problems. The next line contains n integers and the i-th integer ti (ti <= 1000) represents the time needed to solve the ith problem. Finally, there is another line containing n integers and the i-th integer vi (vi <= 1000) represents the attractiveness value of the i-th problem. Time is measured in minutes.
Output
For each case, output a single line containing 3 integers in this order: the total attractiveness value, the number of problems solved, the total penalty time. The 3 integers should be separated by a space.
Sample Input
2
300 10
10 10 10 10 10 10 10 10 10 10
1 2 3 4 5 6 7 8 9 10
300 10
301 301 301 301 301 301 301 301 301 301
1000 1000 1000 1000 1000 1000 1000 1000 1000 1000
Sample Output
55 10 550
0 0 0
(1)题意:有个对要去比赛。带了个女孩。每个题都有吸引女孩的值。给你总时间,一些题,每个题给你所花时间,吸引力值。然后问你在总时间内,选取一种A题方案,使获得的吸引力值最大,如果有多种情况,则选择出题数较多的,如果也有多种情况,选择罚时较少的。(不在乎是否能赢)
(2)解法:背包问题。本题重点就是后面的几种比较关系;如果单看第一个条件,就是裸的01背包,但是再处理剩下的2个条件,也就是在状态转移中加入一些条件:f[v]=max{f[v],f[v-c[i]]+w[i]}。使得条件满足的情况下再进行状态的转移。先满足吸引力的情况,再考虑解题数量,最后考虑罚时问题。这样单一的背包f[v]=max{f[v],f[v-c[i]]+w[i]}变为多元条件。
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<iostream>
#include<queue>
using namespace std;
int main()
{
int k;
scanf("%d",&k);
while(k--)
{
int sumtime,n;
int time[55],like[55];
int dp[1005]={0};
int zs[1005]={0};
int jie[1005]={0};
int fuck[1005]={0};
int i,j;
scanf("%d %d",&sumtime,&n);
for(i=0;i<n;i++) scanf("%d",&time[i]);
for(i=0;i<n;i++) scanf("%d",&like[i]);
for(i=0;i<n;i++) //冒泡 。按时间从小到大排。写到最后了。不想写成结构体了T_T
{
for(j=i;j<n;j++)
{
if(time[j]<time[i])
{
int t;
t=time[i];
time[i]=time[j];
time[j]=t;
t=like[i];
like[i]=like[j];
like[j]=t;
}
}
}
for(i=0;i<n;i++)
{
for(j=sumtime;j>=time[i];j--)
{
if(dp[j]<dp[j-time[i]]+like[i]) //最先满足like最大
{
dp[j]=dp[j-time[i]]+like[i];
jie[j]=jie[j-time[i]]+1;
zs[j]=zs[j-time[i]]+time[i];
fuck[j]=fuck[j-time[i]]+zs[j-time[i]]+time[i];
}
else if(dp[j]==dp[j-time[i]]+like[i])
{
if(jie[j]<jie[j-time[i]]+1)//然后满足解决问题最多
{
dp[j]=dp[j-time[i]]+like[i];
zs[j]=zs[j-time[i]]+time[i];
fuck[j]=fuck[j-time[i]]+zs[j-time[i]]+time[i];
jie[j]=jie[j-time[i]]+1;
}
else if(jie[j]==jie[j-time[i]]+1)
{
if(fuck[j]>fuck[j-time[i]]+zs[j-time[i]]+time[i])//最后满足罚时最少
{
jie[j]=jie[j-time[i]]+1;
zs[j]=zs[j-time[i]]+time[i];
fuck[j]=fuck[j-time[i]]+zs[j-time[i]]+time[i];
dp[j]=dp[j-time[i]]+like[i];
}
}
}
}
}
int outlove=0,outjie=0,outsumtime=0;
for(i=0;i<=sumtime;i++) //找出最大的
{
if(dp[i]>outlove)
{
outlove=dp[i];
outjie=jie[i];
outsumtime=fuck[i];
}
else if(dp[i]==outlove)
{
if(jie[i]>outjie)
{
outlove=dp[i];
outjie=jie[i];
outsumtime=fuck[i];
}
else if(jie[i]==outjie)
{
if(fuck[i]>outsumtime)
{
outlove=dp[i];
outjie=jie[i];
outsumtime=fuck[i];
}
}
}
}
printf("%d %d %d\n",outlove,outjie,outsumtime);
}
return 0;
}
版权声明:本文为博主原创文章,未经博主允许不得转载。