CF#508 1038E Maximum Matching
题解:
感觉还是比较妙的,复杂度看上去很高(其实也很高),但是因为n只有100,所以还是可以过的。
考虑一个很暴力的状态f[i][j][x][y]表示考虑取区间i ~ j的方格,左右端点颜色分别是x, y.的最大值。
那么有如下转移
1,直接继承子区间的答案
f[i][j][x][y] = max(f[i][k][x][y], f[k + 1][j][x][y]);//因为子区间就这2种,毕竟子区间一定比当前区间小,因此不靠在端点上的区间一定已经被靠在端点上的区间给取过max了。
2,由2段子区间拼凑而来,相当于枚举中间断开的地方是选了那个块.//如果中间断开的地方的块没有被选,那么一定可以找到一个被选的块作为断点(如果找不到就说明这整个区间内只取了端点,再转移也没有什么意义。)
翻转操作是不需要考虑的,因为可以在初始化的地方就处理掉,因此只需要在转移的地方考虑一下乱序继承即可。
即正常的顺序是[i, l] + [l + 1, j] = [i, j];
乱序则可以支持[l + 1][j] + [i, l] = [i, j];
所以对于这2种情况都转移一下,转移的时候必须要求相接的地方颜色相同即可。
注意因为有子区间相加转移的地方,所以初始化为极小值的时候不要太小了,不然太小了直接一加就爆了。
#include<bits/stdc++.h>
using namespace std;
#define R register int
#define AC 110
#define ac 6 int n, ans;
int f[AC][AC][ac][ac]; inline int read()
{
int x = ;char c = getchar();
while(c > '' || c < '') c = getchar();
while(c >= '' && c <= '') x = x * + c - '', c = getchar();
return x;
} void pre()
{
n = read();
memset(f, -, sizeof(f));
int a, b, c;
for(R i = ; i <= n; i ++)
{
a = read(), b = read(), c = read();
f[i][i][a][c] = f[i][i][c][a] = b;
}
} inline void upmax(int &a, int b)
{
if(b > a) a = b;
} void work()
{
for(R i = n; i; i --)
for(R j = i; j <= n; j ++)
for(R x = ; x <= ; x ++)
for(R y = ; y <= ; y ++)
{
for(R l = i; l < j; l ++)
{
upmax(f[i][j][x][y], max(f[i][l][x][y], f[l + ][j][x][y]));
for(R k = ; k <= ; k ++)
{
upmax(f[i][j][x][y], f[i][l][x][k] + f[l + ][j][k][y]);
upmax(f[i][j][x][y], f[i][l][k][y] + f[l + ][j][x][k]);
}
}
upmax(ans, f[i][j][x][y]);
}
printf("%d\n", ans);
} int main()
{
//freopen("in.in", "r", stdin);
pre();
work();
//fclose(stdin);
return ;
}
CF#508 1038E Maximum Matching的更多相关文章
- [Codeforces Round #508 (Div. 2)][Codeforces 1038E. Maximum Matching]
前几天给舍友讲这题的时候感觉挺有意思的,就贴上来吧... 题目链接:1038E - Maximum Matching 题目大意:有\(n\)个棒子,每个条两端有颜色\(c1,c2\)以及他的价值\(v ...
- Codeforces 1038E Maximum Matching
可能写了个假算法 假设定义:含有一个欧拉路的图为类欧拉图 欧拉路的定义:一个无向连通图中,存在一条路径对所有边都遍历且仅遍历一次:判断方法:该连通图中度为奇数的点的个数不能超过2,即为0或者2 题目解 ...
- Codeforces Round #508 (Div. 2) E. Maximum Matching(欧拉路径)
E. Maximum Matching 题目链接:https://codeforces.com/contest/1038/problem/E 题意: 给出n个项链,每条项链左边和右边都有一种颜色(范 ...
- Codeforces 1038 E - Maximum Matching
E - Maximum Matching 思路: 欧拉图 定理:一个度数为奇数的点的个数小于等于2的联通图存在欧拉回路 对于这道题目的图,点的个数为4,所以最坏的情况下4个点的度数都为奇数,在这种情况 ...
- [codeforces 508E]Maximum Matching
题目:Maximum Matching 传送门:http://codeforces.com/contest/1038/problem/E 分析: 一个块拥有{color1,val,color2},两个 ...
- cf#513 B. Maximum Sum of Digits
B. Maximum Sum of Digits time limit per test 2 seconds memory limit per test 512 megabytes input sta ...
- CF 353C Find Maximum #205 (Div. 2)
#include<stdio.h> #include<string.h> #include<algorithm> using namespace std; ]; ] ...
- SPOJ4206Fast Maximum Matching(hopcroft-karp)
题目请戳这里 题目大意:裸的二分匹配. 题目分析:数据比较强,用来测模版的.这题用hungry跑着会比较吃力,所以用hopcroft-karp算法.这个算法较hungry高效是因为每次bfs找到一个增 ...
- cf C. Find Maximum
http://codeforces.com/contest/353/problem/C 先预处理前i个数的和,然后找到第一个出现的1,然后变成0后的和与目前的和比较,如果大就更新. #include ...
随机推荐
- jquery实现倒计时功能
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- Ruby 基础教程1-5
1.条件语句 if unless case unless和if相反,条件不成立则执行 2.条件 除了 false和nil 其他都是true 3.unless 语法 ...
- SSM-最新pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/20 ...
- CentOS 7.2安装11g Grid Infrastructure
Preface Oracle claimed that 11g RAC is supported on Redhat Linux 7 and above version,but the ...
- TPO-13 C1 Understand the assignment in psychology course
TPO-13 C1 Understand the assignment in psychology course 第 1 段 1.listen to a conversation between a ...
- 了解Python控制流语句——if语句
控制流 截止到现在,在我们所看过的程序中,总是有一系列语句从上到下精确排列,并交由 Python 忠实地执行.如果你想改变这一工作流程,应该怎么做?就像这样的情况:你需要程序作出一些决定,并依据不同的 ...
- 爬虫1.1-基础知识+requests库
目录 爬虫-基础知识+requests库 1. 状态返回码 2. URL各个字段解释 2. requests库 3. requests库爬虫的基本流程 爬虫-基础知识+requests库 关于html ...
- [模板]非递归线段树(zkw的变异版本)
类似于zkw,但空间只用两倍,zkw要4倍. 链接 可以下传标记,打熟后很好码. #include <set> #include <cmath> #include <cs ...
- java.net.ProtocolException: Server redirected too many times
网页爬虫时,原来正常的代码,可能是因为网站做了cookie校验处理,报异常:java.net.ProtocolException: Server redirected too many times 表 ...
- NYOJ 35 表达式求值(逆波兰式求值)
http://acm.nyist.net/JudgeOnline/problemset.php?typeid=4 NYOJ 35 表达式求值(逆波兰式求值) 逆波兰式式也称后缀表达式. 一般的表达式求 ...