bzoj 3312: [Usaco2013 Nov]No Change

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时计算剩余价值(注意一下循环的范围)。。

#include<stdio.h>
#include<iostream>
using namespace std;
const int M=100005;
int n,m,i,j,s,ans,p,a[20],sum[M],f[(1<<16)+5];
inline int erfen(int x,int v,int l,int r)
{
    if(l>r) return r;
    int mid=(l+r)>>1;
    if(sum[mid]-sum[x-1]<=v) return erfen(x,v,mid+1,r);else return erfen(x,v,l,mid-1);
}
inline void read(int &v){
    char ch,fu=0;
    for(ch=‘*‘; (ch<‘0‘||ch>‘9‘)&&ch!=‘-‘; ch=getchar());
    if(ch==‘-‘) fu=1, ch=getchar();
    for(v=0; ch>=‘0‘&&ch<=‘9‘; ch=getchar()) v=v*10+ch-‘0‘;
    if(fu) v=-v;
}
int main()
{
    scanf("%d%d",&n,&m);
    for(i=1;i<=n;i++)
        read(a[i]);
    for(i=1;i<=m;i++)
        read(p),sum[i]=sum[i-1]+p;
    ans=-1;
    for(i=1;i<=(1<<n)-1;i++)
    {
        s=0;
        for(j=0;j<n;j++)
            if(i&(1<<j)) f[i]=max(f[i],erfen(f[i^(1<<j)]+1,a[j+1],f[i^(1<<j)]+1,m));else
            s+=a[j+1];
        if(f[i]==m) ans=max(ans,s);
    }
    cout<<ans;
    return 0;
}
时间: 2024-07-30 13:36:06

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

bzoj3312: [Usaco2013 Nov]No Change

题意: K个硬币,要买N个物品.K<=16,N<=1e5 给定买的顺序,即按顺序必须是一路买过去,当选定买的东西物品序列后,付出钱后,货主是不会找零钱的.现希望买完所需要的东西后,留下的钱越多越好,如果不能完成购买任务,输出-1 =>K那么小...那么我们可以想到二进制枚举状态...然后转移...好像算不上状压dp吧...时间复杂度O(K2^Klogn) #include<cstdio> #include<cstring> #include<cctype&g

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

从左到右扫一遍, 维护一个单调不递减队列. 然后再从右往左重复一遍然后就可以统计答案了. ---------------------------------------------------------------------------- #include<bits/stdc++.h> #define rep(i, n) for(int i = 0; i < n; ++i) #define clr(x, c) memset(x, c, sizeof(x)) #define forea

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( i ) ( t <= p <= j ) 表示跳到第 i 个点, 上一个点是在 j 的最大得分, 其中t是满足条件的最小p. 我们在计算dp(i, j) (1 <= j <= i )时会发现, 随着 j 的递减, t也在不断地减小, 这样我们只要在dp过程中维护h(i, j)表示 m

3314: [Usaco2013 Nov]Crowded Cows

3314: [Usaco2013 Nov]Crowded Cows Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 111  Solved: 79[Submit][Status][Discuss] Description Farmer John's N cows (1 <= N <= 50,000) are grazing along a one-dimensional fence. Cow i is standing at location x(

BZOJ 1662: [Usaco2006 Nov]Round Numbers 圆环数(数位DP+恶心细节)

BZOJ 1662: [Usaco2006 Nov]Round Numbers 圆环数 Time Limit: 5 Sec  Memory Limit: 64 MB Description 正如你所知,奶牛们没有手指以至于不能玩“石头剪刀布”来任意地决定例如谁先挤奶的顺序.她们甚至也不能通过仍硬币的方式. 所以她们通过"round number"竞赛的方式.第一头牛选取一个整数,小于20亿.第二头牛也这样选取一个整数.如果这两个数都是 "round numbers"

矩阵乘法专题2——bzoj 1706 [usaco2007 Nov] relays 奶牛接力跑 题解

转载请注明:http://blog.csdn.net/jiangshibiao/article/details/24960651 [原题] 1706: [usaco2007 Nov]relays 奶牛接力跑 Time Limit: 5 Sec  Memory Limit: 64 MB Submit: 340  Solved: 162 [Submit][Status] Description FJ的N(2 <= N <= 1,000,000)头奶牛选择了接力跑作为她们的日常锻炼项目.至于进行接力

BZOJ3315: [Usaco2013 Nov]Pogo-Cow

3315: [Usaco2013 Nov]Pogo-Cow Time Limit: 3 Sec  Memory Limit: 128 MBSubmit: 143  Solved: 79[Submit][Status] Description In an ill-conceived attempt to enhance the mobility of his prize cow Bessie, Farmer John has attached a pogo stick to each of Bes

BZOJ3314: [Usaco2013 Nov]Crowded Cows

3314: [Usaco2013 Nov]Crowded Cows Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 86  Solved: 61[Submit][Status] Description Farmer John's N cows (1 <= N <= 50,000) are grazing along a one-dimensional fence. Cow i is standing at location x(i) and has

Bzoj3315 [Usaco2013 Nov]Pogo-Cow(luogu3089)

3315: [Usaco2013 Nov]Pogo-Cow Time Limit: 3 Sec  Memory Limit: 128 MBSubmit: 352  Solved: 181[Submit][Status][Discuss] Description In an ill-conceived attempt to enhance the mobility of his prize cow Bessie, Farmer John has attached a pogo stick to e