【LOJ #2869】「USACO 2018 US Open Platinum」Out of Sorts(冒泡排序)

传送门

考虑一个位置被计算的次数就是左右分分别分割的较大时间
考虑一轮冒泡排序,一个该在左边的右边的元素只会往左移一格
所以找到最右的应该在左边的元素即可求出分隔的时间

#include<bits/stdc++.h>
using namespace std;
#define cs const
#define re register
#define pb push_back
#define pii pair<int,int>
#define ll long long
#define y1 shinkle
#define tm shiki
#define data shiroha
#define fi first
#define se second
#define bg begin
cs int RLEN=1<<20|1;
inline char gc(){
    static char ibuf[RLEN],*ib,*ob;
    (ib==ob)&&(ob=(ib=ibuf)+fread(ibuf,1,RLEN,stdin));
    return (ib==ob)?EOF:*ib++;
}
inline int read(){
    char ch=gc();
    int res=0;bool f=1;
    while(!isdigit(ch))f^=ch=='-',ch=gc();
    while(isdigit(ch))res=(res+(res<<2)<<1)+(ch^48),ch=gc();
    return f?res:-res;
}
inline ll readll(){
    char ch=gc();
    ll res=0;bool f=1;
    while(!isdigit(ch))f^=ch=='-',ch=gc();
    while(isdigit(ch))res=(res+(res<<2)<<1)+(ch^48),ch=gc();
    return f?res:-res;
}
inline int readstring(char *s){
	int top=0;char ch=gc();
	while(isspace(ch))ch=gc();
	while(!isspace(ch)&&ch!=EOF)s[++top]=ch,ch=gc();
	s[top+1]='\0';return top;
}
template<typename tp>inline void chemx(tp &a,tp b){a=max(a,b);}
template<typename tp>inline void chemn(tp &a,tp b){a=min(a,b);}
pii p[100005];
int n,t[100005];
int main(){
	#ifdef Stargazer
	freopen("lx.in","r",stdin);
	#endif
	n=read();
	for(int i=1;i<=n;i++)p[i].fi=read(),p[i].se=i;
	sort(p+1,p+n+1);
	for(int i=1,mx=0;i<=n;i++){
		chemx(mx,p[i].se),t[i]=mx-i;
	}ll res=max(1,t[1]);
	for(int i=2;i<=n;i++)res+=max(t[i-1],max(t[i],1));
	cout<<res<<'\n';return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_42555009/article/details/105718694
今日推荐