zoukankan      html  css  js  c++  java
  • 【bzoj3312】[Usaco2013 Nov]No Change 状态压缩dp+二分

    题目描述

    Farmer John is at the market to purchase supplies for his farm. He has in his pocket K coins (1 <= K <= 16), each with value in the range 1..100,000,000. FJ would like to make a sequence of N purchases (1 <= N <= 100,000), where the ith purchase costs c(i) units of money (1 <= c(i) <= 10,000). As he makes this sequence of purchases, he can periodically stop and pay, with a single coin, for all the purchases made since his last payment (of course, the single coin he uses must be large enough to pay for all of these). Unfortunately, the vendors at the market are completely out of change, so whenever FJ uses a coin that is larger than the amount of money he owes, he sadly receives no changes in return! Please compute the maximum amount of money FJ can end up with after making his N purchases in sequence. Output -1 if it is impossible for FJ to make all of his purchases.

    K个硬币,要买N个物品。

    给定买的顺序,即按顺序必须是一路买过去,当选定买的东西物品序列后,付出钱后,货主是不会找零钱的。现希望买完所需要的东西后,留下的钱越多越好,如果不能完成购买任务,输出-1

    输入

    Line 1: Two integers, K and N.

    * Lines 2..1+K: Each line contains the amount of money of one of FJ's coins.

    * Lines 2+K..1+N+K: These N lines contain the costs of FJ's intended purchases. 

    输出

    * Line 1: The maximum amount of money FJ can end up with, or -1 if FJ cannot complete all of his purchases.

    样例输入

    3 6
    12
    15
    10
    6
    3
    3
    2
    3
    7

    样例输出

    12


    题解

    状压dp+二分

    f[i]表示硬币使用状态为i时最多能购买的物品

    那么有f[i]=k (sum[k]-sum[f[i^(1<<j)]]≤v[j])。

    然后二分查找求出k即可。

    还是注意数组从1开始的问题。

    #include <cstdio>
    #include <algorithm>
    using namespace std;
    int f[70000] , v[20] , a[100010] , sum[100010] , cost[70000] , n , base[70000];
    int search(int z , int c)
    {
    	int l = z , r = n , mid , ans = -1;
    	while(l <= r)
    	{
    		mid = (l + r) >> 1;
    		if(sum[mid] - sum[z] <= c)
    			ans = mid , l = mid + 1;
    		else r = mid - 1;
    	}
    	return ans;
    }
    int main()
    {
    	int k , i , j , tmp , ans = -1 , sn = 0;
    	scanf("%d%d" , &k , &n);
    	for(i = 1 ; i <= k ; i ++ )
    		scanf("%d" , &v[i]) , sn += v[i] , base[1 << (i - 1)] = i;
    	for(i = 1 ; i <= n ; i ++ )
    		scanf("%d" , &a[i]) , sum[i] = sum[i - 1] + a[i];
    	for(i = 1 ; i < (1 << k) ; i ++ )
    	{
    		for(j = 1 ; j <= k ; j ++ )
    		{
    			if((1 << (j - 1)) & i)
    			{
    				tmp = search(f[i ^ (1 << (j - 1))] , v[j]);
    				if(tmp != -1)
    					f[i] = max(f[i] , tmp);
    			}
    		}
    	}
    	for(i = 1 ; i < (1 << k) ; i ++ )
    	{
    		cost[i] = cost[i - (i & (-i))] + v[base[i & (-i)]];
    		if(f[i] == n) ans = max(ans , sn - cost[i]);
    	}
    	printf("%d
    " , ans);
    	return 0;
    }

     

  • 相关阅读:
    用fnmatch函数进行字符通配
    activity和service之间的相互通信方法
    IGMP协议简介
    Android2.2快速入门
    Android开发之旅:HelloWorld项目的目录结构
    Android的五大基本组件
    Android Service 组件
    TCP交互数据流 成块数据流
    为什么要进行IP选路?
    embOS实时操作系统 任务通讯
  • 原文地址:https://www.cnblogs.com/GXZlegend/p/6440961.html
Copyright © 2011-2022 走看看