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. sharepoint2013配置开发环境

  2. antd不想写那么多option怎么办

    做项目的时候发现如果下拉列表选项多的时候会写很多的 Option ,但是用到下拉列表的地方又超级多.所以自己写了一个方法,哪需要就放到哪. 记录一下方法.留待以后用 selectStreetIdCha ...

  3. SpringBoot非官方教程 | 第十一篇:springboot集成swagger2,构建优雅的Restful API

    转载请标明出处: 原文首发于:https://www.fangzhipeng.com/springboot/2017/07/11/springboot-swagger2/ 本文出自方志朋的博客 swa ...

  4. Shiro 登录认证源码详解

    Shiro 登录认证源码详解 Apache Shiro 是一个强大且灵活的 Java 开源安全框架,拥有登录认证.授权管理.企业级会话管理和加密等功能,相比 Spring Security 来说要更加 ...

  5. MySQL提升课程 全面讲解MySQL架构设计

    1:并发量:同一时间处理请求数量,同一时间处理请求数量和连接数量是两个概念,连接数大于处理请求数量, MySQL参数最大连接数max_connections 这是是查询数据库当前设置的最大连接数 my ...

  6. 背景qwq

  7. mysql-介绍

    1.mysql几个重要的文件 每个数据库新建后,会产生数据库文件夹,在该文件夹下每张表均对应以下三个文件: xx.frm  存放表结构 xx.MYD    存放表数据 xx.MYI 存放表索引 mys ...

  8. redis 带入的挖矿病毒 qW3xT.2 wnTKYg 解决方法

    最近我的阿里云ecs 老是收到 云盾态势感知系统检测到异常 top -c 后发现一个 疑似病毒  /tmp/qW3xT.2 看到网友们的解决方案 试过之后效果不错,可以用的 知道wnTKYg是什么鬼之 ...

  9. C语言:类型、运算符、表达式

    看了一天书,有点累了.就写写随笔记录一下今天的复习成果吧. C语言的基本数据类型 数值型:整型数,浮点数,布尔数,复数和虚数. 非数值型:字符. 整数最基本的是int,由此引出许多变式诸如有符号整数s ...

  10. R语言学习笔记(十七):data.table包中melt与dcast函数的使用

    melt函数可以将宽数据转化为长数据 dcast函数可以将长数据转化为宽数据 > DT = fread("melt_default.csv") > DT family_ ...