3312: [Usaco2013 Nov]No Change

Description

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

Input

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.

Output

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

Sample Input

3 6
12
15
10
6
3
3
2
3
7

INPUT DETAILS: FJ has 3 coins of values 12, 15, and 10. He must make purchases in sequence of value 6, 3, 3, 2, 3, and 7.

Sample Output

12
OUTPUT DETAILS: FJ spends his 10-unit coin on the first two purchases, then the 15-unit coin on the remaining purchases. This leaves him with the 12-unit coin.
题解:
k<=16,很快可以想到是状压DP,设f[i]为i状态下最多可以买的个数,当f[i]=m时计算剩余价值(注意一下循环的范围)。。
  1. #include<stdio.h>
  2. #include<iostream>
  3. using namespace std;
  4. const int M=;
  5. int n,m,i,j,s,ans,p,a[],sum[M],f[(<<)+];
  6. inline int erfen(int x,int v,int l,int r)
  7. {
  8. if(l>r) return r;
  9. int mid=(l+r)>>;
  10. if(sum[mid]-sum[x-]<=v) return erfen(x,v,mid+,r);else return erfen(x,v,l,mid-);
  11. }
  12. inline void read(int &v){
  13. char ch,fu=;
  14. for(ch='*'; (ch<''||ch>'')&&ch!='-'; ch=getchar());
  15. if(ch=='-') fu=, ch=getchar();
  16. for(v=; ch>=''&&ch<=''; ch=getchar()) v=v*+ch-'';
  17. if(fu) v=-v;
  18. }
  19. int main()
  20. {
  21. scanf("%d%d",&n,&m);
  22. for(i=;i<=n;i++)
  23. read(a[i]);
  24. for(i=;i<=m;i++)
  25. read(p),sum[i]=sum[i-]+p;
  26. ans=-;
  27. for(i=;i<=(<<n)-;i++)
  28. {
  29. s=;
  30. for(j=;j<n;j++)
  31. if(i&(<<j)) f[i]=max(f[i],erfen(f[i^(<<j)]+,a[j+],f[i^(<<j)]+,m));else
  32. s+=a[j+];
  33. if(f[i]==m) ans=max(ans,s);
  34. }
  35. cout<<ans;
  36. return ;
  37. }

bzoj 3312: [Usaco2013 Nov]No Change的更多相关文章

  1. bzoj3312: [Usaco2013 Nov]No Change

    题意: K个硬币,要买N个物品.K<=16,N<=1e5 给定买的顺序,即按顺序必须是一路买过去,当选定买的东西物品序列后,付出钱后,货主是不会找零钱的.现希望买完所需要的东西后,留下的钱 ...

  2. 【BZOJ3312】[Usaco2013 Nov]No Change 状压DP+二分

    [BZOJ3312][Usaco2013 Nov]No Change Description Farmer John is at the market to purchase supplies for ...

  3. BZOJ 3315: [Usaco2013 Nov]Pogo-Cow( dp )

    我真想吐槽USACO的数据弱..= = O(n^3)都能A....上面一个是O(n²), 一个是O(n^3) O(n^3)做法, 先排序, dp(i, j) = max{ dp(j, p) } + w ...

  4. BZOJ 3314: [Usaco2013 Nov]Crowded Cows( 单调队列 )

    从左到右扫一遍, 维护一个单调不递减队列. 然后再从右往左重复一遍然后就可以统计答案了. ------------------------------------------------------- ...

  5. 【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 ...

  6. [Usaco2013 Nov]No Change

    Description Farmer John is at the market to purchase supplies for his farm. He has in his pocket K c ...

  7. BZOJ 3314 [Usaco2013 Nov]Crowded Cows:单调队列

    题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=3314 题意: N头牛在一个坐标轴上,每头牛有个高度.现给出一个距离值D. 如果某头牛在它的 ...

  8. BZOJ3315: [Usaco2013 Nov]Pogo-Cow

    3315: [Usaco2013 Nov]Pogo-Cow Time Limit: 3 Sec  Memory Limit: 128 MBSubmit: 143  Solved: 79[Submit] ...

  9. BZOJ3314: [Usaco2013 Nov]Crowded Cows

    3314: [Usaco2013 Nov]Crowded Cows Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 86  Solved: 61[Subm ...

随机推荐

  1. HTML中设置超链接字体 & 字体颜色

    定义链接样式 CSS为一些特殊效果准备了特定的工具,我们称之为“伪类”.其中有几项是我们经常用到的,下面我们就详细介绍一下经常用于定义链接样式的四个伪类,它们分别是: :link :visited : ...

  2. mysql中的单引号/小数点/字符转换为数字/警告信息

    我们准备玩点有趣的: select 一个数字: mysql from mysql.user; +---+ | +---+ | | | +---+ rows in set (0.00 sec) mysq ...

  3. postman测试express restful接口

    安装express及postman var express = require('express') var app = express(); var calculation = require('. ...

  4. frp 使用入门

    1.下载安装对应系统版本 https://github.com/fatedier/frp/releases/ 2.将下载的frp移动到系统软件目录 mv frp/ /usr/local 3.配置frp ...

  5. centos_7.1.1503_src_3

    http://vault.centos.org/7.1.1503/os/Source/SPackages/ kdf-4.10.5-3.el7.src.rpm 05-Jul-2014 13:28 161 ...

  6. $scope作用及模块化解决全局问题

    $scope对象就是一个普通的JavaScript对象,我们可以在其上随意修改或添加属性.$scope对象在AngularJS中充当数据模型,但与传统的数据模型不一样,$scope并不负责处理和操作数 ...

  7. OC 06 Block、数组高级

    主要内容: ⼀.Block语法 ⼆.Block使⽤ 三.Block实现数组排序 Block简介 Block:块语法,本质上是匿名函数(没有名称的函数) 标准C⾥面没有Block,C语⾔言的后期扩展版本 ...

  8. 12-4 NSString

    原文:http://rypress.com/tutorials/objective-c/data-types/nsstring NSString 在本教程的内容可能我们已经看到过很多次了,NSStri ...

  9. Redis 集群使用(2)

    Redis包含三种集群策略: 主从复制 哨兵模式 redis cluster 主从复制 在主从复制中,数据分为两类:主数据库(master)和 从数据库(slave).其中主从复制有如下特点: 主数据 ...

  10. 机器学习方法(七):Kmeans聚类K值如何选,以及数据重抽样方法Bootstrapping

    欢迎转载,转载请注明:本文出自Bin的专栏blog.csdn.net/xbinworld. 技术交流QQ群:433250724,欢迎对算法.技术感兴趣的同学加入.我的博客写一些自己用得到东西,并分享给 ...