Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 591 Accepted Submission(s): 359
for(int i=1;i<=N;++i)
for(int j=N,t;j>i;—j)
if(P[j-1] > P[j])
t=P[j],P[j]=P[j-1],P[j-1]=t;
#include <bits/stdc++.h> using namespace std; const int maxn = 1e5 + 10; int C[maxn]; int n; inline int lowbit(int x) { return x & (-x); } int sum(int x) { int ret = 0; while(x > 0) { ret += C[x]; x -= lowbit(x); } return ret; } void add(int x, int d) { while(x <= maxn) { C[x] += d; x += lowbit(x); } } int a[maxn]; int r[maxn], l[maxn]; int id[maxn]; int main() { int t; scanf("%d", &t); int cas = 0; while(t--) { memset(C, 0, sizeof(C)); scanf("%d", &n); for(int i = 1; i <= n; i++) scanf("%d", &a[i]); for(int i = n; i >= 1; i--) { add(a[i], 1); r[i] = sum(a[i] - 1); id[a[i]] = i; } //for(int i = 1; i <= n; i++) printf("%d ", r[i]); puts(""); vector<int> ans; for(int i = 1; i <= n; i++) { int ll = min(id[i], i); int rr = id[i] + r[id[i]]; //printf("check %d %d %d\n", i, ll, rr); ans.push_back(rr - ll); } printf("Case #%d:", ++cas); for(int i = 0; i < ans.size(); i++) { printf(" %d", ans[i]); }puts(""); } }
原文:http://www.cnblogs.com/lonewanderer/p/5719457.html