Submission #3017095


Source Code Expand

//4:11
#include<iostream>
#include<algorithm>
#include<vector>
#include<queue>
#define lol(i,n) for(int i=0;i<n;i++)
#define mod 1000000007
using namespace std;
typedef long long ll;
#define N (1<<17)
int n,m[N];
void Zaats(){
    vector<int> v;
    lol(i,n)v.push_back(m[i]);
    sort(v.begin(),v.end());
    lol(i,n){
	m[i]=lower_bound(v.begin(),v.end(),m[i])-v.begin();
    }
}
vector<int> A,B,Y;
void SetABY(){
    int maxi=-1;
    lol(i,n){
	if(maxi<m[i]){
	    maxi=m[i];
	    A.push_back(i);
	}
    }
    int mini=mod;
    for(int i=n-1;i>=0;i--){
	if(mini>m[i]){
	    mini=m[i];
	    B.push_back(i);
	}
    }
    sort(A.begin(),A.end());
    sort(B.begin(),B.end());
    vector<pair<int,int> >v;
    lol(i,n)v.push_back(make_pair(m[i],i));
    sort(v.begin(),v.end());
    lol(i,n)Y.push_back(v[i].second);
}
class RAQxRMQ{
private:
    ll dat[2*N],laz[2*N];
    void eval(int k){
	dat[k]+=laz[k];
	if(k<N-1){
	    laz[k*2+1]+=laz[k];
	    laz[k*2+2]+=laz[k];
	}
	laz[k]=0;
    }
    void ufs(int l,int r,int a,int b,int k,ll x){
	eval(k);
	if(l<=a&&b<=r){laz[k]+=x;eval(k);return;}
	if(b<=l||r<=a)return;
	ufs(l,r,a,(a+b)/2,k*2+1,x);
	ufs(l,r,(a+b)/2,b,k*2+2,x);
	dat[k]=max(dat[k*2+1],dat[k*2+2]);
    }
    ll dfs(int l,int r,int a,int b,int k){
	eval(k);
	if(l<=a&&b<=r)return dat[k];
	if(b<=l||r<=a)return 0;
	ll vl=dfs(l,r,a,(a+b)/2,k*2+1);
	ll vr=dfs(l,r,(a+b)/2,b,k*2+2);
	return max(vl,vr);
    }
public:
    void Init(){
	lol(i,2*N)dat[i]=laz[i]=0;
    }
    void Add(int l,int r,ll x){
	ufs(l,r+1,0,N,0,x);
    }
    ll Max(int l,int r){
	return dfs(l,r+1,0,N,0);
    }
};
class RSQ{
private:
    ll dat[2*N];
    ll dfs(int l,int r,int a,int b,int k){
	if(l<=a&&b<=r)return dat[k];
	if(b<=l||r<=a)return 0;
	ll vl=dfs(l,r,a,(a+b)/2,k*2+1);
	ll vr=dfs(l,r,(a+b)/2,b,k*2+2);
	return vl+vr;
    }
public:
    void Init(){
	lol(i,2*N)dat[i]=0;
    }
    void Upd(int i,int x){
	i+=N-1,dat[i]=x;
	while(i>0){
	    i=(i-1)/2;
	    dat[i]=dat[i*2+1]+dat[i*2+2];
	}
    }
    void Add(int i,int x){
	Upd(i,Sum(i,i)+x);
    }
    int Sum(int l,int r){
	return dfs(l,r+1,0,N,0);
    }
};
ll Tentou(){
    RSQ seg;seg.Init();
    ll res=0;
    lol(i,n){
	res+=seg.Sum(m[i]+1,n);
	seg.Add(m[i],1);
    }
    return res;
}
bool Topo(ll x1,ll y1,ll x2,ll y2){
    return (x1<x2&&y1>y2);
}
ll L,R;
void BNS(ll x,ll y){
    ll l,r,mid;
    l=-1,r=B.size();
    while(l<r-1){
	mid=(l+r)/2;
	if(B[mid]>x)r=mid;
	else l=mid;
    }
    L=r;
    l=-1,r=B.size();
    while(l<r-1){
	mid=(l+r)/2;
	if(m[B[mid]]<y)l=mid;
	else r=mid;
    }
    R=l;
}
int main(){
    cin>>n;
    lol(i,n)cin>>m[i];
    Zaats();
    SetABY();
    RAQxRMQ seg;seg.Init();
    ll ans=0,px=0,py1=0,py2=0,bx=-1,by=-1;
    lol(i,A.size()){
	ll ax=A[i],ay=m[A[i]];
	for(;px<n;px++){
	    ll x=px,y=m[px];
	    if(!(x<=ax))break;
	    if(Topo(bx,by,x,y)){
		BNS(x,y+1);
		seg.Add(L,R,-1);
	    }
	    if(Topo(bx,by+1,x,y)){
		BNS(x,y);
		seg.Add(L,R,-1);
	    }
	}
	for(;py1<n;py1++){
	    ll x=Y[py1],y=m[Y[py1]];
	    if(!(y<ay))break;
	    if(Topo(ax,ay,x,y)){
		BNS(x,y+1);
		seg.Add(L,R,1);
	    }
	}
	for(;py2<n;py2++){
	    ll x=Y[py2],y=m[Y[py2]];
	    if(!(y<=ay))break;
	    if(Topo(ax,ay+1,x,y)){
		BNS(x,y);
		seg.Add(L,R,1);
	    }
	}
	ans=max(ans,seg.Max(0,B.size()-1));
	bx=ax,by=ay;
    }
    ll t=Tentou();
    if(t==0){
	bool same=false;
	lol(i,n-1)if(m[Y[i]]==m[Y[i+1]])same=true;
	cout<<1-same<<endl;
    }
    else cout<<t-1-ans<<endl;
    return 0;
}

