zoukankan      html  css  js  c++  java
  • ZOJ 3870 Team Formation

    题目链接:

    http://www.icpc.moe/onlinejudge/showProblem.do?problemCode=3870

    题解:

    如果x xor y>max(x,y),那么就会有x和y的最高位不同(二进制表示),并且对于最高位小的那个数的最高位的位置,最高位大的那个数在相同的位置应为0.

    代码:

     1 #include<iostream>
     2 #include<cstdio>
     3 #include<cstring>
     4 using namespace std;
     5 
     6 typedef long long LL;
     7 const int maxn = 1e5 + 10;
     8 
     9 int n;
    10 
    11 int arr[maxn];
    12 //cnt[i]表示最高位为i的数有多少个
    13 int cnt[32];
    14 
    15 void init() {
    16     memset(cnt, 0, sizeof(cnt));
    17 }
    18 
    19 int main() {
    20     int tc;
    21     scanf("%d", &tc);
    22     while (tc--) {
    23         init();
    24         scanf("%d", &n);
    25         for (int i = 0; i<n; i++) {
    26             scanf("%d", &arr[i]);
    27             for (int j = 30; j >= 0; j--) {
    28                 if ((1 << j)&arr[i]) {
    29                     cnt[j]++; break;
    30                 }
    31             }
    32         }
    33         LL ans = 0;
    34         for (int i = 0; i<n; i++) {
    35             int j;
    36             for (j = 30; j >= 0; j--) {
    37                 if ((1 << j)&arr[i]) {
    38                     break;
    39                 }
    40             }
    41             for (j -= 1; j >= 0; j--) {
    42                 if (((1 << j)&arr[i]) == 0) {
    43                     ans += cnt[j];
    44                 }
    45             }
    46         }
    47         printf("%lld
    ", ans);
    48     }
    49     return 0;
    50 }
  • 相关阅读:
    idea自定义servlet模板
    jsp基础-指令,内置对象,动作,EL表达式,JSTL技术
    cookie和session
    HttpServletRequest
    IO字符流
    IO字节流
    递归
    File 类
    JDBC数据库连接
    Map接口
  • 原文地址:https://www.cnblogs.com/fenice/p/5472246.html
Copyright © 2011-2022 走看看