题目: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)化工厂装箱员的更多相关文章

  1. 洛谷 P2530 [SHOI2001]化工厂装箱员 解题报告

    P2530 [SHOI2001]化工厂装箱员 题目描述 118号工厂是世界唯一秘密提炼锎的化工厂,由于提炼锎的难度非常高,技术不是十分完善,所以工厂生产的锎成品可能会有3种不同的纯度,A:100%,B ...

  2. 洛谷P2530 [SHOI2001]化工厂装箱员

    题目描述 118号工厂是世界唯一秘密提炼锎的化工厂,由于提炼锎的难度非常高,技术不是十分完善,所以工厂生产的锎成品可能会有3种不同的纯度,A:100%,B:1%,C:0.01%,为了出售方便,必须把不 ...

  3. 洛谷 p2530 化工场装箱员(资源型)

    化工场装箱员 https://www.luogu.org/problem/show?pid=2530 118号工厂是世界唯一秘密提炼锎的化工厂,由于提炼锎的难度非常高,技术不是十分完善,所以工厂生产的 ...

  4. 斜率优化dp学习笔记 洛谷P3915[HNOI2008]玩具装箱toy

    本文为原创??? 作者写这篇文章的时候刚刚初一毕业…… 如有错误请各位大佬指正 从例题入手 洛谷P3915[HNOI2008]玩具装箱toy Step0:读题 Q:暴力? 如果您学习过dp 不难推出d ...

  5. 化工厂装箱员(洛谷 P2530)

    题目描述 118号工厂是世界唯一秘密提炼锎的化工厂,由于提炼锎的难度非常高,技术不是十分完善,所以工厂生产的锎成品可能会有3种不同的纯度,A:100%,B:1%,C:0.01%,为了出售方便,必须 ...

  6. 化工厂装箱员 洛谷 p2530

    题目描述 118号工厂是世界唯一秘密提炼锎的化工厂,由于提炼锎的难度非常高,技术不是十分完善,所以工厂生产的锎成品可能会有3种不同的纯度,A:100%,B:1%,C:0.01%,为了出售方便,必须 ...

  7. Luogu 2530 化工厂装箱员

    Written with StackEdit. Description \(118\)号工厂是世界唯一秘密提炼锎的化工厂,由于提炼锎的难度非常高,技术不是十分完善,所以工厂生产的锎成品可能会有\(3\ ...

  8. [SHOI2001]化工厂装箱员

    题目描述 118号工厂是世界唯一秘密提炼锎的化工厂,由于提炼锎的难度非常高,技术不是十分完善,所以工厂生产的锎成品可能会有3种不同的纯度,A:100%,B:1%,C:0.01%,为了出售方便,必须 ...

  9. [SHOI2001]化工厂装箱员(dp?暴力:暴力)

    118号工厂是世界唯一秘密提炼锎的化工厂,由于提炼锎的难度非常高,技术不是十分完善,所以工厂生产的锎成品可能会有3种不同的纯度,A:100%,B:1%,C:0.01%,为了出售方便,必须把不同纯度 ...

随机推荐

  1. 使用GAN 进行异常检测——anoGAN,TODO,待用于安全分析实验

    先说实验成功的代码: git clone https://github.com/tkwoo/anogan-keras.git mkdir weights python main.py --mode t ...

  2. Intel daal4py demo运行过程

    daal安装(记得先安装anaconda): git clone https://github.com/IntelPython/daal4py.git cd daal4py conda create ...

  3. learning scala read from console

    控制台输入语句: readInt, readDouble, readByte, readShort, readLong, readChar, readBoolean, readLine example ...

  4. linux create a process

    When the system starts up it is running in kernel mode and there is, in a sense, only one process, t ...

  5. 3.strcpy使用注意(3)

    void test3(char * str1) { if(str1==NULL) { return; } char string[10]; if(strlen(str1)<=10) { strc ...

  6. Java——多线程常见面试题

    body, table{font-family: 微软雅黑} table{border-collapse: collapse; border: solid gray; border-width: 2p ...

  7. sql server中的go

    1. 作用:向 SQL Server 实用工具发出一批 Transact-SQL 语句结束的信号.2. 语法:一批 Transact-SQL 语句GO如Select 1Select 2Select 3 ...

  8. php include,require 主要是向网页中引入文件

  9. C++语言的一些问题

    论坛上看到的有人提出的关于C++的一些问题,真是细致(下面还有回复说他对C++一知半解的),一直以为自己的C++还是可以的,可是看了之后真是内牛满面,为什么自己从来没有想的这么深入,差距真的有这么大吗 ...

  10. memmove 对同一个指针不操作

    memmove 对同一个指针不操作,所以调用memmove之前不用比较两个指针是否相同 void CTestDLLDlg::OnBnClickedButton6() { ; char* data = ...