Codeforces #803E: Roma and Pokers 题解

这题显然的dp

设dp[i][j]表示当前考虑到第i个位置,胜场比负场多j个能否做到,同时记录转移的坐标方便打印

因为j可能是负的,所以要加一个base

如果当前位是W:dp[i][j]=dp[i-1][j-1]

如果当前位是L:dp[i][j]=dp[i][j+1]

如果当前位是D:dp[i][j]=dp[i-1][j]

如果当前位是?:上面三种情况都考虑一遍

注意:转移的时候abs(j)是不能为k的

#include <cstdio>
#include <iostream>
#include <cstring>
#include <string>
#include <cmath>
#include <algorithm>
#include <cstdlib>
#include <utility>
#include <map>
#include <stack>
#include <set>
#include <vector>
#include <queue>
#include <deque>
#define x first
#define y second
#define mp make_pair
#define pb push_back
#define LL long long
#define Pair pair<bool,pair<int,int> >
#define LOWBIT(x) x & (-x)
using namespace std;

const int zero_stand=1500;
const int MOD=1e9+7;
const int INF=0x7ffffff;
const int magic=348;

Pair dp[1048][3048];
int n,k;
char s[1048];

void print(int x,int y)
{
	if (dp[x][y].y.y!=-1) print(x-1,dp[x][y].y.y);
	if (dp[x][y].y.x==1) printf("W");
	if (dp[x][y].y.x==2) printf("L");
	if (dp[x][y].y.x==3) printf("D");
}

int main ()
{
	int i,j;
	scanf("%d%d%s",&n,&k,s+1);
	for (i=1;i<=n;i++)
		for (j=zero_stand-k-10;j<=zero_stand+k+10;j++)
			dp[i][j]=mp(false,mp(0,0));
	if (s[1]=='W') dp[1][zero_stand+1]=mp(true,mp(1,-1));
	if (s[1]=='L') dp[1][zero_stand-1]=mp(true,mp(2,-1));
	if (s[1]=='D') dp[1][zero_stand]=mp(true,mp(3,-1));
	if (s[1]=='?')
	{
		dp[1][zero_stand+1]=mp(true,mp(1,-1));
		dp[1][zero_stand-1]=mp(true,mp(2,-1));
		dp[1][zero_stand]=mp(true,mp(3,-1));
	}
	for (i=2;i<=n;i++)
		for (j=zero_stand-k;j<=zero_stand+k;j++)
		{
			if ((j==zero_stand-k || j==zero_stand+k) && i!=n) continue;
			if (s[i]=='W')
				if (dp[i-1][j-1].x && j-1!=zero_stand-k)
				{
					dp[i][j]=mp(true,mp(1,j-1));
					continue;
				}
			if (s[i]=='L')
				if (dp[i-1][j+1].x && j+1!=zero_stand+k)
				{
					dp[i][j]=mp(true,mp(2,j+1));
					continue;
				}
			if (s[i]=='D')
				if (dp[i-1][j].x)
				{
					dp[i][j]=mp(true,mp(3,j));
					continue;
				}
			if (s[i]=='?')
			{
				if (dp[i-1][j-1].x && j-1!=zero_stand-k)
				{
					dp[i][j]=mp(true,mp(1,j-1));
					continue;
				}
				if (dp[i-1][j+1].x && j+1!=zero_stand+k)
				{
					dp[i][j]=mp(true,mp(2,j+1));
					continue;
				}
				if (dp[i-1][j].x)
				{
					dp[i][j]=mp(true,mp(3,j));
					continue;
				}
			}
		}
	if (!dp[n][zero_stand-k].x && !dp[n][zero_stand+k].x)
	{
		printf("NO\n");
		return 0;
	}
	if (dp[n][zero_stand-k].x) print(n,zero_stand-k); else print(n,zero_stand+k);
	printf("\n");
	return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值