POJ 2168 Joke with Turtles

本文探讨了一种使用动态规划(DP)算法解决儿童智力谜题的方法,该谜题涉及三条乌龟在道路上爬行,并且部分乌龟在陈述时存在误导。文章详细介绍了算法实现步骤,并通过实例解释如何确定最少的撒谎者数量。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

脑子一抽用0初始化DP。。。TLE到爆了。。。。


Joke with Turtles
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 1203 Accepted: 319 Special Judge

Description

There is a famous joke-riddle for children: 
Three turtles are crawling along a road. One turtle says: "There are two turtles ahead of me." 
The other turtle says: "There are two turtles behind me." The third turtle says: "There are 
two turtles ahead of me and two turtles behind me." How could this have happened? 
The answer is -- the third turtle is lying!

Now in this problem you have n turtles crawling along a road. Some of them are crawling in a group, so that they do not see members of their group neither ahead nor behind them. Each turtle makes a statement of the form: "There are ai turtles crawling ahead of me and bi turtles crawling behind me." Your task is to find the minimal number of turtles that must be lying. 
Let us formalize this task. Turtle i has xi coordinate. Some turtles may have the same coordinate. Turtle i tells the truth if and only if ai is the number of turtles such that xj > xi and bi is the number of turtles such that xj < xi. Otherwise, turtle i is lying.

Input

The first line of the input contains integer number n (1 <= n <= 1000). It is followed by n lines containing numbers ai and bi (0 <= ai, bi <= 1000) that describe statements of each turtle for i from 1 to n.

Output

On the first line of the output file write an integer number m -- the minimal number of turtles that must be lying, followed by m integers -- turtles that are lying. Turtles can be printed in any order. If there are different sets of m lying turtles, then print any of them.

Sample Input

5
0 2
0 3
2 1
1 2
4 0

Sample Output

2 1 4

Source



#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>

using namespace std;

const int maxn=1100;
    
int n,cnt[maxn][maxn],dp[maxn],s[maxn][3],pre[maxn],ans[maxn][2];

void init()
{
    memset(cnt,0,sizeof(cnt));
    memset(dp,-1,sizeof(dp));
}

int DP(int x)
{
    if(dp[x]!=-1) return dp[x]; 
    if(x==0) return dp[x]=0;

    int ret=0;
    for(int i=1;i<=x;i++)
    {
        if(ret<=DP(x-i)+min(cnt[x-i][n-x],i))
        {
            ret=dp[x-i]+min(cnt[x-i][n-x],i);
            pre[x]=x-i; 
            ans[x][0]=x-i;ans[x][1]=n-x;
        }
    }
    return dp[x]=ret;
}

int main()
{
while(scanf("%d",&n)!=EOF)
{
    init();
    for(int i=0;i<n;i++) 
    {
        scanf("%d%d",&s[i][0],&s[i][1]);
        cnt[s[i][0]][s[i][1]]++;
    }
    int res=DP(n);
    for(int i=n;i;i=pre[i])
    {
        for(int j=0,k=i-pre[i];k&&j<n;j++)
        {
            if(s[j][0]==ans[i][0]&&s[j][1]==ans[i][1])
            {
                k--;
                s[j][2]=1;
            }
        }
    }
    printf("%d",n-res);
    for(int i=0;i<n;i++) if(!s[i][2]) printf(" %d",i+1); 
    putchar(10);
}
    return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值