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 ...
随机推荐
- 46个Linux面试常见问题
问题一: 绝对路径用什么符号表示?当前目录.上层目录用什么表示?主目录用什么表示? 切换目录用什么命令? 答案:绝对路径: 如/etc/init.d当前目录和上层目录: ./ ../主目录: ~/切 ...
- [Ljava.lang.String; cannot be cast to java.lang.String报错的原因
完整错误信息: java.lang.ClassCastException: [Ljava.lang.String; cannot be cast to java.lang.String 报这个错的原因 ...
- 在CentOS VPS上通过SSH安装 MySQL
输入 yum install mysql-server 按Y继续 安装完成,设置开机启动Mysql,输入 chkconfig --levels 235 mysqld on 然后启动tomcat,输入s ...
- PHP处理mysql事务
MYSQL的事务处理主要有两种方法.1.用begin,rollback,commit来实现begin 开始一个事务rollback 事务回滚commit 事务确认2.直接用set来改变mysql的自动 ...
- tomcat 无论如何都无法创建新的Servers
1.退出eclipse 2.到[工程目录下 workspace ]/.metadata/.plugins/org.eclipse.core.runtime 3.把org.eclipse.wst.ser ...
- E - Nature Reserve CodeForces - 1059D
传送门 There is a forest that we model as a plane and live nn rare animals. Animal number iihas its lai ...
- [Bzoj3991]寻宝游戏(dfs序+set)
Description 题目链接 Solution 用set按dfs序维护当前的宝物序列,那么答案为相邻2个点的距离加上头尾2个的距离 Code #include <cstdio> #in ...
- 笔记-reactor pattern
笔记-reactor pattern 1. reactor模式 1.1. 什么是reactor模式 The reactor design pattern is an event han ...
- hive-pom.xml
4.0.0 <groupId>com.cenzhongman</groupId> <artifactId>hive</artifactId> <v ...
- python语法re.compile模块介绍
1. re模块是正则表达式模块,re模块中包含一个重要函数是compile(pattern [, flags]) ,该函数根据包含的正则表达式的字符串创建模式对象.可以实现更有效率的匹配. impor ...