zoukankan      html  css  js  c++  java
  • HDU

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2546

    分析:

    如果 m 小于5的话,直接输出m;
    否则的话,先对菜的价值排序,求出在前n-1种菜中能得到m-5的最大价值, 再减去价值最大的菜的价值
     
    Sample Input
    1
    50
    5
    10
    1 2 3 2 1 1 2 3 2 1
    50
    0
    Sample Output
    -45
    32

    ***********************************************

    AC代码 :

     1 #include<stdio.h>
     2 #include<stdlib.h>
     3 #include<string.h>
     4 #include<queue>
     5 #include<algorithm>
     6 #include<cmath>
     7 #include<iostream>
     8 
     9 using namespace std;
    10 typedef long long LL;
    11 
    12 #define INF 0x3f3f3f3f
    13 #define N 22000
    14 #define MAXN 100000000
    15 #define mod 1000000007
    16 
    17 int dp[N], a[N];
    18 
    19 int main()
    20 {
    21     int n,m,i,j,ans;
    22 
    23     while(scanf("%d", &n),n)
    24     {
    25         memset(dp,0,sizeof(dp));
    26         memset(a,0,sizeof(a));
    27 
    28         for(i=0;i<n;i++)
    29             scanf("%d",&a[i]);
    30         scanf("%d", &m);
    31 
    32         sort(a,a+n);
    33 
    34         for(i=0;i<n-1;i++)
    35             for(j=m-5;j>=a[i];j--)
    36             dp[j]=max(dp[j],dp[j-a[i]]+a[i]);
    37 
    38         if(m>=5)
    39         ans=m-(dp[m-5]+a[n-1]);
    40         else
    41             ans=m;
    42 
    43         printf("%d
    ", ans);
    44     }
    45     return 0;
    46 }
  • 相关阅读:
    2021年2月13
    2021年2月12
    2021年2月11
    2021年2月10
    2021年2月9
    下载优化
    20180301越努力越轻松
    2018-03-01继续完善2
    2018-03-01继续完善
    2018-02-16 GetSameTypeQuestion
  • 原文地址:https://www.cnblogs.com/weiyuan/p/5743462.html
Copyright © 2011-2022 走看看