洛谷2530(codevs2098)化工厂装箱员
题目:https://www.luogu.org/problemnew/show/P2530
dp或搜索。
dp做法就是 当前值+1 转移到 当前某一维为0、位置前进了c位 的地方。但没写。
写了搜索的方法。细节众多,而且RE地莫名其妙!
搜索要注意记忆化。
特别奇怪的细节:代码中用注释(d数组)代替t1 t2 t3的话就会WA。
子函数中传参如果写成c[ ],就是不确定大小,于是不能用memcpy了。
AC代码:
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
const int INF=;
int n,col[],tmp[],f[][][][];
char ch;
int num(char ch)
{
if(ch=='A')return ;
if(ch=='B')return ;
if(ch=='C')return ;
}
int dfs(int c[],int now)
{
if(f[now][c[]][c[]][c[]])return f[now][c[]][c[]][c[]];
if(!c[]&&!c[]&&!c[])return ;
int num=INF,j=;
int t1=c[],t2=c[],t3=c[];
// int d[5]={0};
// memcpy(d,c,sizeof c);
// d[1]=c[1];d[2]=c[2];d[3]=c[3];
for(int i=;i<=;i++)
{
if(!c[i])continue;
int t=c[i];
c[i]=;
for(j=now;j<now+t&&j<=n;j++)
c[col[j]]++;
num=min(num,dfs(c,j));
c[]=t1;c[]=t2;c[]=t3;
// memcpy(c,d,sizeof d);
// c[1]=d[1];c[2]=d[2];c[3]=d[3];
}
return f[now][c[]][c[]][c[]]=num+;
}
int main()
{
scanf("%d",&n);
for(int i=;i<=n;i++)
{
scanf(" %c",&ch);
col[i]=num(ch);
if(i<=)tmp[col[i]]++;
}
printf("%d",dfs(tmp,min(,n+)));
return ;
}
RE+WA+MLE代码(注意判断return 0和记忆化的那两句的位置是在补满10个以后):
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
const int INF=;
int n,col[],tmp[],f[][][][];
char ch;
int num(char ch)
{
if(ch=='A')return ;
if(ch=='B')return ;
if(ch=='C')return ;
}
int dfs(int c[],int now)
{
int k=-c[]-c[]-c[];
for(int i=now;i<now+k&&i<=n;i++)
c[col[i]]++;
if(!c[]&&!c[]&&!c[]&&now>)return ;
if(f[now][c[]][c[]][c[]])return f[now][c[]][c[]][c[]];
int d[]={},num=INF;
// d[1]=c[1];d[2]=c[2];d[3]=c[3];
memcpy(d,c,sizeof c);
for(int i=;i<=;i++)
{
if(!c[i])continue;
c[i]=;
num=min(num,dfs(c,now+k));
c[i]=d[i];
}
return f[now][c[]][c[]][c[]]=num+;
}
int main()
{
scanf("%d",&n);
for(int i=;i<=n;i++)
{
scanf(" %c",&ch);
col[i]=num(ch);
}
printf("%d",dfs(tmp,));
return ;
}
代码2
改了改上边之后的WA代码:
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
const int INF=;
int n,col[],tmp[],f[][][][];
char ch;
int num(char ch)
{
if(ch=='A')return ;
if(ch=='B')return ;
if(ch=='C')return ;
}
int dfs(int c[],int now,int k)
{
// int k=10-c[1]-c[2]-c[3],l=0;
int l=;
for(l=now;l<now+k&&l<=n;l++)
c[col[l]]++;
if(f[now][c[]][c[]][c[]])return f[now][c[]][c[]][c[]];
if(!c[]&&!c[]&&!c[])return ;
int num=INF,t1=c[],t2=c[],t3=c[];
// d[1]=c[1];d[2]=c[2];d[3]=c[3];
// memcpy(d,c,sizeof c);
for(int i=;i<=;i++)
{
if(!c[i])continue;
int t=c[i];
c[i]=;
num=min(num,dfs(c,l,t));
// c[i]=d[i];
c[]=t1;c[]=t2;c[]=t3;
}
return f[now][c[]][c[]][c[]]=num+;
}
int main()
{
scanf("%d",&n);
for(int i=;i<=n;i++)
{
scanf(" %c",&ch);
col[i]=num(ch);
}
printf("%d",dfs(tmp,,));
return ;
}
代码3
洛谷2530(codevs2098)化工厂装箱员的更多相关文章
- 洛谷 P2530 [SHOI2001]化工厂装箱员 解题报告
P2530 [SHOI2001]化工厂装箱员 题目描述 118号工厂是世界唯一秘密提炼锎的化工厂,由于提炼锎的难度非常高,技术不是十分完善,所以工厂生产的锎成品可能会有3种不同的纯度,A:100%,B ...
- 洛谷P2530 [SHOI2001]化工厂装箱员
题目描述 118号工厂是世界唯一秘密提炼锎的化工厂,由于提炼锎的难度非常高,技术不是十分完善,所以工厂生产的锎成品可能会有3种不同的纯度,A:100%,B:1%,C:0.01%,为了出售方便,必须把不 ...
- 洛谷 p2530 化工场装箱员(资源型)
化工场装箱员 https://www.luogu.org/problem/show?pid=2530 118号工厂是世界唯一秘密提炼锎的化工厂,由于提炼锎的难度非常高,技术不是十分完善,所以工厂生产的 ...
- 斜率优化dp学习笔记 洛谷P3915[HNOI2008]玩具装箱toy
本文为原创??? 作者写这篇文章的时候刚刚初一毕业…… 如有错误请各位大佬指正 从例题入手 洛谷P3915[HNOI2008]玩具装箱toy Step0:读题 Q:暴力? 如果您学习过dp 不难推出d ...
- 化工厂装箱员(洛谷 P2530)
题目描述 118号工厂是世界唯一秘密提炼锎的化工厂,由于提炼锎的难度非常高,技术不是十分完善,所以工厂生产的锎成品可能会有3种不同的纯度,A:100%,B:1%,C:0.01%,为了出售方便,必须 ...
- 化工厂装箱员 洛谷 p2530
题目描述 118号工厂是世界唯一秘密提炼锎的化工厂,由于提炼锎的难度非常高,技术不是十分完善,所以工厂生产的锎成品可能会有3种不同的纯度,A:100%,B:1%,C:0.01%,为了出售方便,必须 ...
- Luogu 2530 化工厂装箱员
Written with StackEdit. Description \(118\)号工厂是世界唯一秘密提炼锎的化工厂,由于提炼锎的难度非常高,技术不是十分完善,所以工厂生产的锎成品可能会有\(3\ ...
- [SHOI2001]化工厂装箱员
题目描述 118号工厂是世界唯一秘密提炼锎的化工厂,由于提炼锎的难度非常高,技术不是十分完善,所以工厂生产的锎成品可能会有3种不同的纯度,A:100%,B:1%,C:0.01%,为了出售方便,必须 ...
- [SHOI2001]化工厂装箱员(dp?暴力:暴力)
118号工厂是世界唯一秘密提炼锎的化工厂,由于提炼锎的难度非常高,技术不是十分完善,所以工厂生产的锎成品可能会有3种不同的纯度,A:100%,B:1%,C:0.01%,为了出售方便,必须把不同纯度 ...
随机推荐
- 87. Scramble String *HARD* 动态规划
Given a string s1, we may represent it as a binary tree by partitioning it to two non-empty substrin ...
- Windows环境搭建ElasticSearch 5.*并配置head
前言: ES5*以上版本需要jdk1.8,jdk1.8,jdk1.8.重要的事情说三遍 1.下载ElasticSearch https://www.elastic.co/cn/downloads/el ...
- winRAR显示树树目录
这样 比较方便
- 《Python》 函数进阶和名称空间作用域
函数进阶: 一.动态参数:*args **kwargs *args是元祖形式,接收除去键值对以外的所有参数 # args可以换成任意变量名,约定俗成用args **kwargs接收的只是键值对的参数 ...
- python学习:变量与字符串
counter = # 赋值整型变量 miles = 1000.0 # 浮点型 name = "John" # 字符串 print counter print miles prin ...
- myeclipse10 破解版安装
安装包请看百度云: step1 step2 step3 step4 step5 step6 安装 svn,可以参考: http://www.cnblogs.com/OnlyCT/p/6061134.h ...
- 分享一个使用 vue.js 开发的网站
点我 惠淘党 照着文档和google开发,只花了一个多星期.依赖包如下 { "name": "vue-htd", "version": &q ...
- SIM800C 透传模式
/******************************************************************************* * SIM800C 透传模式 * 说明 ...
- CF1096.F. Inversion Expectation(树状数组)
A permutation of size n is an array of size n such that each integer from 1 to n occurs exactly once ...
- LAMP环境安装实例