Codeforces #803E: Roma and Pokers 题解
来源:互联网 发布:阿里云汽车荣威rx5 编辑:程序博客网 时间:2024/06/07 23:37
这题显然的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 0
- Codeforces #803E: Roma and Pokers 题解
- codeforces 803E Roma and Poker (dp)
- Codeforces 803E Roma and Poker【Dp+记录路径】
- cf Educational Codeforces Round 20 E. Roma and Poker
- Codeforce 803E. Roma and Poker(dp)
- codeforces 262B Roma and Changing Signs
- Codeforces 567E President and Roads 题解
- CodeForces 815E Karen and Neighborhood题解
- Codeforces Round #160 (Div. 2)---A. Roma and Lucky Numbers
- Codeforces 492E Vanya and Field 题解&代码
- Codeforces803E Roma and Poker
- A. Roma and Lucky Numbers
- B. Roma and Changing Signs
- ICM Technex 2017 and Codeforces Round #400 (Div. 1 + Div. 2, combined) 题解(A-E)
- CodeForces 748E. Santa Claus and Tangerines 题解(递推or二分)
- codeforces Sereja and Dima 题解
- Codeforces 165E Compatible Numbers 题解&代码
- Codeforces 7E Defining Macros 题解
- JZOJ100045. 【NOIP2017提高A组模拟7.13】好数
- Sublime Text2.0.2注册码-亲测可用
- 2017.7.12 确定了方向,大数据。
- 8步助你开源硬件起步!
- SQL 语句(二)--------------数据查询(简单查询)
- Codeforces #803E: Roma and Pokers 题解
- Struts2中访问Servlet中的API(ActionContext,ServletActionContext)两种方式
- 线段树求和
- 《GPU高性能编程 CUDA实战》(CUDA By Example)读书笔记
- 友元函数
- qml与c++的简单实例,Connections信号连接
- python爬虫
- JVM内存泄露与内存溢出的区别
- c++字符串读入写入小结