zoukankan      html  css  js  c++  java
  • CF 1368D AND, OR and square sum

    传送门

    题目:

    Gottfried learned about binary number representation. He then came up with this task and presented it to you.

    You are given a collection of nn non-negative integers a1,…,an. You are allowed to perform the following operation: choose two distinct indices 1≤i,j≤n. If before the operation ai=x, aj=y, then after the operation ai=x AND y, aj=x OR y, where AND and OR are bitwise AND and OR respectively (refer to the Notes section for formal description). The operation may be performed any number of times (possibly zero).

    After all operations are done, compute ∑(i=1n)ai^2 — the sum of squares of all ai. What is the largest sum of squares you can achieve?

    思路:x + y = x | y + x & y,ai = x | y, bi = x & y,可以看出总价值不变,只是把y可给x利用的价值加上,然后y减去被利用的价值,而二进制中利用的都是不同权值上的1,说明二进制上1的个数不变,只是在于转移给其他二进制串,这样我们可以统计所有权值的二进制位为1,然后构造出还能构造出的最大的二进制即可(x^2  + y^2 <= (x + y) ^ 2 , x,y>=0).

     1 #include <iostream>
     2 #include <cstdio>
     3 #include <algorithm>
     4 #include <queue>
     5 #include <string>
     6 #include <vector>
     7 #include <cmath>
     8 #include <stack> 
     9  
    10 using namespace std;
    11  
    12 #define ll long long
    13 #define pb push_back
    14 #define fi first
    15 #define se second
    16  
    17 const int N = 2e5 + 10;
    18 int bit[N];
    19 int a[N];
    20  
    21 void solve()
    22 {
    23     int n;
    24     cin >> n;
    25     for(int i = 1; i <= n; ++i) cin >> a[i];
    26     for(int i = 1; i <= n; ++i){
    27         for(int b = 0; b <= 25; ++b){
    28             if(a[i] & (1 << b)) bit[b]++;
    29         }
    30     }
    31  
    32     ll ans = 0;
    33     for(int i = 1; i <= n; ++i){
    34         int num = 0;
    35         for(int b = 30; b >= 0; --b){
    36             if(bit[b]){
    37                 num += (1 << b);
    38                 bit[b]--;
    39             }
    40         }
    41         ans += (ll)num * num;
    42     }
    43     //cout << "ans = ";       
    44     cout << ans << endl;
    45 }
    46  
    47 int main()
    48 {
    49     ios::sync_with_stdio(false);
    50     cin.tie(0);
    51     cout.tie(0); 
    52     solve();
    53  
    54     return 0;
    55 }
    56  
    57 // 10101010101010101010
    58 // 01010101010101010101
    59 // 111
  • 相关阅读:
    Wireshark下载地址
    WireShark过滤语法
    Centos7 虚拟机安装增强功能
    更改切换热键
    kali安装后中文乱码
    C#中关于WebBrowser的一些细节设置
    方法间多参数传递
    绑定checkedComboBox
    反射方法调用例子
    gridView 删除一行后自动定位到指定行
  • 原文地址:https://www.cnblogs.com/SSummerZzz/p/13380816.html
Copyright © 2011-2022 走看看