poj piggy-bank

Piggy-Bank

Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 7921   Accepted: 3829

Description

Before ACM can do anything, a budget must be prepared and the necessary financial support obtained. The main income for this action comes from Irreversibly Bound Money (IBM). The idea behind is simple. Whenever some ACM member has any small money, he takes all the coins and throws them into a piggy-bank. You know that this process is irreversible, the coins cannot be removed without breaking the pig. After a sufficiently long time, there should be enough cash in the piggy-bank to pay everything that needs to be paid.

But there is a big problem with piggy-banks. It is not possible to
determine how much money is inside. So we might break the pig into
pieces only to find out that there is not enough money. Clearly, we want
to avoid this unpleasant situation. The only possibility is to weigh
the piggy-bank and try to guess how many coins are inside. Assume that
we are able to determine the weight of the pig exactly and that we know
the weights of all coins of a given currency. Then there is some minimum
amount of money in the piggy-bank that we can guarantee. Your task is
to find out this worst case and determine the minimum amount of cash
inside the piggy-bank. We need your help. No more prematurely broken
pigs!

Input

The
input consists of T test cases. The number of them (T) is given on the
first line of the input file. Each test case begins with a line
containing two integers E and F. They indicate the weight of an empty
pig and of the pig filled with coins. Both weights are given in grams.
No pig will weigh more than 10 kg, that means 1 <= E <= F <=
10000. On the second line of each test case, there is an integer number N
(1 <= N <= 500) that gives the number of various coins used in
the given currency. Following this are exactly N lines, each specifying
one coin type. These lines contain two integers each, Pand W (1 <= P
<= 50000, 1 <= W <=10000). P is the value of the coin in
monetary units, W is it‘s weight in grams.

Output

Print
exactly one line of output for each test case. The line must contain
the sentence "The minimum amount of money in the piggy-bank is X." where
X is the minimum amount of money that can be achieved using coins with
the given total weight. If the weight cannot be reached exactly, print a
line "This is impossible.".

Sample Input

3
10 110
2
1 1
30 50
10 110
2
1 1
50 30
1 6
2
10 3
20 4

Sample Output

The minimum amount of money in the piggy-bank is 60.
The minimum amount of money in the piggy-bank is 100.
This is impossible.

完全背包的问题,但要把背包装满!!!
#include <stdio.h>
#include <string.h>

int ff[10002];
int p[10002];
int w[10002];

int min(int a, int b)
{
	if(a>b)
		return b;
	else
		return a;
}

int main()
{
	int t;
	scanf("%d", &t );

	int e, f, dd ;
	int i, j, k, n;
	while(t--)
	{
		memset(w,0, sizeof(w));
		memset(p, 0, sizeof(p));

		scanf("%d %d", &e, &f );
		dd = f - e;
		scanf("%d", &n );

		for(i=1; i<=n; i++)
		{
			scanf("%d %d", &p[i], &w[i] );
		}

		ff[0]=0;

		for(j=1; j<10002; j++)
		{
			ff[j] = 99999999;
		}
		for(i=1; i<=n; i++)
		{
			for(k=w[i]; k<=dd; k++)
			{
				ff[k] = min(ff[k], ff[k-w[i]]+p[i] );
			}
		}
		if(ff[dd]==99999999 )
		{
			printf("This is impossible.\n");
		}
		else
		{
			printf("The minimum amount of money in the piggy-bank is %d.\n", ff[dd] );
		}
	}

	return 0;
}

poj piggy-bank

时间: 2024-12-20 03:13:04

poj piggy-bank的相关文章

ACM Piggy Bank

Problem Description Before ACM can do anything, a budget must be prepared and the necessary financial support obtained. The main income for this action comes from Irreversibly Bound Money (IBM). The idea behind is simple. Whenever some ACM member has

寒假训练5解题报告

1.HDU 1114 Piggy Bank 一道简单的背包问题 #include <iostream> #include <cstdio> #include <cstring> using namespace std; #define ll long long const int N = 10005; const int INF = 0x3f3f3f3f; int dp[N]; int main() { // freopen("a.in" , &qu

动物名词

dog    n. 狗 I love dogs. Dogs are man's best friends. The dog is very cute. cat    n. 猫 I have a cat The cat is white The cat likes sleeping puppy    n. 小狗 doggie    n. 小狗 kitty    n. 小猫 rooster    n. 公鸡 It's hard to see a rooster in big cities. I sa

MapReuce中对大数据处理最合适的数据格式是什么?

本节作为<Hadoop从入门到精通>大型专题的第三章第二节将教大家如何在Mapreduce中使用XML和JSON两大常见格式,并分析比较最适合Mapreduce大数据处理的数据格式. 在本章的第一章节介绍中,我们简单了解了Mapreduce数据序列化的概念,以及其对于XML和JSON格式并不友好.本节作为<Hadoop从入门到精通>大型专题的第三章第二节将教大家如何在Mapreduce中使用XML和JSON两大常见格式,并分析比较最适合Mapreduce大数据处理的数据格式. 3.

一步步学习如何安装并使用SAP HANA Express Edition

使用Jerry这篇文章在Google Cloud platform上的Kubernetes集群部署HANA Express里介绍的方法在Google Cloud Platform的Kubernetes cluster上安装SAP HANA Express. 文中介绍了一个yaml文件,里面声明了容器镜像文件store/saplabs/hanaexpress:2.00.033.00.20180925.2. 安装完成后,在启动的pod里有两个容器,分别运行着SQLPad和HANA Express.

POJ 题目3481 Double Queue(SBT ro map)

Double Queue Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 11824   Accepted: 5385 Description The new founded Balkan Investment Group Bank (BIG-Bank) opened a new office in Bucharest, equipped with a modern computing environment provid

POJ百道水题列表

以下是poj百道水题,新手可以考虑从这里刷起 搜索1002 Fire Net1004 Anagrams by Stack1005 Jugs1008 Gnome Tetravex1091 Knight Moves1101 Gamblers1204 Additive equations 1221 Risk1230 Legendary Pokemon1249 Pushing Boxes 1364 Machine Schedule1368 BOAT1406 Jungle Roads1411 Annive

POJ 3282 Ferry Loading IV(模拟,队列)

题意   汽车通过渡船过河  渡船开始在左边   输入按车辆来的顺序输入河两岸的车   渡船每次运输的汽车的总长度不能超过渡船自己本身的长度  先来的车先走   求轮船至少跨河多少次才能将所有的车辆都运完 简单模拟  建两个队列  分别装左边的车  和右边的车   算出两边各至少需要运输多少次就行了 #include<cstdio> #include<cstring> #include<queue> using namespace std; int main() { i

AVL树(模板题)—— POJ 3481 Double Queue

对应POJ题目:点击打开链接 Double Queue Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 11741   Accepted: 5335 Description The new founded Balkan Investment Group Bank (BIG-Bank) opened a new office in Bucharest, equipped with a modern computing env

POJ 2063 Investment (完全背包)

A - Investment Time Limit:1000MS     Memory Limit:30000KB     64bit IO Format:%I64d & %I64u Submit Status Practice POJ 2063 Description John never knew he had a grand-uncle, until he received the notary's letter. He learned that his late grand-uncle