不想打题面了,题面戳这里

这道题目的模型转换地有点猛。首先我们肯定需要让老板把那些不相邻的人的卡牌放在前面,这样他们就作废了。然后剩下的卡牌就都是相邻人之间的了。我们就可以把这个序列分成若干个联通块,每个联通块内相邻的人之间有连边。此时显然不同联通块是互不干扰的,我们只需要知道每个联通块内剩下的人最多可以是多少就可以了。这个我们就可以dp了。

\(f_i\)表示大小为\(i\)的联通块内最多能剩多少人,那么方程就和显然了。

\[f_i = \max \{ f_j+f_{i-j-1} \},j < 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的更多相关文章

  1. ZOJ3161

    朴素动态规划 ZOJ3161 题意:(严重标题党)老板不想让客人走,客人不想留,客人按顺序排好,老板抽8g(书上翻译成八卦,神翻译),抽到的 如果相邻,其中一个人由客人决定离开,求最后黑心的老板最多能 ...

  2. Sicily 1021. Couples

    题目地址:1021. Couples 思路: 想清楚了这道题其实很简单.利用夫妻出现的位置作为下标,并设为同一值,第一对夫妻值为1,第二对为2,以此类推,存储完毕即可进入下一步. 利用栈这个数据结构: ...

  3. [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 ...

  4. [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 ...

  5. [CC-COUPLES]Couples sit next to each other

    [CC-COUPLES]Couples sit next to each other 题目大意: 有\(n(n\le5\times10^5)\)对小伙伴共\(2n\)个人坐成一圈.刚开始编号为\(i\ ...

  6. 每日英语: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 ...

  7. ZOJ 3161 Damn Couples 动态规划 难度:2

    Damn Couples Time Limit: 1 Second      Memory Limit: 32768 KB As mentioned in the problem "Coup ...

  8. 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 ...

  9. [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 ...

随机推荐

  1. LeetCode804. Unique Morse Code Words

    题目 国际摩尔斯密码定义一种标准编码方式,将每个字母对应于一个由一系列点和短线组成的字符串, 比如: "a" 对应 ".-", "b" 对应 ...

  2. Redis ---------- 持久化(AOF)操作

    每小时做一次快照持久化 8:00 快照持久化 9:00 快照持久化 10:00  快照持久化  上帝想玩弄人类,突然停电,55万个key丢失了 11:00 快照持久化 解决方案: 8:00-9:00在 ...

  3. Redis ----------String的操作

    set    key   value 设置key对应的值为String类型的value mset    key   value 一次设置多个 key对应的值 mget    key   value 一 ...

  4. 6,MongoDB 之 Array Object 的特殊操作

    相比关系型数据库, Array [1,2,3,4,5] 和 Object { 'name':'DragonFire' } 是MongoDB 比较特殊的类型了 特殊在哪里呢?在他们的操作上又有什么需要注 ...

  5. Altium Designer之模块复用——设备图表符与Snippets

    Altium Designer中的设备图表符(Device Sheet Symbol)和Snippets是设计中模块复用的利器,下面简单介绍下这个两个功能的使用. 一.设备图表符(Device She ...

  6. CSS计数器(自定义列表)

    概念 CSS3计数器(CSS Counters)可以允许我们使用css对页面中的任意元素进行计数,实现类似于有序列表的功能(自定义有序列表) 与有序列表相比,它的突出特性在于可以对任意元素计数,同时实 ...

  7. 把实体bean对象转换成DBObject工具类

    import java.lang.reflect.Field; import java.lang.reflect.InvocationTargetException; import java.util ...

  8. oracle 用户被锁定解锁方法

    修改了用户密码,第二天过来发现用户被锁定,晚上走的时候还好好的 . alter profile DEFAULT limit FAILED_LOGIN_ATTEMPTS UNLIMITED; alter ...

  9. java和c/c++

    写c/c++的人,羡慕java可以自己管理内存 写java的人,羡慕c/c++没有gc问题

  10. 【Gradient Boosted Decision Tree】林轩田机器学习技术

    GBDT之前实习的时候就听说应用很广,现在终于有机会系统的了解一下. 首先对比上节课讲的Random Forest模型,引出AdaBoost-DTree(D) AdaBoost-DTree可以类比Ad ...