CMB17. 重叠的装饰
描述
我们部门需要装饰墙,但是墙非常非常的长,有一千万米。我们会按顺序贴很多海报在上面,这些海报相互之间会重叠,请问下,最后还能看到哪些?(只看到一部分也算)输入描述
N表示N张海报输出描述
有多少张海报是可见的示例1
输入:
5 1 4 2 6 8 10 3 4 7 10
输出:
4
C++ 解法, 执行用时: 3ms, 内存消耗: 484KB, 提交时间: 2020-07-28
#include <bits/stdc++.h> using namespace std; vector <pair<int, int> > pv; bool dfs(int index, int x, int y){ if (index == pv.size()){ return true; } if (x >= y){ return false; } if(x >= pv[index].first && y <= pv[index].second){ return false; } if(y <= pv[index].first || x >= pv[index].second){ return dfs(index + 1, x, y); } if(x >= pv[index].first && y >= pv[index].second){ return dfs(index + 1, pv[index].second, y); } if(x <= pv[index].first && y <= pv[index].second){ return dfs(index + 1, x, pv[index].first); } if(x <= pv[index].first && y >= pv[index].second){ return dfs(index + 1, x, pv[index].first) || dfs(index + 1, pv[index].second, y); } return false; } int main() { int n, res = 0; scanf("%d", &n); for (int i = 0; i < n; i++){ pair<int, int> p; scanf("%d %d", &p.first, &p.second); pv.push_back(p); } for (int i = 0; i < n; i++){ if (dfs(i + 1, pv[i].first, pv[i].second)){ res++; } } printf("%d\n", res); return 0; }
C++ 解法, 执行用时: 4ms, 内存消耗: 380KB, 提交时间: 2020-10-29
#include <bits/stdc++.h> using namespace std; vector <pair<int, int> > pv; bool dfs(int index, int x, int y){ if (index == pv.size()){ return true; } if (x >= y){ return false; } if(x >= pv[index].first && y <= pv[index].second){ return false; } if(y <= pv[index].first || x >= pv[index].second){ return dfs(index + 1, x, y); } if(x >= pv[index].first && y >= pv[index].second){ return dfs(index + 1, pv[index].second, y); } if(x <= pv[index].first && y <= pv[index].second){ return dfs(index + 1, x, pv[index].first); } if(x <= pv[index].first && y >= pv[index].second){ return dfs(index + 1, x, pv[index].first) || dfs(index + 1, pv[index].second, y); } return false; } int main() { int n, res = 0; scanf("%d", &n); for (int i = 0; i < n; i++){ pair<int, int> p; scanf("%d %d", &p.first, &p.second); pv.push_back(p); } for (int i = 0; i < n; i++){ if (dfs(i + 1, pv[i].first, pv[i].second)){ res++; } } printf("%d\n", res); return 0; }