这道题需要二分在我前面第一个比我高的人的位置,用二分是因为二分的时间复杂度较低这里的check就是判断当前这个是否大于a,题目说要用快读,第一次竟然没看到=-=
快读就是用char把要读的东西读进来,然后在转成int,比普通的要快
#include<bits/stdc++.h>
using namespace std;
struct Fast_IO
{
inline Fast_IO operator>>(int &x)
{
int c;
for(x=0;!isdigit(c=getchar()););
for(;isdigit(c);c=getchar())x=(x<<3)+(x<<1)+(c^48);
return *this;
}
}IO;
int n,a;
int s[1000005],top,p[1000005];
int main()
{
IO>>n;
long long ans=0;
for(int i=1;i<=n;++i)
{
IO>>a;
if(!top||a>=s[top])
s[++top]=a,p[top]=i;
else
{
int l=0,r=top,mid;
while(l<r)
{
if(s[mid=l+r>>1]>a)
r=mid;
else
l=mid+1;
}
ans+=i-p[l];
}
}
printf("%lld\n",ans);
return 0;
}
来源:zr