3 solutions
-
10
题意
样例解释:
面朝--------------> 后 10 3 7 4 12 2 前
思路
这道题用了降序的单调栈
后面的人比前面的人高的话,就可以看到这个人(保留在栈内);比前面的人矮的话,就看不到这个人(出栈)。最后能看到的总人数加上能看到这个人的人数(栈.size())
自己模拟一下[1]
代码
#include <bits/stdc++.h> // 不开龙龙见祖宗 #define int long long using namespace std; const int N = 8e4 + 5; int a[N],top,cnt; bool cmp(int b,int c){ return b > c; } signed main(int argc, char **argv){ int n; cin >> n; for (int i = 0;i < n;i++){ int x; cin >> x; top = lower_bound(a,a + top + 1,x,cmp) - a; // 比 x 矮的看不到 x,记得改排序规则 a[top] = x; cnt += top; // 比 x 大且站在 x 后面的都可以看到 x } cout << cnt; return 0; }
从最后一个人开始向前推
模拟栈:
↩︎1: 10 2: 10 3 3: 10 7 解释:3 比 7 矮,看不到 7 4: 10 7 4 5: 12 解释:所有人都比 12 矮,没人能看到 12 6: 12 2
-
0
xzy的题解
思路:每次输入一头牛的身高找到比牛矮小的出栈 剩下的牛也可以看到那个这头牛 ans为栈 中牛个数
#include <bits/stdc++.h> #include<map> #include<unordered_map> #include<iostream> #include<algorithm> using namespace std; int n,t; long long ans; stack<int>a; int main(){ cin>>n; for(int i=1;i<=n;i++){ cin>>t; while(!a.empty()&&a.top()<=t)//入栈 //思路每次输入一头牛的身高找到比牛矮小的出战剩下的也可以看到那个这头牛 ans为栈 中牛个数 a.pop() ; ans+=a.size(); a.push(t) ; } cout<<ans; }
- 1
Information
- ID
- 1923
- Time
- 1000ms
- Memory
- 125MiB
- Difficulty
- 3
- Tags
- # Submissions
- 37
- Accepted
- 11
- Uploaded By