poj 1742 硬幣面值 拼湊 重解 poj 1014

題意:

1742 給定硬幣的面值 和 數量  ,能拼湊出指定範圍內的多少個數值

解法:

一上來首先想到分組揹包,然後優化成0-1揹包,設置體積爲給定值(從0---設定數值),初值爲0

0-1揹包計算O(N V) ,再循環0-V,果斷TLE


後來看解題報告發現一個更好的方法

在給定體積很大的情況下可以大量縮短計算時間

對於每一個分組,設定一個數組cnt表示拼湊到當前數值i所需要的當前分組的硬幣數cnt[i]

只需要一次揹包計算可以得出結論

#include <iostream>
#include <vector>
#include <map>
#include <list>
#include <set>
#include <deque>
#include <stack>
#include <queue>
#include <algorithm>
#include <cmath>
#include <cctype>
#include <cstdio>
#include <iomanip>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <string>
#include <sstream>
#include <cstring>
#include <queue>
using namespace std;

///宏定義
const int  INF = 990000000;
const int maxn = 100100 ;
const int MAXN = maxn;
///全局變量 和 函數
int max(int a, int b)
{
	return a > b ? a : b;
}

//int T;
//int nums[maxn];
int val[maxn];
//int f[maxn];
//int fee[maxn];
int n, m;
bool flag[maxn]; //當前值是否被覆蓋
int cnt[maxn];
int main()
{

	///變量定義
	int i, j;
	while(scanf("%d %d", &n, &m) != EOF && (n != 0 && m != 0))
	{
	
		for (i = 0; i < n; i++)
		{
			scanf("%d", &val[i]);
		}
		memset(flag, 0, sizeof(flag));
		flag[0] = true; //初始化很重要

		int ans = 0;
		for (i = 0; i < n; i++)
		{
			int sum;
			scanf("%d", &sum);  //該分組的硬幣數目
			memset(cnt, 0, sizeof(cnt));  //使用當前分組硬幣數目初始化
			for (j = val[i]; j <= m; j++)
			{
				if (flag[j - val[i]] && cnt[j - val[i]] < sum && !flag[j])  //如果上一個值被覆蓋,且還有當前分組硬幣,且當前值未覆蓋
				{
					flag[j] = true;
					ans++;  //記錄答案個數
					cnt[j] = cnt[j - val[i]] + 1;
				}
			}
		}

//		int ans = V;
		printf("%d\n", ans);
	}
	
	///結束
	return 0;
}

同樣的,採用這個方法重新解答poj 1014 拼湊硬幣面值的題目

當時採取的就是分組優化的方法

現在採用flag和cnt數組的方法,速度快一些

#include <iostream>
#include <vector>
#include <map>
#include <list>
#include <set>
#include <deque>
#include <stack>
#include <queue>
#include <algorithm>
#include <cmath>
#include <cctype>
#include <cstdio>
#include <iomanip>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <string>
#include <sstream>
#include <cstring>
#include <queue>
using namespace std;

///宏定義
const int  INF = 990000000;
const int maxn = 65100 ;
const int MAXN = maxn;
///全局變量 和 函數
int max(int a, int b)
{
	return a > b ? a : b;
}

//int T;
int nums[maxn];
int val[maxn];
//int f[maxn];
//int fee[maxn];
int n, m;
bool flag[maxn];
int cnt[maxn];
int main()
{
	//input

	///變量定義
	int i, j;
	int cases = 1;
	while(1)
	{
		int totval = 0;
		int V;
		bool flaginput = false;
		for (i = 1; i <= 6; i++)
		{
			scanf("%d", &nums[i]);
			if (nums[i] != 0)
			{
				flaginput = true;
				totval += i * nums[i];
			}			
		}
		if (!flaginput)
			break;

		printf("Collection #%d:\n", cases++);
		if (totval % 2 != 0)
		{
			printf("Can't be divided.\n");
			printf("\n");
			continue;
		}
		else
			V = totval / 2;

		int ans = 0;
		bool flagg = false;
		memset(flag, false, sizeof(flag));
		flag[0] = true;
		for (i = 1; i <= 6; i++)
		{
			int sum;
			sum = nums[i];
			memset(cnt, 0, sizeof(cnt));
			for (j = i; j <= V; j++)
			{
				if (flag[j - i] && cnt[j - i] < sum && !flag[j])
				{
					flag[j] = true;
					ans++;
					cnt[j] = cnt[j - i] + 1;
				}
			}
			if (flag[V])
			{
				flagg = true;
				break;
			}
		}
		
		if (flagg)
		{
			printf("Can be divided.\n");
			printf("\n");
		}
		else
		{
			printf("Can't be divided.\n");
			printf("\n");
		}

	}
	
	///結束
	return 0;
}



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章