Codeforces 919 行+列前缀和 树上记忆化搜索(树形DP)
A
B
C
#include <bits/stdc++.h>
#define PI acos(-1.0)
#define mem(a,b) memset((a),b,sizeof(a))
#define TS printf("!!!\n")
#define pb push_back
#define inf 1e9
//std::ios::sync_with_stdio(false);
using namespace std;
//priority_queue<int,vector<int>,greater<int>> que; get min
const double eps = 1.0e-8;
typedef pair<int, int> pairint;
typedef long long ll;
typedef unsigned long long ull;
//const int maxn = 3e5 + 10;
const int maxm = ;
const int turn[][] = {{, }, { -, }, {, }, {, -}};
//priority_queue<int, vector<int>, less<int>> que;
//next_permutation
ll mod = 3e7;
int pop = ;
char f[][];
int dp1[][];
int dp2[][];
string a;
int main()
{
int n, m, k;
cin >> n >> m >> k;
int anser = ;
char ch;
for (int i = ; i <= n; i++)
{
scanf("%s", f[i] + );
}
if (k == )
{
for (int i = ; i <= n; i++)
{
for (int j = ; j <= m; j++)
{
ch = f[i][j];
if (ch == '.')
{
anser++;
}
}
}
cout << anser << endl;
return ;
}
for (int i = ; i <= n; i++)
{
for (int j = ; j <= m; j++)
{
ch = f[i][j];
if (ch == '*')
{
dp1[i][j] = dp2[i][j] = ;
}
else
{
dp1[i][j] = dp1[i - ][j] + ;
dp2[i][j] = dp2[i][j - ] + ;
if (dp1[i][j] >= k)
{
anser++;
}
if (dp2[i][j] >= k)
{
anser++;
}
}
}
}
cout << anser << endl;
return ;
}
D
#include <bits/stdc++.h>
#define PI acos(-1.0)
#define mem(a,b) memset((a),b,sizeof(a))
#define TS printf("!!!\n")
#define pb push_back
#define inf 1e9
//std::ios::sync_with_stdio(false);
using namespace std;
//priority_queue<int,vector<int>,greater<int>> que; get min
const double eps = 1.0e-8;
typedef pair<int, int> pairint;
typedef long long ll;
typedef unsigned long long ull;
//const int maxn = 3e5 + 10;
const int maxm = ;
const int turn[][] = {{, }, { -, }, {, }, {, -}};
//priority_queue<int, vector<int>, less<int>> que;
//next_permutation
ll mod = 3e7;
int pop = ;
vector<int> tree[];
int visit[];
int vis[];
int dp[][];
int ans[];
int gone[];
int num[];
int aim[];
string a;
int anser = ;
int flag = ;
void dfs(int x)
{
gone[x] = ;
int len = tree[x].size();
for (int i = ; i < len; i++)
{
int to = tree[x][i];
if (gone[to] == )
{
cout << - << endl;
exit();
}
else if (gone[to] == )
{
for (int j = ; j < ; j++)
{
int add = num[x] == j;
dp[x][j] = max(dp[x][j], dp[to][j] + add);
}
}
else
{
dfs(to);
for (int j = ; j < ; j++)
{
int add = num[x] == j;
dp[x][j] = max(dp[x][j], dp[to][j] + add);
}
}
}
gone[x] = ;
}
int main()
{
int n, m;
int from, to;
cin >> n >> m;
cin >> a;
for (int i = ; i < a.size(); i++)
{
num[i + ] = a[i] - 'a';
dp[i + ][num[i + ]] = ;
}
for (int i = ; i <= m; i++)
{
scanf("%d %d", &from, &to);
tree[from].pb(to);
}
for (int i = ; i <= n; i++)
{
if (!gone[i])
{
dfs(i);
}
}
for (int i = ; i <= n; i++)
{
for (int j = ; j <= ; j++)
{
ans[i] = max(ans[i], dp[i][j]);
}
anser = max(anser, ans[i]);
}
cout << anser << endl;
return ;
}
Codeforces 919 行+列前缀和 树上记忆化搜索(树形DP)的更多相关文章
- 记忆化搜索(DFS+DP) URAL 1223 Chernobyl’ Eagle on a Roof
题目传送门 /* 记忆化搜索(DFS+DP):dp[x][y] 表示x个蛋,在y楼扔后所需要的实验次数 ans = min (ans, max (dp[x][y-i], dp[x-1][i-1]) + ...
- 记忆化搜索(DFS+DP) URAL 1501 Sense of Beauty
题目传送门 /* 题意:给了两堆牌,每次从首部取出一张牌,按颜色分配到两个新堆,分配过程两新堆的总数差不大于1 记忆化搜索(DFS+DP):我们思考如果我们将连续的两个操作看成一个集体操作,那么这个操 ...
- Codeforces Round #554 (Div. 2) D 贪心 + 记忆化搜索
https://codeforces.com/contest/1152/problem/D 题意 给你一个n代表合法括号序列的长度一半,一颗有所有合法括号序列构成的字典树上,选择最大的边集,边集的边没 ...
- Codeforces Round #336 (Div. 2) D. Zuma 记忆化搜索
D. Zuma 题目连接: http://www.codeforces.com/contest/608/problem/D Description Genos recently installed t ...
- Codeforces Round #406 (Div. 1) A. Berzerk 记忆化搜索
A. Berzerk 题目连接: http://codeforces.com/contest/786/problem/A Description Rick and Morty are playing ...
- codeforces 793 D. Presents in Bankopolis(记忆化搜索)
题目链接:http://codeforces.com/contest/793/problem/D 题意:给出n个点m条边选择k个点,要求k个点是联通的而且不成环,而且选的边不能包含选过的边不能包含以前 ...
- Codeforces Gym 191033 E. Explosion Exploit (记忆化搜索+状压)
E. Explosion Exploit time limit per test 2.0 s memory limit per test 256 MB input standard input out ...
- CodeForces - 632E Thief in a Shop (FFT+记忆化搜索)
题意:有N种物品,每种物品有价值\(a_i\),每种物品可选任意多个,求拿k件物品,可能损失的价值分别为多少. 分析:相当于求\((a_1+a_2+...+a_n)^k\)中,有哪些项的系数不为0.做 ...
- Codeforces Round #459 (Div. 2):D. MADMAX(记忆化搜索+博弈论)
题意 在一个有向无环图上,两个人分别从一个点出发,两人轮流从当前点沿着某条边移动,要求经过的边权不小于上一轮对方经过的边权(ASCII码),如果一方不能移动,则判负.两人都采取最优策略,求两人分别从每 ...
随机推荐
- leetcode 347前k个高频元素
通过hash map遍历一遍存储出现的次数,通过小顶堆存储k个元素 //设想利用hash map来存储每个元素的个数:采用小顶堆存储k个元素:timeO(n+klogk)spaceO(n+k) cla ...
- leetcode171 Excel列表序列号
/** 可看做26进制到10进制转换问题:v=26*v+s[i]-'A'; **/ class Solution { public: int titleToNumber(string s) { ; f ...
- leetcode 分割回文串
这个方法有问题,这是计算所有子串组成的所有回文子串:而不是所有分割的回文子串: class Solution { public: vector<vector<string>> ...
- matplotlib之折线图
1.案例一 # coding=utf-8 from matplotlib import pyplot as plt import random # 设置字体相关 from matplotlib imp ...
- React之生命周期函数
1.新增知识点 /* https://reactjs.org/docs/react-component.html React生命周期函数: 组件加载之前,组件加载完成,以及组件更新数据,组件销毁. 触 ...
- Synchronized知道这些就可以了
Synchronized关键字算是Java的元老级锁了,一开始它撑起了Java的同步任务,其用法简单粗暴容易上手.但是有些与它相关的知识点还是需要我们开发者去深入掌握的.比如,我们都知道通过Synch ...
- poatman接口测试--初试
接到测试任务,对两个商品接口,进行接口测试 测试工具:postman 域名:rap2查找的或询问开发, 接口的参数规则:参考rap2的备注 开发没有添加详细说明的,让开发补充说明规则,及定义的返回状态 ...
- Git提示要输入密码
在服务器上Git pull或者push时,一直提示要输入密码,但这个密码又不是Gitlab账户的密码,经过同学指点,原来是项目主分支被保护起来了,处于protected的状态,因为目前只有我一个人 在 ...
- word2vec高效训练方法
在word2vec原理中讲到如果每个词向量由300个元素组成,并且一个单词表中包含了10000个单词.回想神经网络中有两个权重矩阵——一个在隐藏层,一个在输出层.这两层都具有300 x 10000 = ...
- CDH6.2官方文档
文档总览: https://www.cloudera.com/documentation/enterprise/6/6.2.html 官方api: https://www.cloudera.com/d ...