BZOJ 1113: [Poi2008]海报PLA
Posted You Siki
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了BZOJ 1113: [Poi2008]海报PLA相关的知识,希望对你有一定的参考价值。
1113: [Poi2008]海报PLA
Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 1025 Solved: 679
[Submit][Status][Discuss]
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
1 2
1 3
2 2
2 5
1 4
Sample Output
4
HINT
Source
分析
单调栈
代码
1 /*<--- Opinion --->*/ 2 3 #define HEADER 4 #define MYMATH 5 // #define FILEIO 6 // #define FSTREAM 7 // #define FREOPEN 8 #define FASTREAD 9 #define SHORTNAME 10 11 ///// HEADER FILE ///// 12 13 #ifdef HEADER 14 15 #include <cmath> 16 #include <string> 17 #include <cstdio> 18 #include <cstring> 19 #include <cstdlib> 20 #include <algorithm> 21 22 #include <sstream> 23 #include <fstream> 24 #include <iostream> 25 26 #include <set> 27 #include <map> 28 #include <list> 29 #include <queue> 30 #include <stack> 31 #include <vector> 32 #include <utility> 33 #include <functional> 34 35 #endif 36 37 ///// SHORTNAME ///// 38 39 #ifdef SHORTNAME 40 41 #define LL long long 42 #define ll long long 43 #define re register 44 #define un unsigned 45 #define rb re bool 46 #define ri re int 47 #define ui un int 48 #define rl re LL 49 #define ul un LL 50 51 #define rep (x, y) for (ri i = x; i <= y; ++i) 52 #define repi(x, y) for (ri i = x; i <= y; ++i) 53 #define repj(x, y) for (ri j = x; j <= y; ++j) 54 #define repk(x, y) for (ri k = x; k <= y; ++k) 55 56 #define upto(x) for (ri i = 1; i <= x; ++i) 57 #define dnto(x) for (ri i = x; i >= 1; --i) 58 59 #define up(x) for (ri i = 1; i <= x; ++i) 60 #define dn(x) for (ri i = x; i >= 1; --i) 61 62 #define put(x) printf("%lld ", (LL)x) 63 #define putln(x) printf("%lld\\n", (LL)x) 64 65 #endif 66 67 ///// FASTREAD ///// 68 69 #ifdef FASTREAD 70 71 const int FR_lim = 10000000; 72 73 char *FR_c; 74 75 inline void fsetup(FILE *FR_file) 76 { 77 FR_c = new char[FR_lim]; 78 fread(FR_c, 1, FR_lim, FR_file); 79 } 80 81 template <class T> 82 inline void fread(T &FR_num) 83 { 84 FR_num = 0; rb FR_neg = 0; 85 86 while (*FR_c < \'0\') 87 if (*FR_c++ == \'-\') 88 FR_neg ^= true; 89 90 while (*FR_c >= \'0\') 91 FR_num = FR_num*10 + *FR_c++ - \'0\'; 92 93 if(FR_neg)FR_num = -FR_num; 94 } 95 96 #endif 97 98 ///// FILE I/O ///// 99 100 #ifdef FILEIO 101 102 FILE *FIN = fopen("input.txt", "r"); 103 FILE *FOUT = fopen("output.txt", "w"); 104 105 #ifndef FSTREAM 106 107 #define fin FIN 108 #define fout FOUT 109 110 #endif 111 112 #endif 113 114 ///// FSTREAM ///// 115 116 #ifdef FSTREAM 117 118 std::ifstream fin("input.txt"); 119 std::ofstream fout("output.txt"); 120 121 #endif 122 123 ///// MYMATH ///// 124 125 #ifdef MYMATH 126 127 #define min(a, b) (a < b ? a : b) 128 #define max(a, b) (a > b ? a : b) 129 130 #define Min(a, b) a = min(a, b) 131 #define Max(a, b) a = max(a, b) 132 133 #define abs(x) (x < 0 ? -x : x) 134 135 #define sqr(x) ((x)*(x)) 136 137 #endif 138 139 ///// _MAIN_ ///// 140 141 void _main_(void); 142 143 signed main(void) 144 { 145 146 #ifdef FREOPEN 147 freopen("input.txt", "r", stdin); 148 freopen("output.txt", "w", stdout); 149 #endif 150 151 _main_(); 152 153 #ifdef FILEIO 154 fclose(FIN); 155 fclose(FOUT); 156 #endif 157 158 #ifdef FREOPEN 159 fclose(stdin); 160 fclose(stdout); 161 #endif 162 163 #ifdef FSTREAM 164 fin.close(); 165 fout.close(); 166 #endif 167 168 return 0; 169 } 170 171 /*<--- Main --->*/ 172 173 #define N 250005 174 175 int n; 176 int ans; 177 int h[N]; 178 int stk[N]; 179 int tot = 0; 180 181 void _main_(void) 182 { 183 fsetup(stdin); 184 185 fread(n); 186 187 ri x; 188 189 up(n) 190 { 191 fread(x); fread(x); 192 while (tot && stk[tot] > x)--tot; 193 if (tot && stk[tot] == x)++ans; 194 stk[++tot] = x; 195 } 196 197 putln(n - ans); 198 }
好像那天特别高兴的样子,不知不觉就敲了个不明所以的模板, 补一份正常的代码。
1 #include <bits/stdc++.h> 2 signed main(void) { 3 int n, tot = 0; 4 scanf("%d", &n); 5 std::stack<int> stk; 6 for (int i = 1; i <= n; ++i) { 7 int h; scanf("%*d%d", &h); 8 while (!stk.empty() && h < stk.top()) 9 stk.pop(); 10 if (!stk.empty() && h == stk.top()) 11 ++tot; 12 stk.push(h); 13 } 14 printf("%d\\n", n - tot); 15 }
@Author: YouSiki
以上是关于BZOJ 1113: [Poi2008]海报PLA的主要内容,如果未能解决你的问题,请参考以下文章