HDU 5562 Clarke and food(排序)——BestCoder Round #62(div.2)

Clarke and food

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)


Problem Description
Clarke is a patient with multiple personality disorder. One day, Clarke turned into a cook, was shopping for food. 
Clarke has bought  n  food. The volume of the  i th food is  vi . Now Clarke has a pack with volume  V . He wants to carry food as much as possible. Tell him the maxmium number he can brought with this pack.
 

Input
The first line contains an integer  T(1T10) , the number of the test cases.
For each test case: 
The first line contains two integers  n,V(1n105,1V109)
The second line contains  n  integers, the  i th integer denotes  vi(1vi109) .
 

Output
For each test case, print a line with an integer which denotes the answer.
 

Sample Input
   
   
   
   
1 3 5 1 3 4
 

Sample Output
   
   
   
   
2 Hint: We can carry 1 and 3, the total volume of them is 5.
 

Source
BestCoder Round #62 (div.2)
 

/************************************************************************/

附上该题对应的中文题

Clarke and food

 
 
 Time Limit: 2000/1000 MS (Java/Others)
 
 Memory Limit: 65536/65536 K (Java/Others)
问题描述
克拉克是一名人格分裂患者。某一天,克拉克变成了一个厨师,在采购食品。  
克拉克一共采购了nn个食物,每个食物有一个体积v_ivi。现在克拉克有一个容量为VV背包,他想用这个背包来装尽量多的食物。请你告诉他最多能装多少食物。  
输入描述
第一行一个整数T(1 \le T \le 10)T(1T10),表示数据的组数。  
每组数据第一行是两个整数n, V(1 \le n \le 10^5, 1 \le V \le 10^9)n,V(1n105,1V109),表示克拉克的数目和背包容量。  
接下来一行有nn个正数,第ii个整数表示v_i(1 \le v_i \le 10^9)vi(1vi109)
输出描述
每组数据输出一行一个数,表示答案。
输入样例
1
3 5
1 3 4
输出样例
2
Hint
我们可以放第1个和第3个,这样总容量为5。
/****************************************************/

出题人的解题思路:

Clarke and food

可以证明从小到大一直拿,拿到不能拿为止是最优的。

所以排序一下即可。

说实话,第一眼看到的时候觉得是01背包,然而,V的范围果断让我否定了这种想法
好吧,其实这题没有想象的那么难,要装尽可能多的食物,当然先装体积小的咯,所以我们先对食物的体积排序,然后依次放进背包就可以了
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<queue>
#include<stack>
#include<math.h>
#include<vector>
#include<map>
#include<set>
#include<cmath>
#include<string>
#include<algorithm>
#include<iostream>
#define exp 1e-10
using namespace std;
const int N = 100005;
const int M = 100005;
const int inf = 100000000;
const int mod = 2009;
int s[N];
int main()
{
    int t,n,V,i,k;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d%d",&n,&V);
        for(k=i=0;i<n;i++)
            scanf("%d",&s[i]);
        sort(s,s+n);
        for(i=0;i<n;i++)
            if(V-s[i]>=0)
            {
                k++;
                V-=s[i];
            }
            else
                break;
        printf("%d\n",k);
    }
    return 0;
}
菜鸟成长记

你可能感兴趣的:(排序,ACM)