HRBEU Max Use Of CPU(01背包)

来源:互联网 发布:淘宝店铺资质代表什么 编辑:程序博客网 时间:2024/05/16 07:05

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;
}


 

原创粉丝点击