简单的背包问题, 算是一种多重的吧。

解题的关键在于,要控制最后所用的时间最少,所以在程序的最开始应该先将 输入的各种题目 以时间升序排列, 然后就可以保证每次都以时间小的优先选, 这样就可以保证最后相同的吸引值和解题数的情况下所话的时间最少。

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

Author: HUANG, Qiao
Contest: The 13th Zhejiang University Programming Contest

#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <iostream>
using namespace std;struct node
{int key,time,cnt;int ty;
}dp[1100][1100];struct node1
{int w,key;
}g[1100];int t,n;int cmp(node1 t,node1 t1)
{return t.w < t1.w;
}int main()
{int T;scanf("%d",&T);while(T--){scanf("%d%d",&t,&n);for(int i=1;i<=n;i++)scanf("%d",&g[i].w);for(int i=1;i<=n;i++)scanf("%d",&g[i].key);sort(g+1,g+1+n,cmp);memset(dp,0,sizeof(dp));for(int i=1;i<=n;i++){for(int j=0;j<=t;j++){dp[i][j] = dp[i-1][j];//dp[i][j].ty=j;if(j<g[i].w) continue;int flag=0;int tkey,tcnt,time;tkey = dp[i-1][ j-g[i].w ].key + g[i].key;tcnt = dp[i-1][ j-g[i].w ].cnt+1;if( tkey < dp[i][j].key) continue;if( tkey>dp[i][j].key ){dp[i][j].cnt=tcnt;dp[i][j].key=tkey;dp[i][j].ty=j-g[i].w;dp[i][j].time=dp[i-1][j-g[i].w].time+j;continue;} // 如果解题数都相同的话if(tcnt < dp[i][j].cnt) continue;if(tcnt>dp[i][j].cnt){dp[i][j].cnt=tcnt;dp[i][j].ty=j-g[i].w;dp[i][j].time=dp[i-1][j-g[i].w].time+j;continue;}}}int mx=0,mxcnt=0,mxi=0;for(int i=0;i<=t;i++){if(dp[n][i].key<mx) continue;if(dp[n][i].key > mx) {mx=dp[n][i].key;mxcnt=dp[n][i].cnt;mxi=dp[n][i].time;continue;}if(dp[n][i].cnt<mxcnt) continue;if(dp[n][i].cnt>mxcnt) {mxcnt=dp[n][i].cnt;mxi=dp[n][i].time;continue;}if(dp[n][i].time<mxi) mxi=dp[n][i].time;}/*int time=0;int tg[1100];for(int i=1;i<=t;i++){if(dp[n][i].key==mx&&dp[n][i].cnt==mxcnt){int tx=n,ty=i,tc=dp[n][i].cnt;while(1){if(dp[tx-1][ dp[tx][ty].ty ].cnt!=tc){tg[tc]=ty-dp[tx][ty].ty;tc--;ty = dp[tx][ty].ty;}if(tc==0) break;tx--;}sort(tg+1,tg+mxcnt+1);time=0;int tt=0;for(int j=1;j<=mxcnt;j++){time+=tt+tg[j];tt+=tg[j];}if(time<mxi) mxi=time;}}*/printf("%d %d %d\n",mx,mxcnt,mxi);}return 0;
}

转载于:https://www.cnblogs.com/chenhuan001/archive/2013/04/22/3035091.html

zoj 3703(背包)相关推荐

  1. ZOJ 3703 Happy Programming Contest 0-1背包 DP

    ZOJ 3703 Happy Programming Contest 题目描述: 题目链接:ZOJ 3703 Happy Programming Contest 题目大意: 这是一道虐狗的题目.讲的是 ...

  2. *【ZOJ - 3703】Happy Programming Contest(带优先级的01背包)

    题干: In Zhejiang University Programming Contest, a team is called "couple team" if it consi ...

  3. 【ZOJ - 3703】Happy Programming Contest(带优先级的01背包,贪心背包)

    题干: In Zhejiang University Programming Contest, a team is called "couple team" if it consi ...

  4. ZOJ 3703,3700,3699

    经典背包问题,处理好限制条件的优先级 排序是为了让相同val,相同问题数量以及选择的时候,从ti更小的问题坐起,总的penalty会最少 #include <iostream> #incl ...

  5. ZOJ - 2955 Interesting Dart Game(鸽巢原理+完全背包)

    题目链接:点击查看 题目大意:给定M个不同的得分方式,以及需要得到N分,至少需要多少次(多余N都不行,只能严格等于N) 思路:第一反应肯定是完全背包,可是N给到了1e9的程度,开数组肯定是开不了的,这 ...

  6. 【ZOJ - 2955】Interesting Dart Game(背包,结论,裴蜀定理,数论)

    题干: Recently, Dearboy buys a dart for his dormitory, but neither Dearboy nor his roommate knows how ...

  7. 【ZOJ - 3211】Dream City (01背包类问题,贪心背包)

    题干: JAVAMAN is visiting Dream City and he sees a yard of gold coin trees. There are n trees in the y ...

  8. 【ZOJ - 4019】Schrödinger's Knapsack (dp,背包,贪心,组内贪心组间dp)

    题干: 有两种物品,k分别为k1,k2,有大小各不一的这两种物品若干,放入容量为c的背包中,能获得求最大的值.放的顺序会影响结果.每次放入一物品,其获得的值都可以用v=kr计算,r表示放入后背包剩下的 ...

  9. ZOJ 3450 Doraemon's Railgun (DP·分组背包)

    题意  多啦A梦有一个超电磁炮  然后要打死n堆敌人  在同一条射线上的敌人只有先打死前面的一堆才能打后面的一堆  给你打死某堆敌人需要的时间和这堆敌人的人数   问你在T0时间内最多打死多少个敌人 ...

最新文章

  1. Shine Button动画效果 类似Tinder APP的卡片界面
  2. Android Manager
  3. 大数据平台常用组件_这款大数据智能服务平台火了!全自动化配置30+款开源大数据组件...
  4. 20165101刘天野 2017-2018-2 《Java程序设计》 结对编程练习_四则运算(第二周)
  5. mysql三大范式 答案_数据库逻辑设计之三大范式通俗理解,一看就懂,书上说的太晦涩...
  6. Jmeter基础之JMeter参数化补充练习
  7. SolarWinds 升级 APM Suite,简化应用程序和基础架构管理!
  8. 批处理mysql命令
  9. 简要说明php数组的类型,php数组的概述及分类与声明代码演示
  10. Photoshop怎么实现图片局部马赛克
  11. 查看mysql服务器位置,查看mysql服务器ip地址
  12. 录屏软件推荐:bilibili哔哩哔哩直播姬录屏软件下载使用指南
  13. Easyx-----c语言实现烟花表白程序
  14. 学术文献也有身份证?
  15. 【sphinx】中文声学模型训练
  16. java淡蓝色怎么表示_最淡的蓝是什么颜色(淡蓝色配什么颜色好看)
  17. 安卓仿Toasty消息弹框
  18. CD7388CZ功放IC,4x41W汽车音响功率放大电路,车机标配IC
  19. 前端将时间格式‘2020-03-03T16:49:18.000+0000‘转化成正常格式‘2020-03-03 16:49:18‘ _@jie
  20. 天龙八部搭建mysql教程_天龙八部私服架设mysql数据库安装

热门文章

  1. MariaDB设置root用户密码
  2. java 封闭类型_Java并发之线程封闭
  3. DelphiMVC连接池配置
  4. web 前端常用组件【02】Select 下拉框
  5. Android设置无title报错
  6. 【oneday_onepage】—— 日常用语
  7. 星际争霸战略战术的发展和创新
  8. 05 | 服务编排层:Pipeline 如何协调各类 Handler ?
  9. 2020年,RocketMQ面试题 -面试题驱动RocketMQ学习
  10. 【实用工具】之CSDN表格模板