首页 技术 正文
技术 2022年11月16日
0 收藏 878 点赞 2,975 浏览 3146 个字

Happy Programming Contest


Time Limit: 2 Seconds      Memory Limit: 65536 KB


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

题目的意思是给出总时间和题目的数量,每道题有花费时间和价值,求价值最大是多少,价值最大不唯一时,按题目数量多的排,题目数量也一样按罚时少的排

思路:01背包,加上相等时判定的更新即可

#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
#include <vector>
#include <set>
#include <stack>
#include <map>
#include <cctype>
#include <sstream>
#include <climits>using namespace std;#define LL long long
const int INF=0x3f3f3f3f;
int mod=1e9+7;struct node{
int val,num,t,sum;
}dp[1005];struct pro{
int w,v;
}p[100];bool cmp(pro a,pro b)
{
if(a.w!=b.w)
return a.w<b.w;
return a.v>b.v;
}int main()
{
int T,m,n;
scanf("%d",&T);
while(T--)
{
scanf("%d%d",&m,&n);
for(int i=0;i<n;i++)
scanf("%d",&p[i].w);
for(int i=0;i<n;i++)
scanf("%d",&p[i].v); memset(dp,0,sizeof dp);
sort(p,p+n,cmp);
for(int i=0;i<n;i++)
{
for(int j=m;j>=p[i].w;j--)
{
if(dp[j].val<dp[j-p[i].w].val+p[i].v)
{
dp[j].val=dp[j-p[i].w].val+p[i].v;
dp[j].num=dp[j-p[i].w].num+1;
dp[j].t=dp[j-p[i].w].t+p[i].w;
dp[j].sum=dp[j-p[i].w].sum+dp[j].t;
}
else if(dp[j].val==dp[j-p[i].w].val+p[i].v)
{
if(dp[j].num<dp[j-p[i].w].num+1)
{
dp[j].num=dp[j-p[i].w].num+1;
dp[j].t=dp[j-p[i].w].t+p[i].w;
dp[j].sum=dp[j-p[i].w].sum+dp[j].t;
}
else if(dp[j].num==dp[j-p[i].w].num+1)
{
if(dp[j].sum>dp[j-p[i].w].sum+dp[j-p[i].w].t+p[i].w)
{
dp[j].t=dp[j-p[i].w].t+p[i].w;
dp[j].sum=dp[j-p[i].w].sum+dp[j].t;
}
}
}
}
}
printf("%d %d %d\n",dp[m].val,dp[m].num,dp[m].sum);
}
return 0;
}

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,491
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,907
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,740
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,493
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:8,132
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,294