HRBEU Max Use Of CPU(01背包)

Max Use Of CPU

TimeLimit: 1 Second   MemoryLimit: 32 Megabyte

Totalsubmit: 11   Accepted: 1  

Description

There are n tasks {1,2,3...,n} waiting in line to be operated on the computer. Task i needs Ti CPU time to be finished. Your task is to find a way to maximize the number of tasks can be finished in time T. If there are more than one way containing maximum tasks, then maximize the CPU time used in time T.

Input

The first line contains a integer t (1<=t<=100), the number of test cases. The first line of each case contains integer n and T, representing n tasks and in time T the maximum tasks can be finished and the maximum CPU can be used. The second line of each case contains the time {T1,T2,T3...Tn} each task needs to be finished. All numbers are integer in the input file.

Output

For each test case, output a line with the maximum number of tasks can be finished and the maximum of CPU time can be used in time T. One test case per line and separated by one whitespace.

Sample Input

1
7 47
30 78 33 76 66 8 78

Sample Output

2 41

 

 

#include<cstdio>
#include<cstring>
#include<algorithm>

using namespace std;

#define N 1000005

int f[N],num[N],v,maxans;

void pack01(int cost,int weight){
     int i;
     for(i=v;i>=cost;i--)
       if(f[i-cost]!=-1){
         if(f[i-cost]+1>f[i])
           f[i]=f[i-cost]+1;
           maxans=max(maxans,f[i]);
       }
}

int main(){
        int i,n,t,cost;
        scanf("%d",&t);
        while(t--){
          maxans=0;
          memset(f,-1,sizeof(f));
          f[0]=0;
          scanf("%d%d",&n,&v);
          for(i=1;i<=n;i++){
            scanf("%d",&cost);
            pack01(cost,cost);
          }
          for(i=v;i>=0;i--)
            if(f[i]==maxans){
              printf("%d %d\n",maxans,i);
              break;
            }
        }
        return 0;
}


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值