【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\n" , ans);
return 0;
}
【bzoj3312】[Usaco2013 Nov]No Change 状态压缩dp+二分的更多相关文章
- 【BZOJ3312】[Usaco2013 Nov]No Change 状压DP+二分
[BZOJ3312][Usaco2013 Nov]No Change Description Farmer John is at the market to purchase supplies for ...
- bzoj3312: [Usaco2013 Nov]No Change
题意: K个硬币,要买N个物品.K<=16,N<=1e5 给定买的顺序,即按顺序必须是一路买过去,当选定买的东西物品序列后,付出钱后,货主是不会找零钱的.现希望买完所需要的东西后,留下的钱 ...
- BFS+状态压缩DP+二分枚举+TSP
http://acm.hdu.edu.cn/showproblem.php?pid=3681 Prison Break Time Limit: 5000/2000 MS (Java/Others) ...
- 【bzoj3886】[Usaco2015 Jan]Moovie Mooving 状态压缩dp+二分
题目描述 Bessie is out at the movies. Being mischievous as always, she has decided to hide from Farmer J ...
- 【bzoj1231】[Usaco2008 Nov]mixup2 混乱的奶牛 状态压缩dp
题目描述 混乱的奶牛[Don Piele, 2007]Farmer John的N(4 <= N <= 16)头奶牛中的每一头都有一个唯一的编号S_i (1 <= S_i <= ...
- 【bzoj1725】[USACO2006 Nov]Corn Fields牧场的安排 状态压缩dp
题目描述 Farmer John新买了一块长方形的牧场,这块牧场被划分成M列N行(1<=M<=12; 1<=N<=12),每一格都是一块正方形的土地.FJ打算在牧场上的某几格土 ...
- bzoj 3312: [Usaco2013 Nov]No Change
3312: [Usaco2013 Nov]No Change Description Farmer John is at the market to purchase supplies for his ...
- hdu 4057 AC自己主动机+状态压缩dp
http://acm.hdu.edu.cn/showproblem.php?pid=4057 Problem Description Dr. X is a biologist, who likes r ...
- HOJ 2226&POJ2688 Cleaning Robot(BFS+TSP(状态压缩DP))
Cleaning Robot Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 4264 Accepted: 1713 Descri ...
随机推荐
- 【LG3527】[POI2011]MET-Meteors
[LG3527][POI2011]MET-Meteors 题面 洛谷 题解 整体二分. 每次二分\(mid\),如果到时间\(mid\)以收集过\(P_i\)就存入子序列\(L\),否则存入子序列\( ...
- python之web架构
一.web架构简介 web.py是一个轻量级Python web框架,它简单而且功能强大.web.py是一个开源项目.该框架由已故美国作家.Reddit联合创始人.RSS规格合作创造者.著名计算机黑客 ...
- springBoot Swagger2 接口文档生成
// 生成配置类 package com.irm.jd.config.swagger; import org.springframework.context.annotation.Bean; impo ...
- Spring Boot中使用缓存
Spring Boot中使用缓存 随着时间的积累,应用的使用用户不断增加,数据规模也越来越大,往往数据库查询操作会成为影响用户使用体验的瓶颈,此时使用缓存往往是解决这一问题非常好的手段之一. 原始的使 ...
- PHP中array_reduce()使用
array_reduce — 用回调函数迭代地将数组简化为单一的值 给定一个数组: $ar = array(1,2,3,4,5); 如果要求得这个数组中各个元素之和. 方法一. 很自然的用foreac ...
- windown reids
记录Windown安装Redis和php_redis扩展 和Linux系统不同windown中不需要编译安装:只需要下对版本拖拽过去即可: 首先安装redis服务: 可以百度下一个,只要注意系统版本即 ...
- 1. 两数之和【Leetcode中国,by java】
给定一个整数数组和一个目标值,找出数组中和为目标值的两个数. 你可以假设每个输入只对应一种答案,且同样的元素不能被重复利用. 示例: 给定 nums = [2, 7, 11, 15], target ...
- Java三种编译方式
Java程序代码需要编译后才能在虚拟机中运行,编译涉及到非常多的知识层面:编译原理.语言规范.虚拟机规范.本地机器码优化等:了解编译过程有利于了解整个Java运行机制,不仅可以使得我们编写出更优秀的代 ...
- Ubuntu16.04安装wps办公软件解决文字缺失
先到wps官网下载linux版wps安装包 选择64位的alpha版本下载: 下载完后,同样是cd到Downloads目录,用dpkg命令来安装它: cd Downloads/ sudo dpkg ...
- Python3 小工具-UDP扫描
from scapy.all import * import optparse import threading def scan(target,port): pkt=IP(dst=target)/U ...