当前位置:网站首页>P1077 [NOIP2012 普及组] 摆花
P1077 [NOIP2012 普及组] 摆花
2022-06-22 05:23:00 【hhyy_d】


这是我差不多是第一次从暴力递归优化到记忆化搜索,期间经历了一定的曲折。
解题过程:
1.看到这个题首先能够想到,每次摆花,可以选择和前面一样的花(如果前面的花还有剩余的话),也可以选择摆下一种花,于是最初的暴力递归算法就出来了。此方法提交有30分。
#include<bits/stdc++.h>
#define MAX 101
using namespace std;
int n,m;
int num[MAX];//第i种花的数量
int res=0;
int sum(int temp)
{
//剩余所有花的数量
int all=0;
for(int i=temp;i<n;i++)
{
all+=num[i];
}
return all;
}
void fun(int index,int temp)//temp表示花的编号
{
if(index>=m)
{
res+=1;
res%=(1000007);
return;
}
if(temp>=n||n-index>sum(temp))//如果当前选择花的序号已经超过了种类数,那么就停止
{
return;
}
if(num[temp]>0)
{
num[temp]-=1;
fun(index+1,temp);
num[temp]+=1;//注意回溯
}
fun(index,temp+1);
}
int main()
{
cin>>n>>m;
for(int i=0;i<n;i++)
{
cin>>num[i];
}
fun(0,0);
cout<<res<<endl;
return 0;
}
2.将上述的暴力递归改为记忆化搜索,按照之前写的方法进行改动。提交后发现只有10分,原因在于:这里的变量还应该包含第temp种花的数量,因为前面使用到了回溯,记忆化之后无法将变量返回,所以要把这个变量也当成一个参数传入。得到第三个代码。
#include<bits/stdc++.h>
#define MAX 101
using namespace std;
int n,m;
int num[MAX];//第i种花的数量
int res=0;
int arr[MAX][MAX];
void init()
{
for(int i=0;i<MAX;i++)
{
for(int j=0;j<MAX;j++)
{
arr[i][j]=-1;
}
}
}
int sum(int temp)
{
//剩余所有花的数量
int all=0;
for(int i=temp;i<n;i++)
{
all+=num[i];
}
return all;
}
int fun(int index,int temp)
{
if(arr[index][temp]!=-1)
{
return arr[index][temp];
}
if(index>=m)
{
arr[index][temp]=1;
return arr[index][temp];
}
if(temp>=n||n-index>sum(temp))
{
arr[index][temp]=0;
return arr[index][temp];
}
int ways = 0;
if(num[temp]>0)
{
num[temp]-=1;
ways+=fun(index+1,temp);
num[temp]+=1;
}
ways+=fun(index,temp+1);
arr[index][temp]=ways;
return arr[index][temp];
}
int main()
{
cin>>n>>m;
for(int i=0;i<n;i++)
{
cin>>num[i];
}
init();
int ways = fun(0,0);
cout<<ways-2<<endl;
return 0;
}
3.将第temp种花的数量传入之后得到以下代码,注意方法要取余,不然只有50分:
#include<bits/stdc++.h>
#define MAX 102
using namespace std;
int n,m;
int num[MAX];//第i种花的数量
int res=0;
int arr[MAX][MAX][MAX];
void init()
{
for(int i=0;i<MAX;i++)
{
for(int j=0;j<MAX;j++)
{
for(int k=0;k<MAX;k++)
{
arr[i][j][k]=-1;
}
}
}
}
int sum(int temp,int num_temp)
{
//剩余所有花的数量
int all=num[temp]-num_temp;
for(int i=temp+1;i<n;i++)
{
all+=num[i];
}
return all;
}
long long fun(int index,int temp,int num_temp)//num_temp表示使用了第temp号花多少盆
{
if(arr[index][temp][num_temp]!=-1)
{
return arr[index][temp][num_temp];
}
if(index>=m)
{
arr[index][temp][num_temp]=1;
return arr[index][temp][num_temp];
}
if(temp>=n||(n-index)>sum(temp,num_temp))//超过了n种花或者剩余所有花的总数都比要摆的花小
{
arr[index][temp][num_temp]=0;
return arr[index][temp][num_temp];
}
long long ways = 0;
if(num[temp]-num_temp>0)
{
ways+=(fun(index+1,temp,num_temp+1)%1000007);
//num[temp]+=1;
}
ways+=(fun(index,temp+1,0)%1000007);
ways%=1000007;
arr[index][temp][num_temp]=ways;
return arr[index][temp][num_temp];
}
int main()
{
cin>>n>>m;
for(int i=0;i<n;i++)
{
cin>>num[i];
}
init();
long long ways = fun(0,0,0);
cout<<ways<<endl;
return 0;
}

总结一下就是:要将所有的变量找齐,判断是哪些值在影响答案变化。
边栏推荐
- JedisSentinel 工具类
- DTS迁移秘籍-Oracle篇
- C#中Cookie设置与读取
- 1108. Defanging an IP Address
- 【云原生】2.2 kubeadm创建集群
- Talk about MySQL's locking rule "hard hitting MySQL series 15"
- Reconstructing thinking series 2-functions and variables
- 毕业季 | 新的开始,不说再见
- 2022 welder (primary) new version test questions and welder (primary) free test questions
- 新建本地内容上传至码云分支
猜你喜欢

The prediction made ten years ago by the glacier has now been realized by Ali, which is very shocking

This is a picture

Kubernetes——部署应用到集群中

2022 a special equipment related management (elevator) examination data and a special equipment related management (elevator) analysis
Please, use three JS make 2D pictures have 3D effect cool, OK

Graduation season | a new start, no goodbye

在Vs Code中搭建JSP开发环境

How much is London gold

图扑软件2D与2.5D案例合集|智慧园区、数据中心、SMT 生产线...

Lottie components make animation easier
随机推荐
《MATLAB 神经网络43个案例分析》:第28章 决策树分类器的应用研究——乳腺癌诊断
Contents of 2022 tea master (intermediate) examination and tea master (intermediate) examination
使用keytool从.jks文件获取SSL certificate
Restframework read and non read sequence processing
1108. Defanging an IP Address
Storage mode and lifetime of C language variables
2022 Shenzhen Futian District specialized special new small giant enterprise application conditions, with a subsidy of 500000 yuan
DeformConv
北京密云区知识产权贯标的好处,补贴5-10万
新手开店货源怎么找,怎么找到优质货源?
A piece of code to solve the problem of automatic disconnection of Google colab
Kubernetes - bare metal cluster environment
What is cloud hosting and what are its advantages?
The difference between iqueryable and IEnumerable
Kubernetes - deploy application to cluster
Fancy optimizer finishing
7. Gateway global filter
冰河十年前的预测如今被阿里实现了,非常震撼
Flynk deployment mode summary
89---狄拉克 delta 函数