BZOJ 1113 海报PLA——————单调栈

迈不过友情╰ 2021-11-05 07:02 300阅读 0赞

BZOJ 1113 海报PLA

Description
N个矩形,排成一排. 现在希望用尽量少的矩形海报Cover住它们.

Input
第一行给出数字N,代表有N个矩形.N在[1,250000] 下面N行,每行给出矩形的长与宽.其值在[1,1000000000]2 1/2 Postering

Output
最少数量的海报数.

Sample Input
5

1 2

1 3

2 2

2 5

1 4
在这里插入图片描述
Sample Output
4
在这里插入图片描述


我们可以直接忽略长度,因为长度对这一道题的结果影响不大

那么问题就抽象为:由 n n n 个长度为 1 1 1 ,高度为 h 1 , h 2 , . . . h n h_1, h_2, … h_n h1,h2,…hn 的长方形从左到右依次排列组成的柱状图,问里面包含的长方形的最少个数是多少

我们依旧维护一个单调递增栈,如果当前元素与栈顶元素相等,那么它就可以与栈顶元素在同一个矩阵里面

  1. /*--------- Hongjie ----------*/
  2. /**
  3. *        ┏┓   ┏┓+ +
  4. *       ┏┛┻━━━┛┻┓ + +
  5. *       ┃       ┃
  6. *       ┃   ━   ┃ ++ + + +
  7. *       ████━████ ┃+
  8. *       ┃       ┃ +
  9. *       ┃   ┻   ┃
  10. *       ┃       ┃ + +
  11. *       ┗━┓   ┏━┛
  12. *         ┃   ┃
  13. *         ┃   ┃ + + + +
  14. *         ┃   ┃    Code is far away from bug with the animal protecting
  15. *         ┃   ┃ +     神兽保佑,代码无bug
  16. *         ┃   ┃
  17. *         ┃   ┃  +
  18. *         ┃    ┗━━━┓ + +
  19. *         ┃        ┣┓
  20. *         ┃        ┏┛
  21. *         ┗┓┓┏━┳┓┏┛ + + + +
  22. *          ┃┫┫ ┃┫┫
  23. *          ┗┻┛ ┗┻┛+ + + +
  24. */
  25. // #include<bits/stdc++.h>
  26. #include<map>
  27. #include<set>
  28. #include<queue>
  29. #include<stack>
  30. #include<deque>
  31. #include<cmath>
  32. #include<cstdio>
  33. #include<bitset>
  34. #include<string>
  35. #include<vector>
  36. #include<cstring>
  37. #include<iostream>
  38. #include<algorithm>
  39. using namespace std;
  40. typedef long long ll;
  41. typedef pair<int ,int> P;
  42. const int INF = 0x3f3f3f3f;
  43. const int MAXN = 3e6+7;
  44. int h[MAXN];
  45. int main(){
  46. // freopen("../in.txt","r",stdin);
  47. // freopen("../out.txt","w",stdout);
  48. ios::sync_with_stdio(0);
  49. cin.tie(0);
  50. int n,x;
  51. while(cin>>n) {
  52. for(int i=0;i<n;++i) {
  53. cin>>x>>h[i];
  54. }
  55. stack<int> s;
  56. int ans = n;
  57. for(int i=0;i<n;++i) {
  58. while(!s.empty() && s.top() >= h[i]) {
  59. if(h[i]==s.top()) //如果当前元素与栈顶元素相等,
  60. ans --; //那么它就可以与栈顶元素在同一个矩阵里面
  61. s.pop();
  62. }
  63. s.push(h[i]);
  64. }
  65. cout<<ans<<endl;
  66. }
  67. return 0;
  68. }

发表评论

表情:
评论列表 (有 0 条评论,300人围观)

还没有评论,来说两句吧...

相关阅读

    相关 单调

    单调栈,设置一个数组a\[i\],用一个栈存放该数组的下标,每次存放时先将所有大于等于a\[i\]的数弹出,最后再将i存入栈中,保证后续的所有的数都大于前面一个数,栈就是单调递

    相关 单调

    通过使用栈这个简单的结构,我们可以巧妙地降低一些问题的时间复杂度。 单调栈性质: 1、若是单调递增栈,则从栈顶到栈底的元素是严格递增的。若是单调递减栈,则从栈顶到栈底的元素

    相关 单调

    单调栈 性质 单调栈是一种特殊的栈,特殊之处在于栈内的元素都保持一个单调性,可能为单调递增,也可能为单调递减。 模型 例如下图就是一个单调递增的单调栈。   ![

    相关 单调

    一、单调栈定义 单调递增栈:数据出栈的序列为单调递增序列(比站内元素小就入栈,否则将栈中比当前元素小的元素弹出后再入栈) 单调递减栈:数据出栈的序列为单调递减