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

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
思路:
1 可以把不相邻的先宣布,所以不相邻的不纳入考虑,这时只剩下相连的几片了,任务就是求这几片的某种分割使得留下的人最多,只需要分类统计,关注这些相连片的大小即可
2 因为这些点都必须是孤立的所以就直接设dp[i]为使i相互独立所需删去点数,对一个小于i的点集j,dp[i]=max(dp[i-j]+dp[j-1]+1,dp[i-j-1][j]+1)(FFF团员取最大值),那么对于所有j,Wyest取其中的最小值,也就是dp[i]=min(dp[i],max(dp[i-j]+dp[i-j]+1,dp[i-j-1][j]+1))
3 这道题如果按照我最初的思路,直接dp原图而不是分类统计,那么反而会很麻烦
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
int dp[501];
int n,m;
bool heap[501];

int main(){
    while(scanf("%d%d",&n,&m)==2){
        memset(dp,0,sizeof(dp));
        memset(heap,0,sizeof(heap));
        int f,t;
        for(int i=0;i<m;i++){
            scanf("%d%d",&f,&t);
            if(t<f)swap(f,t);
            if(t-f==1){
                heap[f]=true;
            }
        }
        dp[0]=0;
        dp[1]=0;
        dp[2]=1;
        for(int i=3;i<=n;i++){
            dp[i]=0x7ffffff;
            for(int j=0;j<i;j++){
                dp[i]=min(dp[i],max(dp[j-1]+dp[i-j]+1,dp[j]+dp[i-j-1]+1));
            }
        }
        int ans=0,cnt=1;
        for(int i=0;i<n;i++){
            if(!heap[i]||i==n-1){
             if(cnt>1) ans+=dp[cnt];
               cnt=1;
            }
            else cnt++;
        }
        printf("%d\n",n-ans);
    }
    return 0;
}
View Code
原文地址:https://www.cnblogs.com/xuesu/p/3968651.html