zoj 3161 Damn Couples

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

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;
}

时间: 2024-08-05 08:41:53

zoj 3161 Damn Couples的相关文章

概率dp ZOJ 3640

Help Me Escape Time Limit:2000MS     Memory Limit:32768KB     64bit IO Format:%lld & %llu Submit Status Practice ZOJ 3640 Appoint description:  System Crawler  (2014-10-22) Description Background     If thou doest well, shalt thou not be accepted? an

zoj 2156 - Charlie&#39;s Change

题目:钱数拼凑,面值为1,5,10,25,求组成n面值的最大钱币数. 分析:dp,01背包.需要进行二进制拆分,否则TLE,利用数组记录每种硬币的个数,方便更新. 写了一个 多重背包的 O(NV)反而没有拆分快.囧,最后利用了状态压缩优化 90ms: 把 1 cents 的最后处理,其他都除以5,状态就少了5倍了. 说明:貌似我的比大黄的快.(2011-09-26 12:49). #include <stdio.h> #include <stdlib.h> #include <

ZOJ 1718 POJ 2031 Building a Space Station 修建空间站 最小生成树 Kruskal算法

题目链接:ZOJ 1718 POJ 2031 Building a Space Station 修建空间站 Building a Space Station Time Limit: 2 Seconds      Memory Limit: 65536 KB You are a member of the space station engineering team, and are assigned a task in the construction process of the statio

ZOJ 3607 Lazier Salesgirl (贪心)

Lazier Salesgirl Time Limit: 2 Seconds      Memory Limit: 65536 KB Kochiya Sanae is a lazy girl who makes and sells bread. She is an expert at bread making and selling. She can sell the i-th customer a piece of bread for price pi. But she is so lazy

ZOJ - 2243 - Binary Search Heap Construction

先上题目: Binary Search Heap Construction Time Limit: 5 Seconds      Memory Limit: 32768 KB Read the statement of problem G for the definitions concerning trees. In the following we define the basic terminology of heaps. A heap is a tree whose internal n

ZOJ 2859 二维线段树

思路:自己写的第二发二维线段树1A,哈哈,看来对二维的push操作比较了解了:但是还没遇到在两个线段树中同时进行push操作的,其实这题我是想在x维和y维同时进行push操作的,但是想了好久不会,然后看到这题又给出10秒,然后想想在x维线段直接单点查询肯定也过了,然后在第二维就只有pushup操作,在第一维线段树没有pushup操作.要是在第一维也有pushup操作的话,那就不用单点查询那么慢了.不过也A了,想找题即在二维同时进行pushup和pushdown操作的. #include<iost

ZOJ 2588

求一个无向图的桥(可能存在重边),输出割边的数目,并按顺序输出割边的序号(输入的顺序). 由于内存的限制 , 无法使用邻接矩阵 , 只能用邻接表了 . 第一次用了邻接表,超内存了: 1 #include <iostream> 2 #include <cstdio> 3 #include <algorithm> 4 #include <string.h> 5 using namespace std; 6 const int N=10002; 7 const i

ZOJ 2587 Unique Attack 判断最小割是否唯一

很裸的判断最小割是否唯一.判断方法是先做一遍最大流求最小割,然后从源点和汇点分别遍历所有能够到达的点,看是否覆盖了所有的点,如果覆盖了所有的点,那就是唯一的,否则就是不唯一的. #include <cstdio> #include <cstring> #include <cmath> #include <algorithm> #include <climits> #include <string> #include <iostr

ZOJ 3794 Greedy Driver spfa

题意: 给定n个点,m条有向边,邮箱容量. 起点在1,终点在n,开始邮箱满油. 下面m行表示起点终点和这条边的耗油量(就是长度) 再下面给出一个数字m表示有P个加油站,可以免费加满油. 下面一行P个数字表示加油站的点标. 再下面一个整数Q 下面Q行 u v 表示在u点有销售站,可以卖掉邮箱里的任意数量的油,每以单位v元. 问跑到终点能获得最多多少元. 先求个每个点的最大剩余油量 f[i], 再把边反向,求每个点距离终点的最短路 dis[i]. 然后枚举一下每个销售点即可,( f[i] - dis