BZOJ 1113 海报 单调栈
题目链接:
https://www.lydsy.com/JudgeOnline/problem.php?id=1113
题目大意:
N个矩形,排成一排. 现在希望用尽量少的矩形海报Cover住它们.
思路:
直接维护一个递增的单调栈,注意如果栈顶元素和当前值相同,那么当前值不加入栈中。
1 #include<bits/stdc++.h> 2 #define IOS ios::sync_with_stdio(false);//不可再使用scanf printf 3 #define Max(a, b) ((a) > (b) ? (a) : (b))//禁用于函数,会超时 4 #define Min(a, b) ((a) < (b) ? (a) : (b)) 5 #define Mem(a) memset(a, 0, sizeof(a)) 6 #define Dis(x, y, x1, y1) ((x - x1) * (x - x1) + (y - y1) * (y - y1)) 7 #define MID(l, r) ((l) + ((r) - (l)) / 2) 8 #define lson ((o)<<1) 9 #define rson ((o)<<1|1) 10 #define Accepted 0 11 #pragma comment(linker, "/STACK:102400000,102400000")//栈外挂 12 using namespace std; 13 inline int read() 14 { 15 int x=0,f=1;char ch=getchar(); 16 while (ch<'0'||ch>'9'){if (ch=='-') f=-1;ch=getchar();} 17 while (ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();} 18 return x*f; 19 } 20 typedef long long ll; 21 const int maxn = 1000000 + 10; 22 const int MOD = 1000000007;//const引用更快,宏定义也更快 23 const int INF = 1e9 + 7; 24 const double eps = 1e-6; 25 26 stack<int>q; 27 int main() 28 { 29 int n, ans = 0, x, y; 30 scanf("%d", &n); 31 for(int i = 1; i <= n; i++) 32 { 33 scanf("%d%d", &x, &y); 34 while(!q.empty() && q.top() > y) 35 { 36 q.pop(); 37 ans++; 38 } 39 if(!q.empty() && q.top() == y)continue; 40 q.push(y); 41 } 42 ans += q.size(); 43 printf("%d\n", ans); 44 return Accepted; 45 }
越努力,越幸运