[IOI2007]Miners

题目大意:

两个人吃东西,总共有\(3\)种食物,每个人每次吃到食物时可以获得的收益是当前食物和前两次吃的食物中,不同食物的种数。现在给定一个长度为\(n(n\le10^5)\)的食物序列,按顺序每次将这些食物分配给两个人中的一个。问收益总和的最大值。

思路:

\(f[i][j][k][l][m]\)表示分完前\(i\)个吃的,\(A\)最后两个吃了\(i\)和\(j\),\(B\)最后两个吃了\(l\)和\(m\)时的最大收益。

时间复杂度\(\mathcal O(4^4n)\)。

为了防止MLE需要使用滚动数组。

源代码:

#include<cstdio>
#include<cctype>
#include<climits>
#include<algorithm>
inline int getint() {
register char ch;
while(!isdigit(ch=getchar()));
register int x=ch^'0';
while(isdigit(ch=getchar())) x=(((x<<2)+x)<<1)+(ch^'0');
return x;
}
inline int getfood() {
register char ch;
while(!isalpha(ch=getchar()));
if(ch=='M') return 1;
if(ch=='F') return 2;
if(ch=='B') return 3;
return 0;
}
const int N=1e5+1;
int f[2][4][4][4][4];
inline void up(int &x,const int &y) {
x=std::max(x,y);
}
inline int calc(int x,int y,const int &z) {
x=x?:z;
y=y?:z;
if(x==y&&y==z) return 1;
if(x==y&&y!=z) return 2;
if(x==z&&z!=y) return 2;
if(y==z&&x!=y) return 2;
return 3;
}
int main() {
const int n=getint();
for(register int j=0;j<4;j++) {
for(register int k=0;k<4;k++) {
for(register int l=0;l<4;l++) {
for(register int m=0;m<4;m++) {
f[0][j][k][l][m]=INT_MIN;
}
}
}
}
f[0][0][0][0][0]=0;
for(register int i=1;i<=n;i++) {
const bool cur=i&1;
const int x=getfood();
for(register int j=0;j<4;j++) {
for(register int k=0;k<4;k++) {
for(register int l=0;l<4;l++) {
for(register int m=0;m<4;m++) {
f[cur][j][k][l][m]=INT_MIN;
}
}
}
}
for(register int j=0;j<4;j++) {
for(register int k=0;k<4;k++) {
for(register int l=0;l<4;l++) {
for(register int m=0;m<4;m++) {
if(f[cur^1][j][k][l][m]==INT_MIN) continue;
up(f[cur][k][x][l][m],f[cur^1][j][k][l][m]+calc(j,k,x));
up(f[cur][j][k][m][x],f[cur^1][j][k][l][m]+calc(l,m,x));
}
}
}
}
}
int ans=0;
for(register int j=0;j<4;j++) {
for(register int k=0;k<4;k++) {
for(register int l=0;l<4;l++) {
for(register int m=0;m<4;m++) {
up(ans,f[n&1][j][k][l][m]);
}
}
}
}
printf("%d\n",ans);
return 0;
}

