#include <bits/stdc++.h> #define nmax 250010 using namespace std; typedef long long ll; int n,idx=0; ll ans=0; ll s[nmax]; //单调递增的栈 int main(){ cin>>n; ll a,b; scanf("%lld%lld",&a,&b); s[0]=b; for (int i=1; i<n; i++) { scanf("%lld%lld",&a,&b); //把b入栈 while(s[idx]>b){ ans++; idx--; } if(s[idx]!=b) s[++idx]=b; } //统计栈内不同的元素个数 for (int i=idx; i>0; i--){ if(s[i]!=s[i-1]) ans++; } ans++; cout<<ans<<endl; return 0; }
原文:https://www.cnblogs.com/jiecaoer/p/11296347.html