Free Candies - UVa 10118 dp

本文介绍了一个名为FreeCandies的游戏算法问题,玩家需要通过特定的规则收集糖果,目标是在游戏结束时获得尽可能多的糖果对。文章详细解释了游戏规则,并提供了一段AC代码实现。

Problem B. Free Candies 

The Problem

Little Bob is playing a game. He wants to win some candies in it - as many as possible.

There are 4 piles, each pile contains N candies. Bob is given a basket which can hold at most 5 candies. Each time, he puts a candy at the top of one pile into the basket, and if there're two candies of the same color in it ,he can take both of them outside the basket and put them into his own pocket. When the basket is full and there are no two candies of the same color, the game ends. If the game is played perfectly, the game will end with no candies left in the piles.

For example, Bob may play this game like this (N=5):

Step1Initial PilesStep2Take one from pile #2
PilesBasketPocketPilesBasketPocket
1 2 3 4
1 5 6 7
2 3 3 3
4 9 8 6
8 7 2 1
nothingnothing
1   3 4
1 5 6 7
2 3 3 3
4 9 8 6
8 7 2 1
2nothing
Step3Take one from pile #2Step4Take one from pile #3
PilesBasketPocketPilesBasketPocket
1   3 4
1   6 7
2 3 3 3
4 9 8 6
8 7 2 1
2 5nothing
1     4
1   6 7
2 3 3 3
4 9 8 6
8 7 2 1
2 3 5nothing
Step5Take one from pile #2Step6put two candies into his pocket
PilesBasketPocketPilesBasketPocket
1     4
1   6 7
2   3 3
4 9 8 6
8 7 2 1
2 3 3 5nothing
1     4
1   6 7
2   3 3
4 9 8 6
8 7 2 1
2 5a pair of 3

Note that different numbers indicate different colors, there are 20 kinds of colors numbered 1..20.

'Seems so hard...'Bob got very much puzzled. How many pairs of candies could he take home at most?

The Input

The input will contain no more than 10 test cases. Each test case begins with a line containing a single integer n(1<=n<=40) representing the height of the piles. In the following n lines, each line contains four integers xi1,xi2,xi3,xi4 (in the range 1..20). Each integer indicates the color of the corresponding candy. The test case containing n=0 will terminate the input, you should not give an answer to this case.

The Output

Output the number of pairs of candies that the cleverest little child can take home. Print your answer in a single line for each test case.

Sample Input

5
1 2 3 4
1 5 6 7
2 3 3 3
4 9 8 6
8 7 2 1
1
1 2 3 4
3
1 2 3 4
5 6 7 8
1 2 3 4
0

Sample Output

8
0
3

题意:四列数字只能每列从上往下拿到篮子里,篮子里有相同的两个数字就归你所有,且篮子里最多有5个数字,问你最后能拿到多少对。

思路:f[a][b][c][d]表示在四列分别拿abcd个数的情况是否成立,如果成立,vector<int> dp[a][b][c][d]记录里面还有什么数字。另外我的代码比较麻烦,可以化成一段的,但是我懒得去做了…………反正四段也是一样的。

AC代码如下:

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
using namespace std;
vector<int> dp[45][45][45][45];
int f[45][45][45][45],num[45][5];
int main()
{ int n,m,a,b,c,d,i,j,k,size1,size2,ans;
  while(~scanf("%d",&n) && n)
  { for(i=1;i<=n;i++)
     for(j=1;j<=4;j++)
      scanf("%d",&num[i][j]);
    memset(f,0,sizeof(f));
    ans=0;
    f[0][0][0][0]=1;
    for(a=0;a<=n;a++)
     for(b=0;b<=n;b++)
      for(c=0;c<=n;c++)
       for(d=0;d<=n;d++)
        if(f[a][b][c][d]==1)
        { size1=dp[a][b][c][d].size();
          ans=max(ans,a+b+c+d-size1);

          if(a<n && f[a+1][b][c][d]==0)
          { dp[a+1][b][c][d].clear();
            for(i=0;i<size1;i++)
             if(dp[a][b][c][d][i]!=num[a+1][1])
              dp[a+1][b][c][d].push_back(dp[a][b][c][d][i]);
            size2=dp[a+1][b][c][d].size();
            if(size1==size2)
            { if(size1<4)
              { dp[a+1][b][c][d].push_back(num[a+1][1]);
                f[a+1][b][c][d]=1;
              }
            }
            else
             f[a+1][b][c][d]=1;
          }

          if(b<n && f[a][b+1][c][d]==0)
          { dp[a][b+1][c][d].clear();
            for(i=0;i<size1;i++)
             if(dp[a][b][c][d][i]!=num[b+1][2])
              dp[a][b+1][c][d].push_back(dp[a][b][c][d][i]);
            size2=dp[a][b+1][c][d].size();
            if(size1==size2)
            { if(size1<4)
              { dp[a][b+1][c][d].push_back(num[b+1][2]);
                f[a][b+1][c][d]=1;
              }
            }
            else
             f[a][b+1][c][d]=1;
          }

          if(c<n && f[a][b][c+1][d]==0)
          { dp[a][b][c+1][d].clear();
            for(i=0;i<size1;i++)
             if(dp[a][b][c][d][i]!=num[c+1][3])
              dp[a][b][c+1][d].push_back(dp[a][b][c][d][i]);
            size2=dp[a][b][c+1][d].size();
            if(size1==size2)
            { if(size1<4)
              { dp[a][b][c+1][d].push_back(num[c+1][3]);
                f[a][b][c+1][d]=1;
              }
            }
            else
             f[a][b][c+1][d]=1;
          }

          if(d<n && f[a][b][c][d+1]==0)
          { dp[a][b][c][d+1].clear();
            for(i=0;i<size1;i++)
             if(dp[a][b][c][d][i]!=num[d+1][4])
              dp[a][b][c][d+1].push_back(dp[a][b][c][d][i]);
            size2=dp[a][b][c][d+1].size();
            if(size1==size2)
            { if(size1<4)
              { dp[a][b][c][d+1].push_back(num[d+1][4]);
                f[a][b][c][d+1]=1;
              }
            }
            else
             f[a][b][c][d+1]=1;
          }
        }
    printf("%d\n",ans/2);
  }
}



