Damn couples Time Limit: 1 second memory limit: 32768 KB
As mentioned in the problem "couples", so far 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 .)
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 was Ted 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,OneOf 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. IfITh person left,I-1)Th and(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 peopleBytesAs 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. cocould you help her find it out?
Input
The first line of each case will contain two integersN(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 from0ToN-1And they always sit in increasing order. The followingMLines each contains two integersAAndB, Meaning a "8g"AAndB. 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
Ideas:
1. We can describe non-adjacent items as dummies, so we will not consider anything that is not adjacent. At this time, there will be only a few connected parts, the task is to find some separation of these slices so that the most people are left behind. You only need to classify statistics and pay attention to the size of these connected slices.
2 because these points must be isolated, we directly set DP [I] to delete the points required for independent I. For a point set less than I, DP [I] = max (DP [I-j] + dp [J-1] + 1, DP [i-j-1] [J] + 1) (max for fff members ), for all J, wyest takes the minimum value, that is, DP [I] = min (DP [I], max (DP [I-j] + dp [I-j] + 1, DP [i-j-1] [J] + 1 ))
3. If I directly use the DP source image instead of the classification statistics based on my original ideas, it will be very troublesome.
#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
Zoj 3161 damn couples