CF-31E-TV Game(dp)

游戏开发策略与最优解法
本文探讨了在特定电视游戏中两位玩家通过最优策略获得最高奖励的方法,详细介绍了多阶段决策过程,包括从后向前整序,使用动态规划解决复杂问题,并提供了实现代码示例。

E - TV Game
Crawling in process... Crawling failed Time Limit:2000MS Memory Limit:262144KB 64bit IO Format:%I64d & %I64u

Description

There is a new TV game on BerTV. In this game two players get a number A consisting of 2n digits. Before each turn players determine who will make the next move. Each player should make exactly n moves. On it's turn i-th player takes the leftmost digit of A and appends it to his or her number Si. After that this leftmost digit is erased from A. Initially the numbers of both players (S1 and S2) are «empty». Leading zeroes in numbers A, S1, S2 are allowed. In the end of the game the first player gets S1 dollars, and the second gets S2 dollars.

One day Homer and Marge came to play the game. They managed to know the number A beforehand. They want to find such sequence of their moves that both of them makes exactly n moves and which maximizes their total prize. Help them.

Input

The first line contains integer n (1 ≤ n ≤ 18). The second line contains integer A consisting of exactly 2n digits. This number can have leading zeroes.

Output

Output the line of 2n characters «H» and «M» — the sequence of moves of Homer and Marge, which gives them maximum possible total prize. Each player must make exactly n moves. If there are several solutions, output any of them.

Sample Input

Input
2
1234
Output
HHMM
Input
2
9911
Output
HMHM


思路:每一位不是给W就是给H,很明显是多阶段决策。

           有点难想的是从后往前整,先整小的为逐次分配出最优结果。

          f[x][y]表示给x位给W,y位给H所得到结果的最优值。

          f[x][y]=max(f[x-1][y]+该位给W所增加的值,f[x][y-1]+该位给H所增加的值)

          最后把路径输出即可。

失误:DP做的多了,感觉有的有点畏惧感,有点思路,却挺怕不对而花很多时间来整这思路。╮(╯▽╰)╭

          这题在大牛眼中应该是水DP吧,不知道我这只菜鸟啥时候也能成大牛啊。

#include<iostream>
#include<cstring>
using namespace std;
const int mm=100;
long long f[mm][mm],w[mm];
int main()
{
  int m;char s[mm];
  while(cin>>m>>s)
  { w[1]=1;
    for(int i=2;i<=18;i++)
      w[i]=w[i-1]*10;
    memset(f,0,sizeof(f));
    for(int i=0;i<=m;i++)
      for(int j=0;j<=m;j++)
    { long long z=s[m+m-i-j]-'0';
      if(i)f[i][j]=f[i-1][j]+w[i]*z;
      if(j)f[i][j]=f[i][j]>f[i][j-1]+w[j]*z?f[i][j]:f[i][j-1]+w[j]*z;
    }
    for(int i=m,j=m;i>0||j>0;)
    { long long z=s[m+m-i-j]-'0';
      if(i&&f[i][j]-f[i-1][j]==z*w[i])
        {--i;cout<<"H";
        }
      else {--j;cout<<"M";}
    }
    cout<<"\n";
  }
}





转载于:https://www.cnblogs.com/nealgavin/archive/2013/02/15/3205973.html

## 01、数据介绍 数据整理全国30个省份制造业细分行业产值,能够反映一定时期内细分行业工业生产总规模和总水平的指标,是计算工业生产发展速度和主要比例关系、计算工业产品销售率等其他经济指标的基础。先进制造业数据可用于两业融合测算。 数据名称:全国30省-制造业细分行业产值/先进制造业细分行业产值数据 数据年份:2001-2022年 ## 02、数据指标 农副食品加工业;食品制造业;饮料制造业;烟草制造业;纺织业;纺织服装鞋帽制造业;皮革、毛皮、羽毛(绒)极其制品业;木材加工及木、竹、藤、棕、草制品业;家具制造业;造纸及纸制品业;印刷业和记录媒介的复制;文教体育用品制造业;石油加工、炼焦及核燃料加工业化;学原料及化学制品制造业;医药制造业;化学纤维制造业;橡胶和塑料制品制品业;非金属矿物制品业;黑色金属冶炼及压延加工业;有色金属冶炼及压延加工业;金属制品业;通用设备制造业;专用设备制造业;汽车制造业;其他运输设备制造业;电气机械及器材制造业;通信设备计算机及其他电子设备制造业;仪器仪表及文化、办公用机械制造业;其他制造业;废弃资源和废旧材料回收加工业;制造业总产值 印刷业和记录媒介的复制;石油加工、炼焦及核燃料加工业;化学原料及化学制品制造业;医药制造业;橡胶和塑料制品制品业;非金属矿物制品业;黑色金属冶炼及压延加工业;有色金属冶炼及压延加工业;金属制品业;通用设备制造业;专用设备制造业;汽车制造业;其他运输设备制造业;电气机械及器材制造业;通信设备计算机及其他电子设备制造业;仪器仪表及文化、办公用机械制造业;废弃资源和废旧材料回收加工业
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值