题意:

K个硬币,要买N个物品。K<=16,N<=1e5

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

=>K那么小。。。那么我们可以想到二进制枚举状态。。。然后转移。。。好像算不上状压dp吧。。。时间复杂度O(K2^Klogn)

#include<cstdio>
#include<cstring>
#include<cctype>
#include<algorithm>
using namespace std;
#define rep(i,s,t) for(int i=s;i<=t;i++)
#define dwn(i,s,t) for(int i=s;i>=t;i--)
#define clr(x,c) memset(x,c,sizeof(x))
int read(){
int x=0;char c=getchar();
while(!isdigit(c)) c=getchar();
while(isdigit(c)) x=x*10+c-'0',c=getchar();
return x;
}
const int nmax=20;
const int maxn=1e5+5;
const int inf=0x7f7f7f7f;
int a[nmax],b[maxn],dp[maxn],n,m,sm;
int find(int x,int eo){
if(x==m) return 0;
int l=x,r=m,ans=0,mid;
while(l<=r){
mid=(l+r)>>1;
if(b[mid]-b[x]<=eo) ans=mid,l=mid+1;
else r=mid-1;
}
return ans-x;
}
int main(){
n=read(),m=read(),sm=0;
rep(i,1,n) a[i]=read(),sm+=a[i];
rep(i,1,m) b[i]=read()+b[i-1];
int se=(1<<n)-1,tm=0,ans=-1;
rep(i,1,se){
tm=0;
rep(j,1,n) if(i&(1<<(j-1))) dp[i]=max(dp[i],dp[i-(1<<(j-1))]+find(dp[i-(1<<j-1)],a[j])),tm+=a[j];
if(dp[i]==m) ans=max(ans,sm-tm);
//printf("%d:%d\n",i,dp[i]);
}
printf("%d\n",ans);return 0;
}

  

3312: [Usaco2013 Nov]No Change

Time Limit: 10 Sec  Memory Limit: 128 MB
Submit: 177  Solved: 113
[Submit][Status][Discuss]

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.

HINT

 

Source

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

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

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

  2. bzoj 3312: [Usaco2013 Nov]No Change

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

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

  4. [Usaco2013 Nov]No Change

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

  5. BZOJ3315: [Usaco2013 Nov]Pogo-Cow

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

  6. BZOJ3314: [Usaco2013 Nov]Crowded Cows

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

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

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

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

  9. 3314: [Usaco2013 Nov]Crowded Cows

    3314: [Usaco2013 Nov]Crowded Cows Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 111  Solved: 79[Sub ...

随机推荐

  1. 51nod 1154【DP】

    区间DP大暴力吧?GG. dp[ i ] 为字符至 i 的最少数量. 如果[Left , Right]是回文串, dp[Right] = min(dp[ Right ] , dp[Left-1] + ...

  2. python之01电脑和操作系统简史

    电脑简史 早期计算方式发展 :手指和石头 ->结绳 ->算筹->计算尺 -> 算盘 19岁时(1642),帕斯卡发明了人类有史以来第一台机械计算机——帕斯卡加法器.它是一种系列 ...

  3. rlwrap: command not found和解决linux下sqlplus 提供浏览历史命令行的功能

    rlwrap工具可以解决linux下sqlplus 提供浏览历史命令行的功能,和删除先前输入错误的字母等问题 1.安装 需要readline包 这个安装光盘就有 [root@asm RedHat]# ...

  4. 多实例:MySQL系列之二

    MySQL的多实例配置 ​ 在一台物理机中需要多个测试环境,那么就需要用到了搭建数据库的多个实例,多个实例的意思就是运行多份程序,实例与实例之间没有影响.要注意监听的端口需要不同. 环境:CentOS ...

  5. 小程序组件交互 -- 传入js

    1.父组件(wxml页面)向子组件传递 在子组件中定义需要传入的属性 properties: { count: { type: Number, value: 0, observer:function( ...

  6. 如何顺畅使用sourcetree可视化工具

    http://www.360doc.com/content/17/0711/10/11253639_670493403.shtml sourcetree软件下载 下载地址:https://www.so ...

  7. c# 手动实现 \u 转义字符。。效果。。。

    string s ="\\u"+item.Icon; // item.Icon = UnicodeEncoding.Unicode.GetString(UnicodeEncodin ...

  8. 牛客假日团队赛1 I.接机

    链接: https://ac.nowcoder.com/acm/contest/918/I 题意: 一场别开生面的牛吃草大会就要在Farmer John的农场举办了! 世界各地的奶牛将会到达当地的机场 ...

  9. 转 基于MySQL MEB的备份恢复

    几种备份方式的介绍 mysqlbackup是一个热备份工具.也就是说它不像mysqldump那样给表上一个全局锁,由于mysqldump上了这个锁,所以就造成客户端只能对 数据库进行读操作不能写,这也 ...

  10. unity ForceMode

    public float jumpAbility; GetComponent<Rigidbody>().AddForce(Vector3.up * jumpAbility, ForceMo ...