D1T1:异或粽子

显然令b[]为a[]的前缀和,那么就是在b[]中任取两数异或,求异或结果前k大和。

于是暴力$O(n^2)$显然,60pts。

 1 #include<cstdio>
 2 #include<algorithm>
 3 #define rep(i,l,r) for (int i=(l); i<=(r); i++)
 4 typedef long long ll;
 5 using namespace std;
 6 
 7 const int N=2000010;
 8 int n,k,tot;
 9 ll ans,a[N],b[N],s[N];
10 
11 int main(){
12     freopen("xor.in","r",stdin);
13     freopen("xor.out","w",stdout);
14     scanf("%d%d",&n,&k);
15     rep(i,1,n) scanf("%lld",&a[i]),b[i]=b[i-1]^a[i];
16     rep(i,1,n) rep(j,0,i-1) s[++tot]=b[i]^b[j];
17     sort(s+1,s+tot+1);
18     for (int i=tot; i>=tot-k+1; i--) ans+=s[i];
19     printf("%lld\n",ans);
20     return 0;
21 }
View Code

相关文章:

  • 2021-06-17
  • 2021-05-20
  • 2021-12-17
  • 2021-09-13
  • 2021-06-18
  • 2021-08-30
  • 2022-12-23
猜你喜欢
  • 2021-12-16
  • 2021-09-07
  • 2022-01-15
  • 2021-12-11
  • 2022-12-23
  • 2021-08-07
相关资源
相似解决方案