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

  • 相关阅读:
    layui 3种导航栏
    SQL语句内做除法得出百分比
    JS 日期比较方法
    JDK 13 的 12 个新特性,真心涨姿势了
    怎么实现单点登录?面试必问!
    厉害了,Apache架构师们遵循的 30 条设计原则
    面试官问线程安全的List,看完再也不怕了!
    Java 类在 Tomcat 中是如何加载的?
    Redis 21问,你接得住不?
    String hashCode 这个数字,很多人不知道!
  • 原文地址:https://www.cnblogs.com/EDGsheryl/p/7337163.html
Copyright © 2011-2022 走看看