本文主要是介绍区间dp,POJ 2168 Joke with Turtles,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
目录
一、题目
1、题目描述
2、题目描述
2.1输入
2.2输出
3、原题链接
二、解题报告
1、思路分析
2、复杂度
3、代码详解
一、题目
1、题目描述
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.
2、题目描述
2.1输入
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.
Sample Input
5 0 2 0 3 2 1 1 2 4 0
2.2输出
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.2 1 4
3、原题链接
2168 -- Joke with Turtles (poj.org)
二、解题报告
1、思路分析
和HDU4293做法是一样的,那道题问最大说真话人数,这道题问最小说谎人数,总人数减去最大说真话人数就是答案了
然后要求输出任意一组说谎的那些人,那么我们dp的时候保存路径即可
2、复杂度
时间复杂度: O(n^2)空间复杂度:O(n^2)
3、代码详解
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <queue>
#include <vector>
#include <cstring>
#include <functional>
#include <bitset>
using namespace std;
const int N = 1005;
typedef pair<int, int> pii;
int num[N][N], dp[N], pre[N];
vector<int> cnt[N][N];
bitset<N> vis;
void solve()
{int n;cin >> n, memset(num, 0, sizeof(num)), memset(dp, 0, sizeof(dp));for (int i = 1, a, b; i <= n; i++){cin >> a >> b, cnt[a][b].push_back(i), num[a][b] += (a + b < n && num[a][b] < n - a - b);}for (int i = 1; i <= n; i++){for (int j = 0; j < i; j++){if (dp[j] + num[j][n - i] > dp[i]){dp[i] = dp[j] + num[j][n - i];pre[i] = j;}}}cout << n - dp[n];int p = n;while (p){int a = pre[p], b = n - p;for (int i = 0, sz = num[a][b]; i < sz; i++)vis.set(cnt[a][b][i]);p = a;}for (int i = 1; i <= n; i++)if (!vis[i])cout << ' ' << i;
}int main()
{ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);// freopen("in.txt", "r", stdin);int _ = 1;// cin >> _;while (_--)solve();return 0;
}
这篇关于区间dp,POJ 2168 Joke with Turtles的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!