Codeforces 354B dp Game with Strings dp
题意并不是在图上走,看了好久才看出来。。
dp[ i ][ mask ]表示从 i 层开始走,起点有mask个, a的个数-b的个数的 最大值或者最小值。
#include<bits/stdc++.h>
#define LL long long
#define LD long double
#define fi first
#define se second
#define mk make_pair
#define PLL pair<LL, LL>
#define PLI pair<LL, int>
#define PII pair<int, int>
#define SZ(x) ((int)x.size())
#define ull unsigned long long using namespace std; const int N = + ;
const int inf = 0x3f3f3f3f;
const LL INF = 0x3f3f3f3f3f3f3f3f;
const int mod = 1e9 + ;
const double eps = 1e-;
const double PI = acos(-); template<class T, class S> inline void add(T& a, S b) {a += b; if(a >= mod) a -= mod;}
template<class T, class S> inline void sub(T& a, S b) {a -= b; if(a < ) a += mod;}
template<class T, class S> inline bool chkmax(T& a, S b) {return a < b ? a = b, true : false;}
template<class T, class S> inline bool chkmin(T& a, S b) {return a > b ? a = b, true : false;} int n;
int dp[ * ][ << ];
bool vis[ * ][ << ];
char Map[][];
int id[][]; struct Point {
int x, y;
}; vector<Point> vc[]; int dfs(int d, int mask, int op) {
if(d == n * - ) return ;
int& ans = dp[d][mask];
if(vis[d][mask]) return ans;
ans = op ? inf : -inf;
int up = SZ(vc[d]);
for(char c = 'a'; c <= 'z'; c++) {
int nxtmask = , sco = ;
if(c == 'a') sco = ;
else if(c == 'b') sco = -;
for(int i = ; i < up; i++) {
if(mask >> i & ) {
int x = vc[d][i].x, y = vc[d][i].y + ;
if(y < n && Map[x][y] == c) {
nxtmask |= << id[x][y];
}
x = vc[d][i].x + , y = vc[d][i].y;
if(x < n && Map[x][y] == c) {
nxtmask |= << id[x][y];
}
}
}
if(nxtmask) {
if(op) chkmin(ans, dfs(d + , nxtmask, op ^ ) + sco);
else chkmax(ans, dfs(d + , nxtmask, op ^ ) + sco);
}
}
vis[d][mask] = true;
return ans;
} int main() {
scanf("%d", &n);
for(int i = ; i < n; i++) scanf("%s", Map[i]);
for(int i = ; i < n; i++) {
int x = i, y = ;
while(x < n && y < n && x >= && y >= ) {
id[x][y] = SZ(vc[i]);
vc[i].push_back(Point{x, y}), x--, y++;
}
}
for(int i = n; i < * n; i++) {
int x = n - , y = i - n + ;
while(x < n && y < n && x >= && y >= ) {
id[x][y] = SZ(vc[i]);
vc[i].push_back(Point{x, y}), x--, y++;
}
}
int judge = dfs(, , );
if(Map[][] == 'a') judge++;
else if(Map[][] == 'b') judge--;
if(judge > ) puts("FIRST");
else if(judge < ) puts("SECOND");
else puts("DRAW");
return ;
} /*
*/
Codeforces 354B dp Game with Strings dp的更多相关文章
- Codeforces 682 D. Alyona and Strings (dp)
题目链接:http://codeforces.com/contest/682/problem/D 给你两个字符串,求两个字符串中顺序k个的相同子串 长度之和.(注意是子串) dp[i][j][k][0 ...
- codeforces 682D D. Alyona and Strings(dp)
题目链接: D. Alyona and Strings time limit per test 2 seconds memory limit per test 256 megabytes input ...
- Codeforces Round #272 (Div. 2) E. Dreamoon and Strings dp
题目链接: http://www.codeforces.com/contest/476/problem/E E. Dreamoon and Strings time limit per test 1 ...
- Codeforces 467C George and Job(DP)
题目 Source http://codeforces.com/contest/467/problem/C Description The new ITone 6 has been released ...
- codeforces 161D Distance in Tree 树形dp
题目链接: http://codeforces.com/contest/161/problem/D D. Distance in Tree time limit per test 3 secondsm ...
- Codeforces 479E. Riding in a Lift (dp + 前缀和优化)
题目链接:http://codeforces.com/contest/479/problem/E 题意: 给定一个启示的楼层a,有一个不能去的楼层b,对于你可以去的下一个楼层必须满足你 ...
- Codeforces 118 D. Caesar's Legions (dp)
题目链接:http://codeforces.com/contest/118/problem/D 有n个步兵和m个骑兵要排成一排,其中连续的步兵不能超过k1个,连续的骑兵不能超过k2个. dp[i][ ...
- Codeforces 710 E. Generate a String (dp)
题目链接:http://codeforces.com/problemset/problem/710/E 加或者减一个字符代价为x,字符数量翻倍代价为y,初始空字符,问你到n个字符的最小代价是多少. d ...
- Codeforces 148D Bag of mice 概率dp(水
题目链接:http://codeforces.com/problemset/problem/148/D 题意: 原来袋子里有w仅仅白鼠和b仅仅黑鼠 龙和王妃轮流从袋子里抓老鼠. 谁先抓到白色老师谁就赢 ...
随机推荐
- 运维与自动化系列③自动化部署基础与shell脚本实现
自动化部署基础与shell脚本实现 关于自动化的基础知识: 1.1:当前代码部署的实现方式: 运维纯手工scp到web服务器纯手工登录git服务器执行git pull或svn服务器执行svn upda ...
- java后台发送请求并获取返回值
项目中需要前端发送请求给后端,而后端需要从另一个平台中取数据然后再透传给前端,通过下述代码将其实现.在此记录一下. package com.autotest.utils; import java.io ...
- hadoop客户端如何配置
Hadoop集群主要是由三部分组成的:主节点.从节点和客户端,即master.slave和client.我们在搭建hadoop集群的时候通常只考虑了主节点和从节点的搭建,却忽略了客户端.当我们搭建完成 ...
- 阿里云服务器ubuntu 配置
由于阿里云的导入自定义 ubuntu 镜像需要开通 OSS 快照是收费的(看着感觉不贵,但是也很麻烦),而且自己已配置好的镜像想导入需要转换格式,还存在不能使用的情况,所以麻烦点直接在阿里云原来的ub ...
- 用docker快速搭建wordpress博客
WordPress是一个非常著名的PHP编写的博客平台,发展到目前为止已经形成了一个庞大的网站平台系统.在WP上有规模庞大的插件和主题,可以帮助我们快速建立一个博客甚至网站. 在Windows上可 ...
- 洛谷P4859 已经没有什么好害怕的了 [DP,容斥]
传送门 思路 大佬都说这是套路题--嘤嘤嘤我又被吊打了\(Q\omega Q\) 显然,这题是要\(DP\)的. 首先思考一下性质: 为了方便,下面令\(k=\frac{n+k}{2}\),即有恰好\ ...
- 银联支付Java开发
注:原来来源于: < 银联支付Java开发 > 银联的demo写的不错,基本上可以直接使用. 首先是对acp_sdk.properties的内容修改,注意这个文件的文件名不能进行修改. ...
- Django项目的创建及基本使用
安装步骤 Django是Python进行Web开发的框架,目前应用比较广泛.使用python进行Web开发,能够很快的搭建所需的项目,可以运用于原型开发,也可以部署到实际的应用环境. 使用Django ...
- 【深度学习】吴恩达网易公开课练习(class1 week4)
概要 class1 week3的任务是实现单隐层的神经网络代码,而本次任务是实现有L层的多层深度全连接神经网络.关键点跟class3的基本相同,算清各个参数的维度即可. 关键变量: m: 训练样本数量 ...
- HTML&javaSkcript&CSS&jQuery&ajax(二)
一.HTML 1.标签<a href="http:www.baidu.com">This is a link </a> <img sr ...