zoj3161 Damn Couples
不想打题面了,题面戳这里
这道题目的模型转换地有点猛。首先我们肯定需要让老板把那些不相邻的人的卡牌放在前面,这样他们就作废了。然后剩下的卡牌就都是相邻人之间的了。我们就可以把这个序列分成若干个联通块,每个联通块内相邻的人之间有连边。此时显然不同联通块是互不干扰的,我们只需要知道每个联通块内剩下的人最多可以是多少就可以了。这个我们就可以dp了。
\(f_i\)表示大小为\(i\)的联通块内最多能剩多少人,那么方程就和显然了。
\]
最后对统计所有联通块答案求和即可。
#include<iostream>
#include<cstdio>
#include<cstdlib>
using namespace std;
const int maxn = 510;
int N,M,f[maxn],father[maxn],ans;
inline int find(int x) { return father[x] != x?father[x] = find(father[x]):x; }
int main()
{
freopen("3161.in","r",stdin);
freopen("3161.out","w",stdout);
f[1] = 1; f[0] = 0;
for (int i = 2;i <= 500;++i) for (int j = 1;j < i;++j) f[i] = max(min(f[j-1]+f[i-j],f[j]+f[i-j-1]),f[i]);
while (scanf("%d %d",&N,&M) != EOF)
{
ans = 0;
for (int i = 0;i < N;++i) father[i] = i;
for (int i = 1,a,b;i <= M;++i)
{
scanf("%d %d",&a,&b);
if (a > b) swap(a,b);
if (b - a == 1)
{
int r1 = find(a),r2 = find(b);
father[r2] = r1;
}
}
for (int i = 0,j;i < N;++i)
{
for (j = i;j < N&&find(j) == i;++j);
ans += f[j-i]; --j;
}
printf("%d\n",ans);
}
fclose(stdin); fclose(stdout);
return 0;
}
zoj3161 Damn Couples的更多相关文章
- ZOJ3161
朴素动态规划 ZOJ3161 题意:(严重标题党)老板不想让客人走,客人不想留,客人按顺序排好,老板抽8g(书上翻译成八卦,神翻译),抽到的 如果相邻,其中一个人由客人决定离开,求最后黑心的老板最多能 ...
- Sicily 1021. Couples
题目地址:1021. Couples 思路: 想清楚了这道题其实很简单.利用夫妻出现的位置作为下标,并设为同一值,第一对夫妻值为1,第二对为2,以此类推,存储完毕即可进入下一步. 利用栈这个数据结构: ...
- [LeetCode] Couples Holding Hands 两两握手
N couples sit in 2N seats arranged in a row and want to hold hands. We want to know the minimum numb ...
- [Swift]LeetCode765. 情侣牵手 | Couples Holding Hands
N couples sit in 2N seats arranged in a row and want to hold hands. We want to know the minimum numb ...
- [CC-COUPLES]Couples sit next to each other
[CC-COUPLES]Couples sit next to each other 题目大意: 有\(n(n\le5\times10^5)\)对小伙伴共\(2n\)个人坐成一圈.刚开始编号为\(i\ ...
- 每日英语:Why Rate Your Marriage? A Numerical Score Can Help Couples Talk About Problems
When marriage therapist Sharon Gilchrest O'Neill met with new clients recently, she asked them why t ...
- ZOJ 3161 Damn Couples 动态规划 难度:2
Damn Couples Time Limit: 1 Second Memory Limit: 32768 KB As mentioned in the problem "Coup ...
- LeetCode765. Couples Holding Hands
N couples sit in 2N seats arranged in a row and want to hold hands. We want to know the minimum numb ...
- [LeetCode] 765. Couples Holding Hands 情侣牵手
N couples sit in 2N seats arranged in a row and want to hold hands. We want to know the minimum numb ...
随机推荐
- LunaSchedule记录
博客访问量突破10000!!!(值得高兴一下 用一学期超级课程表,被50+M的内存占用,巨慢的加载速度给弄烦了,就自己开发了一款课程表管理程式 添加日历订阅,自动导入到系统日历,无需安装任何app L ...
- ABAP Table Control
SAP中,Table Control是在Screen中用的最广泛的控件之一了,可以实现对多行数据的编辑. 简单来说,Table Control是一组屏幕元素在Screen上的重复出现,这就是它与普通 ...
- java后台poi根据模板导出excel
public class ExcelUtils { private static final String INSPECTIONRECORD_SURFACE_TEMPLET_PATH = " ...
- JAVA / MySql 编程—— 第四章 高级查询(二)
1. EXISTS和NOT EXISTS子查询:EXISTS关键字用来检测数数据库对象是否存在. ★EXISTS和NOT EXISTS的结果只取决于是否 ...
- Guava Cache 工具类 [ GuavaCacheUtil ]
pom.xml <dependency> <groupId>com.google.guava</groupId> <artifactId>guava&l ...
- 第1章 HTML5 概述
第 1 章 HTML5 概述 学习要点: 1.HTML5 的历史 2.HTML5 的功能 3.HTML5 的特点 4.课程学习问题 HTML5 是继 HTML4.01 和 XHTML1.0 之后的超 ...
- php - 从数据库导出百万级数据(CSV文件)
将数据库连接信息.查询条件.标题信息替换为真实数据即可使用. <?php set_time_limit(0); ini_set('memory_limit', '128M'); $fileNam ...
- 环形缓冲区实现类(Delphi)
环形缓冲区的用途及原理可以去百度资料狠多的,这里就不介绍了.直接贴代码.代码分别用D7,XE2编译测试 源码下载 http://files.cnblogs.com/lwm8246/uCircleBuf ...
- python -- configparse读取配置文件
在开发过程中,有的时候需要将一些参数写入到配置文件中,这样在改动一些相关信息时,可以直接在配置文件中进行修改. 而在python中,可以通过内置模块configparse对标准的配置文件进行读取. 配 ...
- Leetcode 538. 把二叉搜索树转换为累加树
题目链接 https://leetcode.com/problems/convert-bst-to-greater-tree/description/ 题目描述 大于它的节点值之和. 例如: 输入: ...