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. 由inline-block小例子引申出的一些问题,及IE6、IE7兼容性解决方案

    使用场景分析: 常见的对块与块之间的横向排列处理 对同级所有元素使用display:inline-block; , 之后块与块直接会产生间隙问题 解决办法: 给父级设 font-size:0; 别高兴 ...

  2. SpringBoot非官方教程 | 第二十一篇: springboot集成JMS

    转载请标明出处: http://blog.csdn.net/forezp/article/details/71024024 本文出自方志朋的博客 springboot对JMS提供了很好的支持,对其做了 ...

  3. c# 开发可替换的通用序列化库

    开篇继续吹牛.... 其实没有什么可吹的了,哈哈哈哈哈 主要是写一个通用库,既可以直接用,又方便替换,我的序列化都是采用第三方的哈. 我不上完整代码了,只是给大家讲讲过程. 1.写一个序列化的类,我是 ...

  4. SSM项目中用ajax尝试实现controller请求中重定向不起作用的问题

    首先我在controller中有一个添加数据的方法: @RequestMapping(value="/emp",method=RequestMethod.POST) public ...

  5. LINUX 启动图形界面和查看运行级别

    runlevel  查看当前运行级别 cat /etc/inittab   可以查看7个运行级别 init 6  ==  reboot == shuttdown -r now   都是表示重启的命令 ...

  6. navicat for MySQL连接本地数据库时报1045错误的解决方法

    navicat for MySQL 连接本地数据库出现1045错误 如下图: 说明连接mysql时数据库密码错误,需要修改密码后才可解决问题: 解决步骤如下: 1.首先打开命令行:开始->运行- ...

  7. python__标准库 : urllib2

    urllib,urllib2 urllib库主要用 urlencode()把字典转换成url的get参数或者post参数 或者用 quote() 进行编码unquote进行解码 用urllib2.Pr ...

  8. 【tp5.1】微信公众号授权登录及获取信息录入数据库

    微信公众号开发文档链接:https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1445241432 微信公众号授权登录分为两种: 1.以 ...

  9. 利用phpspreadsheet切割excel大文件

    背景: 利用phpspreadsheet可以轻松的解析excel文件,但是phpspreadsheet的内存消耗也是比较大的,我试过解析将近5M的纯文字excel内存使用量就会超过php默认的最大内存 ...

  10. 网站安全检测 漏洞检测 对thinkphp通杀漏洞利用与修复建议

    thinkphp在国内来说,很多站长以及平台都在使用这套开源的系统来建站,为什么会这么深受大家的喜欢,第一开源,便捷,高效,生成静态化html,第二框架性的易于开发php架构,很多第三方的插件以及第三 ...