Skip to main content
  1. Posts/

hdu 2546 饭卡 (01背包)

·2 mins
Note: This article is available in Chinese only. 本文暂无英文版本。 View original

饭卡
#

**Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 14225 Accepted Submission(s): 4945 **

Problem Description

电子科大本部食堂的饭卡有一种很诡异的设计,即在购买之前判断余额。如果购买一个商品之前,卡上的剩余金额大于或等于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

UESTC 6th Programming Contest Online

在入门dp。

显然是01背包。 cost和value都是价钱。

但是有限制条件。

很容易想到,为了使得余额最少,我们要把最贵的留在最后买。

读入的时候把最贵的菜价拿出来

然后剩下的n-1个菜,做一个容量为m-5的01背包。

注意如果m<5,直接输出即可。

 1
 2
 3   /* ***********************************************
 4   Author :111qqz
 5   Created Time :2016年02月22日 星期一 22时44分27秒
 6   File Name :code/hdu/2546.cpp
 7   ************************************************ */
 8
 9   #include <iostream>
10   #include <algorithm>
11   #include <cstring>
12   #include <cstdio>
13   #include <cmath>
14
15   using namespace std;
16
17   const int  N=2E3+5;
18   int mmax;
19   int a[N],dp[N],posi;
20   int n,m;
21
22   void ZeroOnePack(int value ,int cost)
23   {
24        for ( int i = m - 5 ; i >= cost ; i-- )
25           dp[i]=max(dp[i],dp[i-cost]+value);
26
27   }
28
29   int main()
30   {
31       while (scanf("%d",&n)!=EOF&&n)
32       {   mmax = -1;
33          memset(dp,0,sizeof(dp));
34          memset(a,0,sizeof(a));
35           for ( int i = 1 ; i <= n ; i++)
36               scanf("%d",&a[i]);
37           for ( int i = 1 ; i <= n ; i++)
38               if ( a[i] > mmax )
39               {
40                   mmax=a[i];
41                   posi=i;
42               }
43           scanf("%d",&m);
44           if ( m< 5) {printf("%d\n",m);continue;}
45
46
47           for ( int i = 1 ; i <= n ; i++ )
48               if ( i!=posi )
49               ZeroOnePack(a[i],a[i]);
50               printf("%d\n",m-dp[m-5]-mmax);
51       }
52       return 0;
53   }

Related

hdu 5119 - Happy Matt Friends(dp解法)

·2 mins
Description Matt has N friends. They are playing a game together. Each of Matt’s friends has a magic number. In the game, Matt selects some (could be zero) of his friends. If the xor (exclusive-or) sum of the selected friends’magic numbers is no less than M , Matt wins.

HDOJ 4882 Loves Codefires

·2 mins
ZCC Loves Codefires Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 988 Accepted Submission(s): 500 Problem Description Though ZCC has many Fans, ZCC himself is a crazy Fan of a coder, called “Memset137”. It was on Codefires(CF), an online competitive programming site, that ZCC knew Memset137, and immediately became his fan. But why? Because Memset137 can solve all problem in rounds, without unsuccessful submissions; his estimation of time to solve certain problem is so accurate, that he can surely get an Accepted the second he has predicted. He soon became IGM, the best title of Codefires. Besides, he is famous for his coding speed and the achievement in the field of Data Structures. After become IGM, Memset137 has a new goal: He wants his score in CF rounds to be as large as possible. What is score? In Codefires, every problem has 2 attributes, let’s call them Ki and Bi(Ki, Bi>0). if Memset137 solves the problem at Ti-th second, he gained Bi-KiTi score. It’s guaranteed Bi-KiTi is always positive during the round time. Now that Memset137 can solve every problem, in this problem, Bi is of no concern. Please write a program to calculate the minimal score he will lose.(that is, the sum of Ki*Ti).

poj 1065 Wooden Sticks

·2 mins
Wooden Sticks Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 19008 Accepted: 8012 Description There is a pile of n wooden sticks. The length and weight of each stick are known in advance. The sticks are to be processed by a woodworking machine in one by one fashion. It needs some time, called setup time, for the machine to prepare processing a stick. The setup times are associated with cleaning operations and changing tools and shapes in the machine. The setup times of the woodworking machine are given as follows: (a) The setup time for the first wooden stick is 1 minute. (b) Right after processing a stick of length l and weight w , the machine will need no setup time for a stick of length l’ and weight w’ if l <= l’ and w <= w’. Otherwise, it will need 1 minute for setup. You are to find the minimum setup time to process a given pile of n wooden sticks. For example, if you have five sticks whose pairs of length and weight are ( 9 , 4 ) , ( 2 , 5 ) , ( 1 , 2 ) , ( 5 , 3 ) , and ( 4 , 1 ) , then the minimum setup time should be 2 minutes since there is a sequence of pairs ( 4 , 1 ) , ( 5 , 3 ) , ( 9 , 4 ) , ( 1 , 2 ) , ( 2 , 5 ) . Input

codeforces 447 B. DZY Loves Strings

·1 min
简单贪心。 因为填的字母没有次数限制,所以最优策略很容易想到,就是在最后面填最大的。 不用实际去填,算出ans就可以。

hdu 1009 FatMouse' Trade

·1 min
简单贪心…. 需要注意的是数据是非负,所以有0的情况要考虑周全,基本都要特殊处理。 多WA了三次,不知道为什么交C++可以过,交G++就不行。