ZOJ3161 Damn Couples

来源:互联网 发布:debian知乎 编辑:程序博客网 时间:2024/05/16 17:55

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 asmany 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) andm (0 <= m <= C(n, 2)), indicating the number of persons and the number of "8g"s in the list. People are indexed from0 to n - 1 and they always sit in increasing order. The followingm lines each contains two integers a and b, meaning a "8g" betweena 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 20 11 23 10 2

Sample Output

13

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, the0th person and the 2nd person didn't sit together, so all of the three remain. 

分析:

先贪心,讲不连着坐的人声明,然后剩下的就是链状的,用一个dp求出每一个的最大保留人数

code:

#include<cstdio>#include<cstring>#define MAXN 500using namespace std;int min(int a,int b){return a<b?a:b;}int max(int a,int b){return a>b?a:b;}int n,m,ans;int announced[MAXN+5];int f[MAXN+5];int my_abs(int x){return x>0?x:-x;}int rtsearch(int x){return x==announced[x]?x:announced[x]=rtsearch(announced[x]);}bool fit(int x,int y){return my_abs(x-y)==1;}int main(){int i,j;f[0]=0,f[1]=f[2]=f[3]=1;for(i=4;i<=MAXN;i++)for(j=1;j<i;j++)f[i]=max(f[i],min(f[j-1]+f[i-j],f[j]+f[i-j-1]));while(~scanf("%d%d",&n,&m)){memset(announced,-1,sizeof announced);ans=0;int cnt=1;for(i=0;i<n;i++)announced[i]=i;for(i=0;i<m;i++){int s,t;scanf("%d%d",&s,&t);if(fit(s,t))announced[min(s,t)]=max(s,t);}for(i=0;i<n;i++){if(announced[i]!=i&&i+1!=n)++cnt;if(announced[i]==i){ans+=f[cnt];cnt=1;}}printf("%d\n",ans);}}


1 0