10 1 3 6 9 0 8 5 7 4 2
16
Ignatius.L | We have carefully selected several similar problems for you: 1698 1540 1542 1255 1754
ACcode:
#pragma warning(disable:4786)//使命名长度不受限制 #pragma comment(linker, "/STACK:102400000,102400000")//手工开栈 #include <map> #include <set> #include <queue> #include <cmath> #include <stack> #include <cctype> #include <cstdio> #include <cstring> #include <stdlib.h> #include <iostream> #include <algorithm> #define rd(x) scanf("%d",&x) #define rd2(x,y) scanf("%d%d",&x,&y) #define rds(x) scanf("%s",x) #define rdc(x) scanf("%c",&x) #define ll long long int #define maxn 100005 #define mod 1000000007 #define INF 0x3f3f3f3f //int 最大值 #define FOR(i,f_start,f_end) for(int i=f_start;i<=f_end;++i) #define MT(x,i) memset(x,i,sizeof(x)) #define PI acos(-1.0) #define E exp(1) using namespace std; int t[maxn<<2],a[maxn],n,ans; void creatTree(int st,int left,int right){ t[st]=0; if(left==right)return; int m=(left+right)>>1; int temp=st<<1; creatTree(temp,left,m); creatTree(temp+1,m+1,right); } void updata(int data,int st,int left,int right){ if(left==right){t[st]++;return;} int m=(left+right)>>1; int temp=st<<1; if(data<=m)updata(data,temp,left,m); else updata(data,temp+1,m+1,right); t[st]=t[temp]+t[temp+1]; } int qurey(int st,int l,int r,int left,int right){ if(l<=left&&r>=right)return t[st]; int m=(left+right)>>1; int temp=st<<1; int ret=0; if(l<=m)ret+=qurey(temp,l,r,left,m); if(r>m)ret+=qurey(temp+1,l,r,m+1,right); return ret; } int main(){ while(rd(n)!=EOF){ creatTree(1,0,n-1); ans=0; FOR(i,0,n-1){ rd(a[i]); ans+=qurey(1,a[i],n-1,0,n-1); updata(a[i],1,0,n-1); } int temp=ans; FOR(i,0,n-1){ temp=temp-(a[i]<<1)+n-1; ans=ans>temp?temp:ans; } printf("%d\n",ans); } return 0; } /* 10 1 3 6 9 0 8 5 7 4 2 */