Submission Info

Submission Time
Task 5 - バブルソート (Bubble Sort)
User ynymxiaolongbao
Language C++14 (GCC 5.4.1)
Score 100
Code Size 3631 Byte
Status AC
Exec Time 204 ms
Memory 8544 KB

Judge Result

Set Name 01 02 03 04 05 06 07 08 09 10
Score / Max Score 10 / 10 10 / 10 10 / 10 10 / 10 10 / 10 10 / 10 10 / 10 10 / 10 10 / 10 10 / 10
Status
AC × 10
AC × 10
AC × 10
AC × 10
AC × 10
AC × 10
AC × 10
AC × 10
AC × 12
AC × 15
Set Name Test Cases
01 01-01.txt, 01-02.txt, 01-03.txt, 01-04.txt, 01-05.txt, 01-06.txt, 01-07.txt, 01-08.txt, 01-09.txt, 01-10.txt
02 02-01.txt, 02-02.txt, 02-03.txt, 02-04.txt, 02-05.txt, 02-06.txt, 02-07.txt, 02-08.txt, 02-09.txt, 02-10.txt
03 03-01.txt, 03-02.txt, 03-03.txt, 03-04.txt, 03-05.txt, 03-06.txt, 03-07.txt, 03-08.txt, 03-09.txt, 03-10.txt
04 04-01.txt, 04-02.txt, 04-03.txt, 04-04.txt, 04-05.txt, 04-06.txt, 04-07.txt, 04-08.txt, 04-09.txt, 04-10.txt
05 05-01.txt, 05-02.txt, 05-03.txt, 05-04.txt, 05-05.txt, 05-06.txt, 05-07.txt, 05-08.txt, 05-09.txt, 05-10.txt
06 06-01.txt, 06-02.txt, 06-03.txt, 06-04.txt, 06-05.txt, 06-06.txt, 06-07.txt, 06-08.txt, 06-09.txt, 06-10.txt
07 07-01.txt, 07-02.txt, 07-03.txt, 07-04.txt, 07-05.txt, 07-06.txt, 07-07.txt, 07-08.txt, 07-09.txt, 07-10.txt
08 08-01.txt, 08-02.txt, 08-03.txt, 08-04.txt, 08-05.txt, 08-06.txt, 08-07.txt, 08-08.txt, 08-09.txt, 08-10.txt
09 09-01.txt, 09-02.txt, 09-03.txt, 09-04.txt, 09-05.txt, 09-06.txt, 09-07.txt, 09-08.txt, 09-09.txt, 09-10.txt, 09-11.txt, 09-12.txt
10 10-01.txt, 10-02.txt, 10-03.txt, 10-04.txt, 10-05.txt, 10-06.txt, 10-07.txt, 10-08.txt, 10-09.txt, 10-10.txt, 10-11.txt, 10-12.txt, 10-13.txt, 10-14.txt, 10-15.txt
Case Name Status Exec Time Memory
01-01.txt AC 6 ms 6400 KB
01-02.txt AC 5 ms 6400 KB
01-03.txt AC 5 ms 6400 KB
01-04.txt AC 6 ms 6528 KB
01-05.txt AC 5 ms 6400 KB
01-06.txt AC 5 ms 6400 KB
01-07.txt AC 6 ms 6528 KB
01-08.txt AC 5 ms 6400 KB
01-09.txt AC 4 ms 6400 KB
01-10.txt AC 5 ms 6400 KB
02-01.txt AC 8 ms 6400 KB
02-02.txt AC 9 ms 6528 KB
02-03.txt AC 7 ms 6528 KB
02-04.txt AC 7 ms 6528 KB
02-05.txt AC 8 ms 6528 KB
02-06.txt AC 8 ms 6528 KB
02-07.txt AC 6 ms 6528 KB
02-08.txt AC 7 ms 6528 KB
02-09.txt AC 7 ms 6528 KB
02-10.txt AC 8 ms 6528 KB
03-01.txt AC 11 ms 6528 KB
03-02.txt AC 11 ms 6528 KB
03-03.txt AC 10 ms 6528 KB
03-04.txt AC 10 ms 6528 KB
03-05.txt AC 10 ms 6528 KB
03-06.txt AC 11 ms 6528 KB
03-07.txt AC 14 ms 6528 KB
03-08.txt AC 11 ms 6528 KB
03-09.txt AC 10 ms 6528 KB
03-10.txt AC 10 ms 6528 KB
04-01.txt AC 49 ms 6744 KB
04-02.txt AC 45 ms 6808 KB
04-03.txt AC 35 ms 6808 KB
04-04.txt AC 37 ms 6936 KB
04-05.txt AC 40 ms 6744 KB
04-06.txt AC 36 ms 6744 KB
04-07.txt AC 26 ms 7000 KB
04-08.txt AC 36 ms 6744 KB
04-09.txt AC 33 ms 6808 KB
04-10.txt AC 38 ms 6744 KB
05-01.txt AC 116 ms 7088 KB
05-02.txt AC 81 ms 7216 KB
05-03.txt AC 68 ms 7216 KB
05-04.txt AC 71 ms 7216 KB
05-05.txt AC 79 ms 7152 KB
05-06.txt AC 70 ms 7088 KB
05-07.txt AC 101 ms 7088 KB
05-08.txt AC 74 ms 7088 KB
05-09.txt AC 64 ms 7344 KB
05-10.txt AC 72 ms 7120 KB
06-01.txt AC 134 ms 7556 KB
06-02.txt AC 119 ms 7940 KB
06-03.txt AC 101 ms 7940 KB
06-04.txt AC 105 ms 7940 KB
06-05.txt AC 105 ms 8068 KB
06-06.txt AC 101 ms 7556 KB
06-07.txt AC 74 ms 8324 KB
06-08.txt AC 101 ms 7428 KB
06-09.txt AC 92 ms 8068 KB
06-10.txt AC 109 ms 7588 KB
07-01.txt AC 196 ms 7648 KB
07-02.txt AC 162 ms 8032 KB
07-03.txt AC 139 ms 8032 KB
07-04.txt AC 143 ms 8032 KB
07-05.txt AC 136 ms 8288 KB
07-06.txt AC 145 ms 7648 KB
07-07.txt AC 189 ms 7648 KB
07-08.txt AC 146 ms 7648 KB
07-09.txt AC 126 ms 8160 KB
07-10.txt AC 144 ms 7840 KB
08-01.txt AC 196 ms 7648 KB
08-02.txt AC 177 ms 8032 KB
08-03.txt AC 136 ms 8032 KB
08-04.txt AC 142 ms 8032 KB
08-05.txt AC 163 ms 7904 KB
08-06.txt AC 145 ms 7648 KB
08-07.txt AC 95 ms 8544 KB
08-08.txt AC 138 ms 7648 KB
08-09.txt AC 125 ms 8160 KB
08-10.txt AC 143 ms 7840 KB
09-01.txt AC 106 ms 7088 KB
09-02.txt AC 75 ms 7152 KB
09-03.txt AC 64 ms 7152 KB
09-04.txt AC 58 ms 7344 KB
09-05.txt AC 63 ms 7088 KB
09-06.txt AC 59 ms 7216 KB
09-07.txt AC 69 ms 7120 KB
09-08.txt AC 57 ms 7088 KB
09-09.txt AC 58 ms 7088 KB
09-10.txt AC 60 ms 7092 KB
09-11.txt AC 63 ms 7104 KB
09-12.txt AC 75 ms 7152 KB
10-01.txt AC 204 ms 7648 KB
10-02.txt AC 154 ms 8032 KB
10-03.txt AC 124 ms 8032 KB
10-04.txt AC 122 ms 8288 KB
10-05.txt AC 130 ms 7648 KB
10-06.txt AC 117 ms 8032 KB
10-07.txt AC 141 ms 7840 KB
10-08.txt AC 106 ms 7648 KB
10-09.txt AC 114 ms 7648 KB
10-10.txt AC 118 ms 7652 KB
10-11.txt AC 127 ms 7664 KB
10-12.txt AC 134 ms 8032 KB
10-13.txt AC 60 ms 7648 KB
10-14.txt AC 71 ms 7648 KB
10-15.txt AC 81 ms 7648 KB