• Divide and conquer:4 Values whose Sum is 0(POJ 2785)


                     

                    找四个数的和为0

      题目大意:给定四个集合,要你每个集合选4个数字,组成和为0

      这题是3977的简单版,只要和是0就可以了

      

     1 #include <iostream>
     2 #include <algorithm>
     3 #include <functional>
     4 #define MAX 4001
     5 
     6 using namespace std;
     7 
     8 typedef long long LL_INT;
     9 static LL_INT list[4][MAX], set_sum1[MAX*MAX];
    10 
    11 LL_INT *Binary_Lower_Bound(int &, LL_INT);
    12 LL_INT *Binary_Upper_Bound(int &, LL_INT);
    13 LL_INT solve(int &,int &);
    14 
    15 int main(void)
    16 {
    17     int list_contian, sum_comb;
    18     LL_INT ans;
    19 
    20     while (~scanf("%d", &list_contian))
    21     {
    22         for (int i = 0; i < list_contian; i++)
    23             scanf("%lld%lld%lld%lld", &list[0][i], &list[1][i], &list[2][i], &list[3][i]);
    24 
    25         sum_comb = 0;
    26         for (int i = 0; i < list_contian; i++)//把第一张和第二张表的总数枚举出来
    27             for (int j = 0; j < list_contian; j++)
    28                 set_sum1[sum_comb++] = list[0][i] + list[1][j];
    29 
    30         sort(set_sum1, set_sum1 + sum_comb);
    31         ans = solve(list_contian, sum_comb);
    32         printf("%lld
    ", ans);
    33     }
    34     return EXIT_SUCCESS;
    35 }
    36 
    37 LL_INT solve(int &list_contain, int &sum_comb)
    38 {
    39     LL_INT tmp_sum, ans = 0;
    40     int pos1, pos2;
    41 
    42     for (int i = 0; i < list_contain; i++)
    43     {
    44         for (int j = 0; j < list_contain; j++)
    45         {
    46             tmp_sum = -list[2][i] - list[3][j];
    47             pos1 = Binary_Upper_Bound(sum_comb, tmp_sum) - set_sum1;
    48             pos2 = Binary_Lower_Bound(sum_comb, tmp_sum) - set_sum1;
    49             ans += pos1 - pos2;
    50         }
    51     }
    52     return ans;
    53 }
    54 
    55 LL_INT *Binary_Lower_Bound(int &sum_comb, LL_INT val)
    56 {
    57     int lb = 0, mid, count1 = sum_comb, count2;
    58     while (count1 > 0)
    59     {
    60         count2 = count1 >> 1;
    61         mid = lb + (count1 >> 1);
    62         if (set_sum1[mid] < val)
    63         {
    64             lb = ++mid;
    65             count1 -= count2 + 1;
    66         }
    67         else count1 = count2;
    68     }
    69     return &set_sum1[lb];
    70 }
    71 
    72 LL_INT *Binary_Upper_Bound(int &sum_comb, LL_INT val)
    73 {
    74     int lb = 0, mid, count1 = sum_comb, count2;
    75     while (count1 > 0)
    76     {
    77         count2 = count1 >> 1;
    78         mid = lb + (count1 >> 1);
    79         if (set_sum1[mid] <= val)
    80         {
    81             lb = ++mid;
    82             count1 -= count2 + 1;
    83         }
    84         else count1 = count2;
    85     }
    86     return &set_sum1[lb];
    87 }

      

  • 相关阅读:
    ggplot2|theme主题设置,详解绘图优化-“精雕细琢”-
    ggplot2|theme主题设置,详解绘图优化-“精雕细琢”
    阻抗设计01
    Geber文件,装配图,BOM表的输出
    c语言里面你不知道的break与switch,contiune的用法
    数据结构之链表学习01
    数据结构概念及连续存储数组的算法演示
    使用malloc和free函数进行内存动态分配
    浅谈结构体
    浅谈指针01
  • 原文地址:https://www.cnblogs.com/Philip-Tell-Truth/p/5175152.html
Copyright © 2020-2023  润新知