向宇同桌:考试中不要发
投递快手等公司10个岗位 >
0 点赞 评论 收藏
分享
(*^__^*)……:第二题ac代码: #include <bits/stdc++.h> using namespace std; long long f[30], e[30]; int a[1050000]; void divide(int l, int r,int x); int main(){ int n, m; scanf("%d", &n); m = pow(2,n); for (int i=0; i<m; ++i){ scanf("%d", &a[i]); } divide(0,m,n); int t, p; long long ans; scanf("%d", &t); while (t--){ scanf("%d", &p); for (int i=1; i<=p; ++i) f[i] = pow(2,i+n-2) - e[i] - f[i]; ans = 0; for (int i=1; i<=n; ++i){ ans += f[i]; } cout << ans << endl; } } void divide(int l, int r,int x){ if (x==0){ return; } int mid = (l+r) / 2; divide(l,mid,x-1); divide(mid,r,x-1); int nowp = mid; for (int i=l; i<mid; i++){ while (nowp < r && a[nowp] < a[i]) nowp++; f[x] += (nowp-mid); } int i=l, j=mid, ri, rj; while (i<mid && j<r){ if (a[i] == a[j]){ ri = rj = 1; while (ri+i < mid && a[ri+i] == a[i]) ri++; while (rj+j < r && a[rj+j] == a[j]) rj++; e[x] += ri * rj; i += ri; j += rj; continue; } if (a[i] > a[j]) j++; else i++; } vector<int> v; i=l; j=mid; while (i<mid || j<r){ if (i==mid) { v.push_back(a[j++]); continue; } if (j==r) { v.push_back(a[i++]); continue; } if (a[i] < a[j]) v.push_back(a[i++]); else v.push_back(a[j++]); } for (int i=l; i<r; ++i) a[i] = v[i-l]; }
投递腾讯等公司10个岗位 >
0 点赞 评论 收藏
分享
关注他的用户也关注了: