看着纸质题做的,感觉挺水的一道题,结果居然过不了
思路和网上的正解非常相似,但是就是不知道哪里错了
我的做法复杂度更优秀,整体思路也基本一致,为什么不对呢?
可能还是没想明白吧,等我明天再想想。
1 #include<iostream> 2 #include<cstdio> 3 #include<algorithm> 4 5 using namespace std; 6 7 typedef long long ll; 8 const ll mod = 99999997; 9 10 ll read(){ 11 ll ans = 0; 12 char last = ' ',ch = getchar(); 13 while(ch < '0'||ch > '9')last = ch,ch = getchar(); 14 while('0' <= ch&&ch <= '9')ans = ans*10+ch-'0',ch = getchar(); 15 if(last == '-')return -ans;return ans; 16 } 17 18 struct node{ 19 ll a,b; 20 bool operator <(const node& x)const{ 21 return a < x.a; 22 } 23 }nbs[100010]; 24 25 ll a[100010]; 26 ll merge_sort(int l,int r){ 27 if(l >= r)return 0; 28 int mid = (l+r)>>1,p = l,q = mid+1; 29 int b[r-l+1],cnt = 0; 30 ll ans = 0; 31 ans += merge_sort(l,mid); 32 ans += merge_sort(mid+1,r); 33 while(p <= mid||q <= r){ 34 if(p > mid||(q <= r&&a[q] < a[p])){ 35 b[cnt++] = a[q++]; 36 ans += mid-p+1; 37 } 38 else b[cnt++] = a[p++]; 39 } 40 for(int i = l;i <= r;i++)a[i] = b[i-l]; 41 return ans%mod; 42 } 43 44 int n; 45 46 int main(){ 47 n = read(); 48 for(int i = 1;i <= n;i++)nbs[i].a = read(); 49 for(int i = 1;i <= n;i++)nbs[i].b = read(); 50 sort(nbs+1,nbs+n+1); 51 for(int i = 1;i <= n;i++)a[i] = nbs[i].b; 52 cout << merge_sort(1,n) << ' '; 53 return 0; 54 }
2020-10-30
来填坑了。。。
当年的我,太可爱了,连离散化都不知道。。。
我自己的ac代码:(现在会用树状数组了)
1 int a[Maxn],b[Maxn],c[Maxn],t[Maxn]; 2 int n,ans; 3 4 void add(int x,int y){ 5 while(x <= n){ 6 t[x] = (1ll*t[x]+y)%mod; 7 x += x&-x; 8 } 9 } 10 11 int ask(int x){ 12 int ans = 0; 13 while(x){ 14 ans += t[x]; 15 x -= x&-x; 16 } 17 return ans; 18 } 19 20 void work1(int a1[]){ 21 rep(i,1,n)a1[i] = b[i] = read(); 22 sort(a1+1,a1+n+1); 23 rep(i,1,n)b[i] = lower_bound(a1+1,a1+n+1,b[i])-a1; 24 rep(i,1,n)a1[b[i]] = i; 25 } 26 27 void work2(int a1[]){ 28 rep(i,1,n)a1[i] = b[i] = read(); 29 sort(b+1,b+n+1); 30 rep(i,1,n)a1[i] = lower_bound(b+1,b+n+1,a1[i])-b; 31 } 32 33 int main(){ 34 n = read(); 35 work1(a),work2(c); 36 per(i,n,1){ 37 ans = (1ll*ans+ask(a[c[i]]-1))%mod; 38 add(a[c[i]],1); 39 } 40 cout << ans; 41 return 0; 42 }