Joke with Turtles - POJ 2168 dp

Joke with Turtles
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 1223 Accepted: 323 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

题意:在一个队列中,可能有多个人并排的情况,每个人要报出他前面和后面各有多少人,问最多有多少人说的是正确的。【好吧……其实是乌龟。

思路:这道题和HDU4293一样,先算出num[q][h]的数量,然后dp[i][j]表示当i+1到j的人是并排时前j个人最多有多少是正确的,最后输出记录路径。

ACD代码如下:

#include<cstdio>
#include<cstring>
#include<vector>
#include<algorithm>
using namespace std;
int dp[1010][1010],f[1010],p[1010],num[1010][1010],vis[1010];
vector <int> vc[1010][1010];
int main()
{
    int n,i,j,k,m,q,h,pos,ans;
    scanf("%d",&n);
    for(i=1;i<=n;i++)
    {
        scanf("%d%d",&q,&h);
        vc[q][h].push_back(i);
        if(q+h<n)
        {
            num[q][h]++;
            if(num[q][h]>n-q-h)
              num[q][h]=n-q-h;
        }
    }
    for(j=1;j<=n;j++)
       for(i=0;i<j;i++)
       {
           dp[i+1][j]=f[i]+num[i][n-j];
           if(dp[i+1][j]>f[j])
           {
               f[j]=dp[i+1][j];
               p[j]=i;
           }
       }
    j=n;
    while(j)
    {
        i=p[j];
        k=num[i][n-j];
        for(m=0;m<k;m++)
           vis[vc[i][n-j][m]]=1;
        j=i;
    }
    printf("%d",n-f[n]);
    for(i=1;i<=n;i++)
       if(vis[i]==0)
         printf(" %d",i);
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值