zoukankan      html  css  js  c++  java
  • POJ-1742 Coins

    POJ-1742 Coins

    题面

    Problem Description

    People in Silverland use coins.They have coins of value A1,A2,A3...An Silverland dollar.One day Tony opened his money-box and found there were some coins.He decided to buy a very nice watch in a nearby shop. He wanted to pay the exact price(without change) and he known the price would not more than m.But he didn't know the exact price of the watch.
    You are to write a program which reads n,m,A1,A2,A3...An and C1,C2,C3...Cn corresponding to the number of Tony's coins of value A1,A2,A3...An then calculate how many prices(form 1 to m) Tony can pay use these coins.

    Input

    The input contains several test cases. The first line of each test case contains two integers n(1<=n<=100),m(m<=100000).The second line contains 2n integers, denoting A1,A2,A3...An,C1,C2,C3...Cn (1<=Ai<=100000,1<=Ci<=1000). The last test case is followed by two zeros.

    Output

    For each test case output the answer on a single line.

    题意

    钱有多少组合方式,组成的价格不同。

    上来,诶 多重背包 二进制优化 美滋滋。

    TLE。。MMP。。。

    一查,居然是啥真男人八题。。。

    用单调队列来优化多重背包。论文请查看2009年的徐持衡的论文《浅谈几类背包题》

    太晚了先睡了,POJ比openjudge慢好多啊QWQ

    代码

    #include <iostream>
    #include <algorithm>
    #include <vector>
    #include <cstring>
    #include <deque> 
    #include <cstdio>
    using namespace std;
    
    int n,m;
    int dp[100010]; 
    pair<int,int> p[110];
    int ans;
    deque<pair<int,int> > q;
    
    int main()
    {
    	while (scanf("%d%d",&n,&m)==2)
    	{
    		if (n==0 && m==0) return 0;
    		memset(dp,0,sizeof dp);
    		dp[0]=1;
    		for (int i=1;i<=n;i++) scanf("%d",&p[i].first);
    		for (int i=1;i<=n;i++) scanf("%d",&p[i].second);
    		for (int i=1;i<=n;i++)
    		{
    			if (p[i].second==1)
    			{
    				for (int j=m-p[i].first;j>=0;j--) if (dp[j]) dp[j+p[i].first]=1;
    			}
    			else if (p[i].first*p[i].second>=m)
    			{
    				for (int j=0;j<=m;j++) if (dp[j] && j+p[i].first<=m) dp[j+p[i].first]=1;
    			}
    			else 
    			{
    				for (int j=0;j<p[i].first;j++)
    				{
    					while (!q.empty()) q.pop_front();
    					for (int k=j;k<=m;k+=p[i].first)
    					{
    						if (!q.empty()) {
    							if (k-q.front().first>p[i].first*p[i].second) q.pop_front();
    						}
    						if (dp[k])
    						{
    							q.push_back(make_pair(k,dp[k]));
    						}
    						else if (!q.empty()) dp[k]=1;
    					}
    				}
    			}
    		}
    		ans=0;
    		for (int i=1;i<=m;i++) ans+=dp[i]; 
    		printf("%d
    ",ans);
    	}
    }
    

    题目链接

    http://poj.org/problem?id=1742

  • 相关阅读:
    冒险岛数据封包分析
    【转】安装版系统封装入门详细解说
    拦截其它程序的网络数据封包
    封包加密解密-01
    vs2010下载Microsoft Visual Studio 2010 Express(vs2010中文版下载)速成官方合集正式版
    《Visual C++开发实战1200例 第1卷》扫描版[PDF]
    《Visual Basic开发实战1200例》包括第I卷、第II卷共计1200个例子,本书是第I卷,共计600个例子。
    服务器上常见软件
    HDU1686
    HDU3336
  • 原文地址:https://www.cnblogs.com/EDGsheryl/p/7337163.html
Copyright © 2011-2022 走看看