码迷,mamicode.com
首页 > 其他好文 > 详细

HDUOJ 2546 饭卡

时间:2014-08-31 21:19:11      阅读:178      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   color   os   io   java   ar   for   

饭卡

Time Limit: 1000ms
Memory Limit: 32768KB
This problem will be judged on HDU. Original ID: 2546
64-bit integer IO format: %I64d      Java class name: Main
 
电子科大本部食堂的饭卡有一种很诡异的设计,即在购买之前判断余额。如果购买一个商品之前,卡上的剩余金额大于或等于5元,就一定可以购买成功(即使购买后卡上余额为负),否则无法购买(即使金额足够)。所以大家都希望尽量使卡上的余额最少。
某天,食堂中有n种菜出售,每种菜可购买一次。已知每种菜的价格以及卡上的余额,问最少可使卡上的余额为多少。
 

Input

多组数据。对于每组数据:
第一行为正整数n,表示菜的数量。n<=1000。
第二行包括n个正整数,表示每种菜的价格。价格不超过50。
第三行包括一个正整数m,表示卡上的余额。m<=1000。

n=0表示数据结束。
 

Output

对于每组输入,输出一行,包含一个整数,表示卡上可能的最小余额。
 

Sample Input

1
50
5
10
1 2 3 2 1 1 2 3 2 1
50
0

Sample Output

-45
32

Source

 
解题:比较好玩的题目。先减除5元 用来买最贵的食物。然后用剩余的钱,进行01背包,取能买价值最多的食物价值。总钱-最大价值-最贵食物价格。
 
 
bubuko.com,布布扣
 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <cmath>
 5 #include <algorithm>
 6 #include <climits>
 7 #include <vector>
 8 #include <queue>
 9 #include <cstdlib>
10 #include <string>
11 #include <set>
12 #include <stack>
13 #define LL long long
14 #define pii pair<int,int>
15 #define INF 0x3f3f3f3f
16 using namespace std;
17 int n,m,p[1010],dp[1010];
18 int main() {
19     while(scanf("%d",&n),n) {
20         memset(dp,0,sizeof(dp));
21         for(int i = 1; i <= n; i++) scanf("%d",p+i);
22         scanf("%d",&m);
23         sort(p+1,p+n+1);
24         dp[0] = 0;
25         if(m < 5) {printf("%d\n",m);continue;}
26         for(int i = 1; i < n; i++){
27             for(int j = m-5; j >= p[i]; j--)
28                 dp[j] = max(dp[j],dp[j-p[i]] + p[i]);
29         }
30         printf("%d\n",m-dp[m-5]-p[n]);
31     }
32     return 0;
33 }
View Code

 

HDUOJ 2546 饭卡

标签:style   blog   http   color   os   io   java   ar   for   

原文地址:http://www.cnblogs.com/crackpotisback/p/3948145.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!