http://acm.xidian.edu.cn/problem.php?id=1006
这道题真的很厉害,开始一看,数据这么大,写了线段树和dp,可想而知都超时了。
要询问这么多次,于是向三个运算符号研究,发现了一种把O(n)处理完的方法,把数都分成二进制位,a[i]保存所有以i结尾的结果为1的个数和,b[i],c[i]类似。
还有一点,a[i]*对应的进制时可能会超int,被这个也坑了好久。
#include<iostream> #include<cstring> #include<cstdio> #include<algorithm> using namespace std; int bin[100005][35],two[35],n; double a[100005],b[100005],c[100005]; int main() { two[0] = 1; for(int i = 1;i <= 30;i++) two[i] = two[i-1]*2; while(~scanf("%d",&n)) { memset(bin,0,sizeof(bin)); int maxcnt = 0; double ans1 = 0,ans2 = 0,ans3 = 0; for(int i = 1;i <= n;i++) { int x; scanf("%d",&x); ans1 -= x; ans2 -= x; ans3 -= x; int cnt = 0; while(x) { bin[i][++cnt] = x%2; x /= 2; } maxcnt = max(cnt,maxcnt); } for(int j = 1;j <= maxcnt;j++) { int last0 = 0,last1 = 0; for(int i = 1;i <= n;i++) { if(bin[i][j] == 1) { if(last1 > 0) a[i] = a[last1-1]+i-last1; else a[i] = i; b[i] = i-last0; c[i] = i; last1 = i; } else { if(i > 1) a[i] = a[i-1]; else a[i] = 0; b[i] = 0; c[i] = last1; last0 = i; } ans1 += a[i]*two[j]; ans2 += b[i]*two[j]; ans3 += c[i]*two[j]; } } printf("%.3lf %.3lf %.3lf\n",ans1/n/n,ans2/n/n,ans3/n/n); } return 0; }
时间: 2024-11-04 22:38:56