ZOJ3161 Damn Couples DP

简单DP,设dp[i]为最糟糕的情况下这连续i人最多留下几人,我们枚举每次报出一对关系后走的人的位置

状态转移方程 dp[i]=max(dp[i],min(dp[j-1]+dp[i-j],dp[j]+dp[i-j-1]));    最后对于序列统计连续关系对数相加即可。

不想临的关系不用管



Damn Couples

Time Limit: 1 Second       Memory Limit: 32768 KB

As mentioned in the problem "Couples", so many new words appear on the internet. Another special one is "Damn Couples", a club which consists of people who have failed on love affairs and decide not to start a relationship. (If you want to know more words, just turn to a search engine.)

Damn couples!

We have a group of people in the club, and everyday they sit there in one line in a fixed order, doing nothing. Life in the club is indeed boring, especially for the leader Wyest, and one day she invented a game. She first makes up a list of imaginary "8g"s among the members, every "8g" is between two persons, then publishes the list. Each minute she chooses one "8g" from the list and announces it to be true, until all "8g"s in the list have been announced. She will not 8g the same two persons more than once.

In a single minute, the following things may happen.

1. If the two persons involved in the "8g" are sitting next to each other, one of them will speak out the "Damn couples!" slogan, then stand up and leave the table. Notice that leaving doesn't prevent him/her from possible future "8g"s. 
2. If the ith person left, the (i-1)th and the (i+1)th are considered to be next to each other.

On the other hand, Wyest wouldn't like to see people become too few since she likes it to be noisy. All the members know this, however, to show their loyalty for the club, they try to make leaving people as many as possible, based on the already announced "8g"s and those not yet to be. Wyest also knows what they're planning, so now she wants to know at most how many people can remain, if she carefully chooses the "8g" to announce each minute. Could you help her find it out?

Input

The first line of each case will contain two integers n (2 <= n <= 500) and m (0 <= m <= C(n, 2)), indicating the number of persons and the number of "8g"s in the list. People are indexed from 0 to n - 1 and they always sit in increasing order. The following m lines each contains two integers a and b, meaning a "8g" between a and b. Proceed to the end of file.

Output

For each case, print one line containing the maximum number of people that can remain in total.

Sample Input

3 2
0 1
1 2
3 1
0 2

Sample Output

1
3

Hint

In the first case, no matter in what order the "8g"s are announced, the 1st person wouldn't leave first. In the second case, the 0th person and the 2nd person didn't sit together, so all of the three remain.

#include<iostream>
#include<cstring>
#include<string>
#include<algorithm>
#include<map>
#include<utility>
#include<cstdio>

using namespace std;

#define MAXN 505

map<pair<int,int>,int> mii;
int dp[MAXN];
int n,m,cnt,ans;
int main()
{
    while(~scanf("%d%d",&n,&m))
    {
        mii.clear();
        for(int i=0;i<m;i++)
        {
            int x,y;
            scanf("%d%d",&x,&y);
            if(abs(x-y)!=1) continue;
            if(x>y) swap(x,y);
            mii[make_pair(x,y)]=1;
        }
        dp[0]=0,dp[1]=dp[2]=dp[3]=1;
        for(int i=4;i<=n;i++)
        {
            dp[i]=0;
            for(int j=1;j<i;j++) dp[i]=max(dp[i],min(dp[j-1]+dp[i-j],dp[j]+dp[i-j-1]));
        }
        ans=0,cnt=1;
        for(int i=0;i<n;i++)
        {
            if(mii.count(make_pair(i,i+1))!=0 && i+1!=n) cnt++;
            else
            {
                ans+=dp[cnt];
                cnt=1;
            }
        }
        printf("%d\n",ans);
    }
    return 0;
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值