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:
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.
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);
}