POJ 2168 Joke with Turtles(DP)
Description
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
Output
题目大意:有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)的更多相关文章
- poj - 1953 - World Cup Noise(dp)
题意:n位长的01序列(0 < n < 45),但不能出现连续的两个1,问序列有多少种. 题目链接:id=1953" target="_blank">h ...
- POJ 1485:Fast Food(dp)&& 面试题
题目链接 题意 给出 n 个餐厅,m 个停车场,现在要将 n 个餐厅中的 m 个变成停车场,使得每个餐厅到最近的停车场的距离之和最短,输出哪个餐厅变成停车场和它服务哪些餐厅,还有最短距离之和. 思路 ...
- poj - 1050 - To the Max(dp)
题意:一个N * N的矩阵,求子矩阵的最大和(N <= 100, -127 <= 矩阵元素 <= 127). 题目链接:http://poj.org/problem?id=1050 ...
- POJ 2533——Longest Ordered Subsequence(DP)
链接:http://poj.org/problem?id=2533 题解 #include<iostream> using namespace std; ]; //存放数列 ]; //b[ ...
- POJ 3666 Making the Grade (DP)
题意:输入N, 然后输入N个数,求最小的改动这些数使之成非严格递增即可,要是非严格递减,反过来再求一下就可以了. 析:并不会做,知道是DP,但就是不会,菜....d[i][j]表示前 i 个数中,最大 ...
- poj 3267 The Cow Lexicon(dp)
题目:http://poj.org/problem?id=3267 题意:给定一个字符串,又给n个单词,求最少删除字符串里几个字母,能匹配到n个单词里 #include <iostream> ...
- 【POJ 3176】Cow Bowling(DP)
题 Description The cows don't use actual bowling balls when they go bowling. They each take a number ...
- 【POJ】3616 Milking Time(dp)
Milking Time Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 10898 Accepted: 4591 Des ...
- 【POJ】2385 Apple Catching(dp)
Apple Catching Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 13447 Accepted: 6549 D ...
随机推荐
- python 实现远程上传文件夹
python2 upload.py "ip" "root" "password" "22" "Only Pro ...
- datagrid中设置编辑,删除列是否可以访问
foreach (RepeaterItem Item in rpt_Result.Items) { LinkButton edit = (LinkButton)Item.FindControl(&qu ...
- 关于SQLNET.AUTHENTICATION_SERVICES= (NTS) 的解释
原文转自:http://www.360doc.com/content/12/0207/12/3446769_184740592.shtml 标题所代表的意思为 使用操作系统本地验证,一般不 ...
- vue.js中的slot
vue.js 中的 slot 一.slot 的作用 调用组件的时候,对于数据,我们会用props将数据从父组件传至子组件.但是,如果从父组件到子组件,单纯是页面局部渲染的改变,slot会更合适. 二. ...
- 重置mysql5.7.25临时密码
安装完mysql之后,登陆以后,不管运行任何命令,总是提示这个:mac mysql error You must reset your password using ALTER USER statem ...
- springmvc处理器拦截器
处理器拦截器(interceptor)是做什么用的? 想知道处理拦截器做什么用的,你要先了解下处理·流程链·. 前端控制器(dispatcherServlet)接收到请求,通过handleMappin ...
- Ubuntu 16.04 swoole扩展安装注意!!!
前言:目前很多项目估计常常会用到swoole扩展,如个人使用Ubuntu虚拟机安装扩展,这里总结一下遇到的问题: 一.先保证服务器时间同步当前地区时间,如北京时间: 1.设定时区 如:设定时区:dpk ...
- php使用file_get_contents 或者curl 发送get/post 请求 的方法总结
file_get_contents模拟GET/POST请求 模拟GET请求: <?php $data = array( 'name'=>'zhezhao', 'age'=>'23' ...
- unity开发c#代码
1.摄像头跟随主角移动,并支持旋转. 开发过程中需要摄像头以一定距离跟随player,同时会进行旋转,属于一种常见的跟随方式. using UnityEngine; using System.Coll ...
- Manacher(马拉车)学习笔记
Manacher可以有效的在\(O(n)\)时间内解决一个字符串的回文子串的题目 目录 简介 讲解 推介 简单的练习 恐怖的练习QAQ 小结 简介 开头都说了,Manacher是目前解决回文子串的最有 ...