zoukankan      html  css  js  c++  java
  • hdu 5562 Clarke and food(贪心)

    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 ith 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(1≤T≤10), the number of the test cases.
    For each test case: 
    The first line contains two integers n,V(1≤n≤105,1≤V≤109). 
    The second line contains n integers, the ith integer denotes vi(1≤vi≤109).
    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
     
    贪心,将值从小到大排序后,一直选就可以了。
     
     1 #pragma comment(linker, "/STACK:1024000000,1024000000")
     2 #include<iostream>
     3 #include<cstdio>
     4 #include<cstring>
     5 #include<cmath>
     6 #include<math.h>
     7 #include<algorithm>
     8 #include<queue>
     9 #include<set>
    10 #include<bitset>
    11 #include<map>
    12 #include<vector>
    13 #include<stdlib.h>
    14 #include <stack>
    15 using namespace std;
    16 #define PI acos(-1.0)
    17 #define max(a,b) (a) > (b) ? (a) : (b)
    18 #define min(a,b) (a) < (b) ? (a) : (b)
    19 #define ll long long
    20 #define eps 1e-10
    21 #define MOD 1000000007
    22 #define N 100006
    23 #define inf 1e12
    24 int n,v;
    25 int a[N];
    26 int dp[N];
    27 int dp_num[N];
    28 int main()
    29 {
    30    int t;
    31    scanf("%d",&t);
    32    while(t--){
    33       scanf("%d%d",&n,&v);
    34       for(int i=0;i<n;i++){
    35          scanf("%d",&a[i]);
    36       }
    37       sort(a,a+n);
    38       int ans=0;
    39       int index=0;
    40       while(v>0){
    41          if(v>=a[index]){
    42             v-=a[index];
    43             index++;
    44             ans++;
    45          }else{
    46             break;
    47          }
    48 
    49       }
    50       printf("%d
    ",ans);
    51    }
    52     return 0;
    53 }
    View Code
  • 相关阅读:
    BZOJ 1718: [Usaco2006 Jan] Redundant Paths 分离的路径( tarjan )
    BZOJ 1040: [ZJOI2008]骑士( 树形dp )
    BZOJ 1691: [Usaco2007 Dec]挑剔的美食家( 平衡树 )
    HDU 5667 Sequence 矩阵快速幂
    FZU 2225 小茗的魔法阵 扫描线+树状数组
    UVA 11916 Emoogle Grid 离散对数 大步小步算法
    UVA 11754 Code Feat 中国剩余定理+暴力
    FZU 2092 收集水晶 dp+bfs
    FZU2090 旅行社的烦恼 巧妙floyd 最短路
    UVA 11426 GCD
  • 原文地址:https://www.cnblogs.com/UniqueColor/p/4972013.html
Copyright © 2011-2022 走看看