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仅仅黑鼠 龙和王妃轮流从袋子里抓老鼠. 谁先抓到白色老师谁就赢 ...
随机推荐
- 通过python操作GeoLite2-City.mmdb库将nginx日志访问IP转换为城市写入数据库
通过python操作GeoLite2-City.mmdb库将nginx日志写入数据库 # 创建存放nginx日志的表accesslog2 CREATE TABLE `accesslog2` ( `id ...
- 【原创】大叔经验分享(32)docker挂载文件修改生效
docker经常需要挂载文件到容器中,比如启动nginx # docker run -d --name test_nginx -v /tmp/nginx.conf:/etc/nginx/nginx.c ...
- Appium新版本不再支持ByName定位了怎么办
appium版本在1.5以后就不再支持ByName的定位,本文章仅介绍在appium1.6.3/1.6.4/1.6.5版本下如何支持ByName定位,适用于安卓.在使用appium1.5之后的版本时, ...
- Github+HEXO FATAL bad indentation of a mapping entry at line 84
当配置玩_config.yml文件时,执行hexo g -d时出现错误如下: $ hexo g -d FATAL can not read a block mapping entry; a multi ...
- 加载UI工程的csb,以及纹理缓存情况
以plist+PNG模式加载csb,并播放UI工程做的动画,用法如下 local Layer = cc.CSLoader:createNode("res/yk/interface/loadi ...
- 获取ScrollView ListView的当前位置的百分比
找不到官方的API,就自己写了一下,下面是自己写的函数 --获取滚动层当前位置的百分比 function GetScrollViewPercent(scrollView) if scrollView ...
- Confluence 6 内存使用和需求和一些问题
系统备份和恢复 Confluence 的备份和恢复是与数据库中数据量的大小有关.这个操作可能会对 Confluence 的性能产生很多关键性的影响并且大量消耗内存.如果你在 Confluence 的 ...
- Confluence 6 重新获得站点备份文件
Confluence 将会创建备份,同时压缩 XML 文件后存储熬你的 <home-directory>/backups> 目录中.你需要自己访问你安装的 Confluence 服务 ...
- Confluence 6 安装 SQL Server
如果你还没有在安装可以连接的 Microsoft SQL Server 数据库,请先下载后进行安装.请参考 MSDN 上 Installation for SQL Server 的指南. 有关授权模 ...
- 扇形多级菜单可配置Demo
预览效果 领导是想把这个做成复选框,所以做成了可以多选的... Demo下载:https://github.com/zhangzn3/arc-menu