ccf201809_4,再卖菜

一开始列了不等式,然后开始递推,结果只有10分。。。后来看了一个答案说用动态规划,代码看不懂…然后看了另一个答案用暴搜+记忆化搜索,欸这个可以,于是写出来了…感觉确实没什么难度,用递归的思想其实就一路往下推就可以,主要是怎么写出来。

#include<cstdio>
#include <cstring>
#include <algorithm>

using namespace std;


int n;
int a[305];
int x[305];

bool f[305][305][305];

void dfs(int day,int lastx,int curx){
    if(lastx <= 0||curx <= 0)
    {
        return;
    }
    if(f[day][lastx][curx]){
        return;
    }
    f[day][lastx][curx] = true;

    if(day==n)
    {
        if((x[day-1]+x[day])/2 == a[day])
        {
            for(int i = 1;i<=n;i++)
            {
                printf("%d ",x[i]);
            }
            exit(0);
        }
    }else{
        x[day+1]=a[day]*3-lastx-curx;
        dfs(day+1,x[day],x[day+1]);

        x[day+1]=a[day]*3-lastx-curx+1;
        dfs(day+1,x[day],x[day+1]);

        x[day+1]=a[day]*3-lastx-curx+2;
        dfs(day+1,x[day],x[day+1]);
    }
}

int main() {
    scanf("%d",&n);
    for(int i =1;i<=n;i++)
    {
        scanf("%d",&a[i]);

    }
    for(int i = 1;i<=a[1]*2;i++)
    {
        x[1] = i;x[2] = a[1]*2-x[1];
        dfs(2,x[1],x[2]);

        x[1] = i;x[2] = a[1]*2+1-x[1];
        dfs(2,x[1],x[2]);
    }

    return 0;
}

在这里插入图片描述

2019年9月4日更新

#include <bits/stdc++.h>
using namespace std;

int n;
int a[301];
bool undo[301][301][301];
int x[301];

void over(){
    for(int i = 0 ;i<n;i++)
    {
        printf("%d ",x[i]);
    }
    exit(0);
}

void dfs(int day,int cur,int next){
    if(cur <=0 || next <= 0)
    {
        return;
    }
    if(undo[day][cur][next]){
        return;
    }

    x[day] = cur;

    if(day == n-2)
    {
        if((cur+next)/2 == a[n-1]){
            x[day+1] = next;
            over();
        }
    }

    if(day<n-2){

        for(int i = 0;i<3;i++)
        {
            dfs(day+1,next,a[day+1]*3+i-cur-next);
            undo[day][cur][next] = true;
        }
    }
}


int main(){
    scanf("%d",&n);
    for(int i = 0;i<n;i++)
    {
        scanf("%d",&a[i]);
    }

    for(int i = 1;i<=a[0]*2;i++){
        dfs(0,i,a[0]*2-i);
    }

    for(int i = 1;i<=a[0]*2+1;i++){
        dfs(0,i,a[0]*2+1-i);
    }


    return 0;
}



/**
8
2 2 1 3 4 9 10 13

*/
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值