[USACO06NOV] Corn Fields

(试题来源:Link

题目描述

Farmer John has purchased a lush new rectangular pasture composed of \(M\) by \(N (1 ≤ M ≤ 12; 1 ≤ N ≤ 12)\) square parcels. He wants to grow some yummy corn for the cows on a number of squares. Regrettably, some of the squares are infertile and can't be planted. Canny FJ knows that the cows dislike eating close to each other, so when choosing which squares to plant, he avoids choosing squares that are adjacent; no two chosen squares share an edge. He has not yet made the final choice as to which squares to plant.

Being a very open-minded man, Farmer John wants to consider all possible options for how to choose the squares for planting. He is so open-minded that he considers choosing no squares as a valid option! Please help Farmer John determine the number of ways he can choose the squares to plant.

农场主 John 新买了一块长方形的新牧场,这块牧场被划分成 \(M\) 行 \(N\) 列 \((1 ≤ M ≤ 12; 1 ≤ N ≤ 12)\),每一格都是一块正方形的土地。John 打算在牧场上的某几格里种上美味的草,供他的奶牛们享用。

遗憾的是,有些土地相当贫瘠,不能用来种草。并且,奶牛们喜欢独占一块草地的感觉,于是 John 不会选择两块相邻的土地,也就是说,没有哪两块草地有公共边。

John 想知道,如果不考虑草地的总块数,那么,一共有多少种种植方案可供他选择?(当然,把新牧场完全荒废也是一种方案)

输入输出格式

输入格式:

  • 第 \(1\) 行:两个整数 \(M\) 和 \(N\),用空格隔开。
  • 第 \(2\) 到第 \(M+1\) 行:每行包含 \(N\) 个用空格隔开的整数,描述了每块土地的状态。第 \(i+1\) 行描述了第 \(i\) 行的土地,所有整数均为 \(0\) 或 \(1\),是 \(1\) 的话,表示这块土地足够肥沃,\(0\) 则表示这块土地不适合种草。

输出格式:

  • 一个整数,即牧场分配总方案数除以 \(100,000,000\) 的余数。

输入输出样例

输入样例 #1:

2 3
1 1 1
0 1 0

输出样例 #1:

9

状态压缩 DP,预处理每行可能的全部状态。每个当前状态由上一行所有并行状态累加得到。

/* [USACO06NOV] Corn Fields
* Au: GG
*/
#include <cstdio>
const int N = 4197, M = 15, MOD = 100000000;
int n, m, dp[M][N], ans;
struct node {
int s[N], st;
} p[M]; int main() {
scanf("%d%d", &m, &n);
for (int i=1, a, t; i<=m; i++) {
t = 0;
for (int j=1; j<=n; j++)
scanf("%d", &a), t = (t<<1) + 1 - a;
for (int j=0; j< (1<<n); j++)
if ((j&(j<<1))||(j&(j>>1))||(j&t)) continue;
else p[i].s[++p[i].st] = j;
}
for (int i=1; i<=p[1].st; i++) dp[1][i] = 1;
for (int i=2; i<=m; i++)
for (int j=1; j<=p[i].st; j++)
for (int k=1; k<=p[i-1].st; k++)
if (!(p[i].s[j] & p[i-1].s[k])) dp[i][j] += dp[i-1][k];
for (int i=1; i<=p[m].st; i++)
ans = (ans + dp[m][i]) % MOD;
printf("%d\n", ans);
return 0;
}

[USACO13NOV] No Change

(试题来源:Link

题目描述

Farmer John is at the market to purchase supplies for his farm. He has in his pocket \(K\) coins \((1 \leq K \leq 16)\), each with value in the range \(1..100,000,000\). FJ would like to make a sequence of \(N\) purchases \((1 \leq N \leq 100,000)\), where the ith purchase costs \(c(i)\) units of money \((1 \leq c(i) \leq 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.

John 到商场购物,他的钱包里有 \(K(1 \leq K \leq 16)\) 个硬币,面值的范围是 \(1..100,000,000\)。

John 想按顺序买 \(N\) 个物品 \((1 \leq N \leq 100,000)\),第 \(i\) 个物品需要花费 \(c(i)\) 块钱,\((1 \leq c(i) \leq 10,000)\)。

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

请计算出在购买完 \(N\) 个物品后,John 最多剩下多少钱。如果无法完成购买,输出 \(- 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:

3 6
12
15
10
6
3
3
2
3
7

输出样例 #1:

12

说明

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.

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.


前缀和存物品费用;\(dp[i]\) 为 \(i\) 状态能购买的最大数量商品。由每个状态产生新的状态(刷表法),二分求出当前已使用 \(i\) 状态硬币之后,下一个硬币能购买到的最大数量商品,统计最大值。

二分理论上可以用 lower_bound 替换,可惜本人技艺不精,bug 太多故未使用。

/* [USACO13NOV] No Change
* Au: GG
*/
#include <cstdio>
#include <algorithm>
using namespace std;
const int N = 100005;
int k, n, w[N], coin[18], dp[1<<17], ans=-1; int main() {
scanf("%d%d", &k, &n);
for (int i=1; i<=k; i++) scanf("%d", &coin[i]);
for (int i=1; i<=n; i++) scanf("%d", &w[i]), w[i]+=w[i-1];
for (int i=0; i<(1<<k); i++) {
if (dp[i]==n) {
int res=0;
for (int j=1; j<=k; j++) if (!(i&(1<<j-1))) res+=coin[j];
ans = max(ans, res);
}
for (int j=1; j<=k; ++j) if (!(i&(1<<j-1))) {
int now = i | (1<<j-1);
int l=dp[i], r=n, mid;
while (l<r) {
mid = (l+r+1)>>1;
if (w[mid]-w[dp[i]]<=coin[j]) l=mid; else r=mid-1;
}
dp[now] = max(dp[now], l);
}
}
printf("%d\n", ans);
return 0;
}

状压 DP:[USACO06NOV] Corn Fields,[USACO13NOV] No Change的更多相关文章

  1. 状压DP : [USACO06NOV]玉米田

    玉米田 内存限制:128 MiB 时间限制:1000 ms 标准输入输出 题目类型:传统 评测方式:文本比较 题目描述 农场主John新买了一块长方形的新牧场,这块牧场被划分成M行N列(1 ≤ M ≤ ...

  2. P1879 [USACO06NOV]玉米田Corn Fields(状压dp)

    P1879 [USACO06NOV]玉米田Corn Fields 状压dp水题 看到$n,m<=12$,肯定是状压鸭 先筛去所有不合法状态,蓝后用可行的状态跑一次dp就ok了 #include& ...

  3. P1879 [USACO06NOV]玉米田Corn Fields 状压dp/插头dp

    正解:状压dp/插头dp 解题报告: 链接! ……我真的太菜了……我以为一个小时前要搞完的题目调错误调了一个小时……90分到100我差不多搞了一个小时…… 然后这题还是做过的……就很气,觉得确实是要搞 ...

  4. [USACO06NOV]玉米田$Corn \ \ Fields$ (状压$DP$)

    #\(\mathcal{\color{red}{Description}}\) \(Link\) 农场主\(John\)新买了一块长方形的新牧场,这块牧场被划分成\(M\)行\(N\)列\((1 ≤ ...

  5. [USACO06NOV]玉米田Corn Fields 状压DP

    题面: 农场主John新买了一块长方形的新牧场,这块牧场被划分成M行N列(1 ≤ M ≤ 12; 1 ≤ N ≤ 12),每一格都是一块正方形的土地.John打算在牧场上的某几格里种上美味的草,供他的 ...

  6. [USACO06NOV]玉米田Corn Fields (状压$dp$)

    题目链接 Solution 状压 \(dp\) . \(f[i][j][k]\) 代表前 \(i\) 列中 , 已经安置 \(j\) 块草皮,且最后一位状态为 \(k\) . 同时多记录一个每一列中的 ...

  7. 状压DP【p1879】[USACO06NOV]玉米田Corn Fields

    Description 农场主John新买了一块长方形的新牧场,这块牧场被划分成M行N列(1 ≤ M ≤ 12; 1 ≤ N ≤ 12),每一格都是一块正方形的土地.John打算在牧场上的某几格里种上 ...

  8. 【BZOJ1725】[Usaco2006 Nov]Corn Fields牧场的安排 状压DP

    [BZOJ1725][Usaco2006 Nov]Corn Fields牧场的安排 Description Farmer John新买了一块长方形的牧场,这块牧场被划分成M列N行(1<=M< ...

  9. poj3254 Corn Fields (状压DP)

    http://poj.org/problem?id=3254 Corn Fields Time Limit: 2000MS   Memory Limit: 65536K Total Submissio ...

随机推荐

  1. Linux运维工程师需掌握的技能

    笔者是运维工程师,对Linux方面有点心得,现在说一下需要掌握哪方面的工具吧.说到工具,在行外可以说是技能,在行内我们一般称之为工具,就是运维必须要掌握的工具.我就大概列出这几方面,这样入门就基本没有 ...

  2. WCF权限认证多种方式

    WCF身份验证一般常见的方式有:自定义用户名及密码验证.X509证书验证.ASP.NET成员资格(membership)验证.SOAP Header验证.Windows集成验证.WCF身份验证服务(A ...

  3. 基于vux的Vue路由切换动画

    const history = window.sessionStorage history.clear() let historyCount = history.getItem('count') * ...

  4. System的两常用个静态方法

    package cn.learn; /* System类在java.lang.System,和操作系统有关 1.currentTimeMillis直接调用,是一个返回为long型的静态方法 常用来计算 ...

  5. [Linux] 020 RPM 包的命名原则与其依赖性

    1. RPM 包命名原则 例如:httpd-2.2.15-15.e16.centos.1.i686.rpm 字符 释义 httpd 软件包名 2.2.15 软件版本 15 软件发布的次数 e16.ce ...

  6. Spring框架中Spring配置文件中<context:annotation-config/>标签说明

    <context:annotation-config/>此标签的重要作用就是: 省去系统繁琐的注解标签,加上一个此标签,就可以在此项目程序添加“注解”的功能,使系统识别相应的注解功能!! ...

  7. 【洛谷UVA307】小木棍Sticks

    小木棍Sticks[传送门] 算法的话:dfs+超强剪枝: (另外注意UVA上好像不接受万能头[因为万能头WA了两次,瑟瑟发抖]) 思路: 最直接的思路,枚举木棍长度来dfs,但这样很容易就TLE了. ...

  8. codeforces hack

    对某一题,首先你自己要先过TEST. 然后回到比赛的PROBLEM列表,把这题后面的锁锁上(锁上了就不能再提交了,所以没把握就别锁了), 然后到ROOM里面,你就可以看别人代码了,下面有HACK按钮, ...

  9. 初识NuGet

    因为想查一查opencvsharp的东西,然后发觉这个包可以再NuGet上面可以直接下载.我也经常在很多地方都可以看到NuGet,所以我想写下来,记录下来. NuGet是一个免费的并且开源的包管理器在 ...

  10. Linux学习笔记4-CentOS7中redis3.2.9安装教程

    redis下载地址:http://www.redis.cn/download.html 1.将下载过来的redis-3.2.9.tar.gz文件复制到/usr/local文件夹下 2.tar xzf ...