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.

题目大意:有n只乌龟,把他们放到一个数轴上(每个点可以放多只乌龟)。然后每只乌龟会告诉你它前面有多少只乌龟后面有多少只乌龟。然后有的乌龟在说谎,问最少有多少只乌龟在说话,是哪几只。

思路:假设乌龟都是朝右看的。那么把乌龟离散到1~n的范围中,若一只乌龟前面有b只,后面有a只,那么这只乌龟就在[a + 1, n - b]的范围内。那么对每只乌龟就有一个所在区间[l, r]。那么对一对乌龟,如果区间完全重叠,那么说明这两只乌龟应该站在同一个位置,所以区间[l, r]的权就为乌龟的区间等于[l, r]的总数。那么就是求不重叠区间的最大权,用DP求即可。

弱证明:如果一只乌龟认为自己在区间[l, r]中,那么[l, r]里面一定需要有l - r + 1(即n - a - b)只乌龟,所以[l, r]里面的乌龟都说的话都应该相同,这也是区间不能相交的原因。其次,得出了不能相交的区间,那么我们一定能把说谎的那些乌龟拿出来摆到1~n中空的位置去,使得说真话的乌龟说真话。

填坑:并非两只乌龟在同一个位置就说话共真假,有可能有10只乌龟认为自己在区间[1, 1]中,但是[1, 1]实际上只能容纳一只乌龟,所以至少会有9只乌龟在说谎。其次,对于a+b≥n的煞笔乌龟毫无疑问在说谎。

代码(32MS,每次交都不一样无力吐槽):

 #include <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std; const int MAXN = ; struct Node {
int l, r, pow, id;
bool operator < (const Node &rhs) const {
if(r != rhs.r) return r < rhs.r;
return l < rhs.l;
}
bool operator == (const Node &rhs) const {
return l == rhs.l && r == rhs.r;
}
}; int n;
Node a[MAXN];
int dp[MAXN], pre[MAXN], last[MAXN];
bool use[MAXN], ans[MAXN]; void solve() {
memset(dp, , sizeof(dp));
int cur = ;
for(int i = ; i <= n; ++i) {
pre[i] = i - ;
dp[i] = dp[i - ];
last[i] = -;
while(cur < n && a[cur].r == i) {
if(dp[i] < dp[a[cur].l - ] + a[cur].pow) {
pre[i] = a[cur].l - ;
dp[i] = dp[a[cur].l - ] + a[cur].pow;
last[i] = cur;
}
++cur;
}
}
} void output() {
int cur = n;
while(cur > ) {
if(last[cur] != -) use[last[cur]] = true;
cur = pre[cur];
}
for(int i = n - ; i > ; --i)
if(use[i] && a[i] == a[i - ]) use[i - ] = true;
printf("%d", n - dp[n]);
for(int i = ; i < n; ++i) ans[a[i].id] = !use[i];
for(int i = ; i <= n; ++i) if(ans[i]) printf(" %d", i);
puts("");
} int main() {
while(scanf("%d", &n) != EOF) {
for(int i = ; i < n; ++i) {
int x, y;
scanf("%d%d", &x, &y);
a[i].l = x + ;
a[i].r = n - y;
if(x + y >= n) a[i].r = n + ;
a[i].pow = ;
a[i].id = i + ;
}
sort(a, a + n);
for(int i = ; i < n - ; ++i)
if(a[i] == a[i + ]) a[i + ].pow += a[i].pow;
for(int i = ; i < n; ++i)
a[i].pow = min(a[i].pow, a[i].r - a[i].l + );
solve();
output();
}
}

POJ 2168 Joke with Turtles(DP)的更多相关文章

  1. poj - 1953 - World Cup Noise(dp)

    题意:n位长的01序列(0 < n < 45),但不能出现连续的两个1,问序列有多少种. 题目链接:id=1953" target="_blank">h ...

  2. POJ 1485:Fast Food(dp)&& 面试题

    题目链接 题意 给出 n 个餐厅,m 个停车场,现在要将 n 个餐厅中的 m 个变成停车场,使得每个餐厅到最近的停车场的距离之和最短,输出哪个餐厅变成停车场和它服务哪些餐厅,还有最短距离之和. 思路 ...

  3. poj - 1050 - To the Max(dp)

    题意:一个N * N的矩阵,求子矩阵的最大和(N <= 100, -127 <= 矩阵元素 <= 127). 题目链接:http://poj.org/problem?id=1050 ...

  4. POJ 2533——Longest Ordered Subsequence(DP)

    链接:http://poj.org/problem?id=2533 题解 #include<iostream> using namespace std; ]; //存放数列 ]; //b[ ...

  5. POJ 3666 Making the Grade (DP)

    题意:输入N, 然后输入N个数,求最小的改动这些数使之成非严格递增即可,要是非严格递减,反过来再求一下就可以了. 析:并不会做,知道是DP,但就是不会,菜....d[i][j]表示前 i 个数中,最大 ...

  6. poj 3267 The Cow Lexicon(dp)

    题目:http://poj.org/problem?id=3267 题意:给定一个字符串,又给n个单词,求最少删除字符串里几个字母,能匹配到n个单词里 #include <iostream> ...

  7. 【POJ 3176】Cow Bowling(DP)

    题 Description The cows don't use actual bowling balls when they go bowling. They each take a number ...

  8. 【POJ】3616 Milking Time(dp)

    Milking Time Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 10898   Accepted: 4591 Des ...

  9. 【POJ】2385 Apple Catching(dp)

    Apple Catching Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 13447   Accepted: 6549 D ...

随机推荐

  1. 复制功能 js

    示例: <input class="herf" type="text" v-model="herfUrl" readonly=&quo ...

  2. HTML基础之标签简单认识

    简介 HTML(Hyper Text Markup Language)译为"超文本标记语言",主要是通过HTML标记对网页中的文本.图片.声音等内容进行描述 HTML之所以称为超文 ...

  3. 【SHOI2015】脑洞治疗仪(恶心的线段树,区间最大子段和)

    题目描述: 曾经发明了自动刷题机的发明家 SHTSC 又公开了他的新发明:脑洞治疗仪——一种可以治疗他因为发明而日益增大的脑洞的神秘装置. 为了简单起见,我们将大脑视作一个 01 序列.11代表这个位 ...

  4. FBI树

    题目描述 我们可以把由"0"和"1"组成的字符串分为三类:全"0"串称为B串,全"1"串称为I串,既含"0&q ...

  5. MySQL传输表空间使用方法

    1.目标端创建同样的表结构 CREATE TABLE `test` (       `id` int(11) DEFAULT NULL     ) ENGINE=InnoDB DEFAULT CHAR ...

  6. linux链路聚合

    配置聚合连接(网卡绑定,链路聚合): eth0 ================>>虚拟网卡team eth1 配置聚合连接 [root@Centos7-Server ~]# nmcli ...

  7. IDEA项目启动报Unable to open debugger port (127.0.0.1:51554): java.net.SocketException "socket closed"

    启动报错: Unable to open debugger port (127.0.0.1:51554): java.net.SocketException "socket closed&q ...

  8. ruby require的使用

    引用单个文件 例: 引用当前rb同目录下的file_to_require.rb先介绍3种方法 require File.join(__FILE__, '../file_to_require') req ...

  9. My First Marathon【我的第一次马拉松】

    My First Marathon A month before my first matathon, one of my ankles was injured and this meant not ...

  10. tail命令使用

    1.tail命令 命令的主要用途是将指定的文件的最后部分输出到终端,如果该文件有更新,tail会自己主动刷新. 2.tail语法 tail [ -f ] [ -c Number | -n Number ...