【顶级EI完整复现】【DRCC】考虑N-1准则的分布鲁棒机会约束低碳经济调度(Matlab代码实现)内容概要:本文介绍了名为《【顶级EI完整复现】【DRCC】考虑N-1准则的分布鲁棒机会约束低碳经济调度(Matlab代码实现)》的技术资源,聚焦于电力系统中低碳经济调度问题,结合N-1安全准则与分布鲁棒机会约束(DRCC)方法,提升调度模型在不确定性环境下的鲁棒性和可行性。该资源提供了完整的Matlab代码实现,涵盖建模、优化求解及仿真分析全过程,适用于复杂电力系统调度场景的科研复现与算法验证。文中还列举了大量相关领域的研究主题与代码资源,涉及智能优化算法、机器学习、电力系统管理、路径规划等多个方向,展示了广泛的科研应用支持能力。; 适合人群:具备一定电力系统、优化理论和Matlab编程基础的研究生、科研人员及从事能源调度、智能电网相关工作的工程师。; 使用场景及目标:①复现高水平期刊(如EI/SCI)关于低碳经济调度的研究成果;②深入理解N-1安全约束与分布鲁棒优化在电力调度中的建模方法;③开展含新能源接入的电力系统不确定性优化研究;④为科研项目、论文撰写或工程应用提供可运行的算法原型和技术支撑。; 阅读建议:建议读者结合文档提供的网盘资源,下载完整代码与案例数据,按照目录顺序逐步学习,并重点理解DRCC建模思想与Matlab/YALMIP/CPLEX等工具的集成使用方式,同时可参考文中列出的同类研究方向拓展研究思路。
内容概要:本文详细介绍了一个基于MATLAB实现的电力负荷预测项目,采用K近邻回归(KNN)算法进行建模。项目从背景意义出发,阐述了电力负荷预测在提升系统效率、优化能源配置、支撑智能电网和智慧城市建设等方面的重要作用。针对负荷预测中影响因素多样、时序性强、数据质量差等挑战,提出了包括特征工程、滑动窗口构造、数据清洗与标准化、K值与距离度量优化在内的系统性解决方案。模型架构涵盖数据采集、预处理、KNN回归原理、参数调优、性能评估及工程部署全流程,并支持多算法集成与可视化反馈。文中还提供了MATLAB环境下完整的代码实现流程,包括数据加载、归一化、样本划分、K值选择、模型训练预测、误差分析与结果可视化等关键步骤,增强了模型的可解释性与实用性。; 适合人群:具备一定MATLAB编程基础和机器学习基础知识,从事电力系统分析、能源管理、智能电网或相关领域研究的研发人员、工程师及高校师生;适合工作1-3年希望提升实际项目开发能力的技术人员; 使用场景及目标:①应用于短期电力负荷预测,辅助电网调度与发电计划制定;②作为教学案例帮助理解KNN回归在实际工程中的应用;③为新能源接入、需求响应、智慧能源系统提供数据支持;④搭建可解释性强、易于部署的轻量级预测模型原型; 阅读建议:建议结合MATLAB代码实践操作,重点关注特征构造、参数调优与结果可视化部分,深入理解KNN在时序数据中的适应性改进方法,并可进一步拓展至集成学习或多模型融合方向进行研究与优化。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值