[IOI2007]Miners的更多相关文章

  1. [Ioi2007]Miners 矿工配餐(BZOJ1806)

    [Ioi2007]Miners 矿工配餐 Time Limit: 10 Sec  Memory Limit: 64 MBSubmit: 214  Solved: 128 Description 现有两 ...

  2. BZOJ 1806: [Ioi2007]Miners 矿工配餐( dp )

    dp... ------------------------------------------------------------------------------- #include<cs ...

  3. bzoj1806 [Ioi2007]Miners矿工配餐

    [bzoj1806][Ioi2007]Miners 矿工配餐 2014年7月10日1,7870 Description 现有两个煤矿,每个煤矿都雇用一组矿工.采煤工作很辛苦,所以矿工们需要良好饮食.每 ...

  4. bzoj 1806 [Ioi2007]Miners 矿工配餐(DP)

    [题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=1806 [题意] 给定一个权在1..3内的序列,在保持相对位置不变的情况下拆分成两个序列 ...

  5. BZOJ 1806 IOI2007 Miners 矿工配餐 动态规划

    题目大意:将一个123序列拆分为两个子序列.定义每一个数的贡献值为以这个数结尾的长度最大为3的子串中不同数的数量,求贡献值和的最大值 令f[i][a1][a2][b1][b2]为前i个数分成两组,第一 ...

  6. [bzoj1806] [ioi2007]Miners 矿工配餐

    相当于noip前两题难度的ioi题........ 还是挺好想的...算是状压一下?...两个二进制位可以表示三种食物或者没有,所以用四个二进制位表示某个煤矿最近两餐的情况... 先把各种情况加上各种 ...

  7. [IOI2007]Miners 矿工配餐

    link 其实就是一个比较简单的$IOI$题.简单$dp$就行,设$5$维$dp$即可 最后在滚动一下,判一下可行性即可. #include<iostream> #include<c ...

  8. 【动态规划/递推】BZOJ1806[IOI2007]- Miners

    IOI历史上的著名水题,我这种蒟蒻都能写的东西. [思路] 用1.2.3分别代替三种食物,0表示当前矿井没有食物.f[i][a][b][c][d]当前第i个食物,矿1的食物顺序由上至下为a,b:矿2的 ...

  9. 【bzoj1806】[Ioi2007]Miners 矿工配餐 dp

    题目描述 有n个物品,每个都是3种之一.现要将这n个物品分成两个序列,对于每个序列中的每个物品,可以得到 它及它前面相邻的两个物品(不足则取全部)中不同种类的个数 的收益.问最大的总收益. 输入 输入 ...

随机推荐

  1. [转]CNN目标检测(一):Faster RCNN详解

    https://blog.csdn.net/a8039974/article/details/77592389 Faster RCNN github : https://github.com/rbgi ...

  2. Hyperledger Fabric1.0.0搭建

    系统环境: 阿里云新装的Centos7.2 yum -y update yum install -y openssl openssl-devel gcc gcc-c++ zlib zlib-devel ...

  3. Pytorch自定义数据库

    1)前言 虽然torchvision.datasets中已经封装了好多通用的数据集,但是我们在使用Pytorch做深度学习任务的时候,会面临着自定义数据库来满足自己的任务需要.如我们要训练一个人脸关键 ...

  4. shell系统检测->

    系统状态检测脚本练习 1-> 查看磁盘状态 思路:查看磁盘/当前使用状态,如果使用率超过80%则报警发邮件 1.获取磁盘当前使用的值 df -h|grep /$ 2.从获取到的值中提取出,对应的 ...

  5. linux终端操作快捷键

    终端操作快捷键: 新建家目录下终端窗口:Ctrl+Alt+t在当期当前路径下新建终端窗口:Ctrl+Shift+n退出终端窗口:Ctrl+Shift+q 多个终端窗口之间相互切换:Tab+Alt 终端 ...

  6. 【前端开发】localStorage的用法

    localStorage.setItem("name","value")  //存储name的值 var type = localStorage.getItem ...

  7. 使用html+css+js实现日历与定时器,看看今天的日期和今天剩余的时间。

    使用html+css+js实现日历与定时器,看看今天的日期和今天剩余的时间. 效果图: 哎,今天就又这么过去了,过的可真快 . 代码如下,复制即可使用: <!DOCTYPE html> & ...

  8. Centos之常见目录作用介绍

    我们先切换到系统根目录 / 看看根目录下有哪些目录 [root@localhost ~]# cd / [root@localhost /]# ls bin   dev  home  lib64  mn ...

  9. (一)问候 HtmlUnit

    第一节: HtmlUnit 简介 htmlunit 是一款开源的java 页面分析工具,读取页面后,可以有效的使用htmlunit分析页面上的内容.项目可以模拟浏览器运行,被誉为java浏览器的开源实 ...

  10. SQL Server中删除表中重复数据

    方法一:利用游标,但要注意主字段或标识列 declare @max integer,@id integer open cur_rows fetch cur_rows into @id,@max beg ...