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 ...
随机推荐
- flask实现基于elasticsearch的关键词搜索建议
1.实现效果 2.fuzzy模糊查询和suggest查询 fuzzy模糊查询 GET chaxun/job/_search { "query": { "fuzzy&quo ...
- jquery 改变标签可见状态的几种方式
第一种: $(selector).show(); //立即显示指定标签 $(selector).hide();//立即隐藏指定标签 第二种: $(selector).fadeIn(ms);//在指定毫 ...
- php动态画图
index.php <?php $width=800; $height=600; //绘图技术 基本步骤 前提:在php.ini文件中启用gd库 //创建画布 默认背景是黑色的 $img=ima ...
- dts--tests(三)
sample_built.py """ DPDK Test suite. Test sample_built. """ import uti ...
- python获取Excel数据
Python中一般使用xlrd(excel read)来读取Excel文件,使用xlwt(excel write)来生成Excel文件(可以控制Excel中单元格的格式),需要注意的是,用xlrd读取 ...
- JZOJ 5919. 逛公园
Description 琥珀色黄昏像糖在很美的远方,思念跟影子在傍晚一起被拉长……Description 小 B 带着 GF 去逛公园,公园一共有 n 个景点,标号为 ...
- python学习之数据类型与运算符号
python版本:3.6 python编辑器:pycharm 最新版本 整理成代码如下: #!/usr/bin/env python #-*- coding: utf-8 -*- # 数学操作符 pr ...
- POJ 2891 中国剩余定理(不互素)
Strange Way to Express Integers Time Limit: 1000MS Memory Limit: 131072K Total Submissions: 17877 ...
- 011---Djang的cookie和session
-------------------------------------------------------------cookie与session------------------------- ...
- POJ:2674-Linear world(名字交换碰撞)
Linear world Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 4514 Accepted: 1025 Descript ...