P3092 [USACO13NOV]没有找零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(1 <= K <= 16)个硬币,面值的范围是1..100,000,000。

约翰想按顺序买 N个物品(1 <= N <= 100,000),第i个物品需要花费c(i)块钱,(1 <= c(i) <= 10,000)。

在依次进行的购买N个物品的过程中,约翰可以随时停下来付款,每次付款只用一个硬币,支付购买的内容是从上一次支付后开始到现在的这些所有物品(前提是该硬币足以支付这些物品的费用)。不幸的是,商场的收银机坏了,如果约翰支付的硬币面值大于所需的费用,他不会得到任何找零。

请计算出在购买完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.
* 输入输出样例

输入样例#1:
4 0
0 0 1 1
输出样例#1:
512
输入样例#2:
5 0
1 0 1 1 1
输出样例#2:
5120
题解:状态压缩DP+二分优化
直接看代码好了

#include<iostream>
#include<cstdlib>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#define il inline
#define rg register
#define ll long long
#define N 20
#define M 100010
#define inf 2147483640
using namespace std;
int k,n;
ll coin[N],cost[M],sum[M],dp[M],ans=-1;
il int read()
{
    rg int data=0;rg char c=getchar();
    while(c<'0'||c>'9')c=getchar();
    while(c>='0'&&c<='9')data=(data<<3)+(data<<1)+c-'0',c=getchar();
    return data;
}
int find(rg int lim){
    rg int le=1,ri=n,pos=0;
    while(le<=ri){
        int mid=((le+ri)>>1);
        if(sum[mid]<=lim)le=mid+1,pos=mid;
        else ri=mid-1;
    }
    return pos;
}
int main()
{
    freopen("s.in","r",stdin);
    k=read(),n=read();
    for(rg int i=0;i<k;++i)//请注意这里的for循环
        coin[i]=read();
    for(rg int i=1;i<=n;++i)
        cost[i]=read(),sum[i]=sum[i-1]+cost[i];//预处理

    for(rg int p=0;p<(1<<k);++p){//枚举所有状态
        for(rg int i=0;i<k;++i){
            if(!(p&(1<<i)))continue;//如果当前货币i没有被选则跳过
            ll res=dp[p^(1<<i)];//p^(1<<j)表示状态pi去掉ci货币时得到的新状态
            //那么res表示新状态可以到达的最末商品
            ll pos=find(sum[res]+coin[i]);
            //我们期望查找到的是从商品1到res的价值并加上货币ci价值的总和pos
            //二分查找到最后一个小于等于的商品下标
            dp[p]=max(dp[p],pos);
            //取最大值
        }
    }
    for(rg int p=0;p<(1<<k);++p){
        if(dp[p]==n){
            ll cnt=0;
            for(rg int j=0;j<k;++j){
                if(!(p&(1<<j)))cnt+=coin[j];
                //如果当前位置没有货币,统计答案
            }
            ans=max(ans,cnt);
        }
    }
    cout<<ans;
    return 0;
}

总结:被for循环坑了一波,最近状态压缩DP,做到快吐

P10491 [USACO09NOV] The Chivalrous Cow B 是一个涉及在二维网格中按照特定规则(类似象棋中马的走法)寻找最短路径的问题。以下是一些类似的题目: #### 洛谷 P1135 奇怪的电梯 有一个奇怪的电梯,大楼的每一层楼都可以停电梯,而且第 $i$ 层楼($1 \leq i \leq N$)上有一个数字 $K_i$($0 \leq K_i \leq N$)。电梯只有两个按钮:上和下。从第 $i$ 层楼上楼时,若按下上的按钮,则会上升 $K_i$ 层;若按下下的按钮,则会下降 $K_i$ 层。当然,电梯不能上升到超过 $N$ 层,也不能下降到低于 $1$ 层。问从 $A$ 层到 $B$ 层至少要按多少次按钮。 #### 洛谷 P1331 海战 在一个矩形的海域上,分布着一些战舰,每艘战舰由若干个相邻(上下左右相邻)的格子组成。现在给出海域的地图,问这片海域上有多少艘战舰,并且判断这些战舰的分布是否符合规则(战舰不能相邻,即两艘战舰之间至少有一个空白格子)。 #### 洛谷 P1605 迷宫 给定一个 $N \times M$ 方格的迷宫,迷宫里有一些障碍格子不能通过,从起点 $(sx, sy)$ 出发,要到达终点 $(fx, fy)$,问有多少条不同的路径可以走。只能向上下左右四个方向移动。 以下是一个简单的广度优先搜索(BFS)示例代码,用于解决类似的最短路径问题: ```python from collections import deque # 定义方向数组,上下左右 dx = [-1, 1, 0, 0] dy = [0, 0, -1, 1] def bfs(grid, start, end): rows, cols = len(grid), len(grid[0]) visited = [[False] * cols for _ in range(rows)] queue = deque([(start[0], start[1], 0)]) # (x, y, steps) visited[start[0]][start[1]] = True while queue: x, y, steps = queue.popleft() if (x, y) == end: return steps for i in range(4): new_x = x + dx[i] new_y = y + dy[i] if 0 <= new_x < rows and 0 <= new_y < cols and not visited[new_x][new_y] and grid[new_x][new_y] != '#': visited[new_x][new_y] = True queue.append((new_x, new_y, steps + 1)) return -1 # 无法到达 # 示例使用 grid = [ ['.', '.', '.', '#'], ['.', '#', '.', '.'], ['.', '.', '.', '.'], ['.', '#', '#', '.'] ] start = (0, 0) end = (3, 3) result = bfs(grid, start, end) print(result) ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值