zoukankan      html  css  js  c++  java
  • 二分查找的应用

     1 #include <iostream>
     2 #include <algorithm>
     3 #include <cstdio>
     4 using namespace std;
     5 
     6 int a1[4001],a2[4001];
     7 int b1[4001],b2[4001];
     8 int sum1[4001*4000],sum2[4001*4000];
     9 
    10 int  binarySearch(int left,int right,int key)
    11 {
    12     __int64 ans=0; //64位整数
    13     while(left<=right)
    14     {
    15         int mid = (left + right) / 2;
    16         if(sum2[mid] == key)
    17         {
    18             ans++;
    19             for(int i=mid-1; i>=left; i--)   //搜到后可能相邻的左边有相等的也满足条件的数!
    20             {
    21                 if(sum2[i]!=key)
    22                 {
    23                     break;
    24                 }
    25                 ans++;
    26             }
    27             for(int j=mid+1; j<=right; j++)
    28             {
    29                 if(sum2[j]!=key)
    30                 {
    31                     break;
    32                 }
    33                 ans++;
    34             }
    35             return ans;
    36         }
    37         else if(sum2[mid] < key) left = mid+1;
    38         else right = mid -1;
    39     }
    40     return 0;
    41 }
    42 
    43 int main()
    44 {
    45     int n;
    46     cin>>n;
    47     __int64 ans=0;
    48     for(int i = 0; i < n; i++)
    49     {
    50         cin>>a1[i]>>a2[i]>>b1[i]>>b2[i];
    51 
    52     }
    53     int times = 0;
    54     for(int i = 0; i < n; i++) //四数相加转化为两数,通过两两随机相加,以便二分
    55         for(int j = 0; j < n; j++)
    56         {
    57             sum1[times] = a1[i] + a2[j];
    58             sum2[times] = b1[i] + b2[j];
    59             times++; //加时的下表
    60         }
    61     sort(sum2,sum2 + times);
    62 
    63     for(int i = 0; i < times; i++)
    64     {
    65         int key = -sum1[i];
    66         ans += binarySearch(0,times-1,key);
    67     }
    68     cout<<ans<<endl;
    69     return 0;
    70 }
    View Code

    二分 基础

    Time Limit:15000MS     Memory Limit:228000KB     64bit IO Format:%I64d & %I64u

    Description

    The SUM problem can be formulated as follows: given four lists A, B, C, D of integer values, compute how many quadruplet (a, b, c, d ) ∈ A x B x C x D are such that a + b + c + d = 0 . In the following, we assume that all lists have the same size n .

    Input

    The first line of the input file contains the size of the lists n (this value can be as large as 4000). We then have n lines containing four integer values (with absolute value as large as 2 28 ) that belong respectively to A, B, C and D .

    Output

    For each input file, your program has to write the number quadruplets whose sum is zero.

    Sample Input

    6
    -45 22 42 -16
    -41 -27 56 30
    -36 53 -37 77
    -36 30 -75 -46
    26 -38 -10 62
    -32 -54 -6 45
    

    Sample Output

    5
    

    Hint

    Sample Explanation: Indeed, the sum of the five following quadruplets is zero: (-45, -27, 42, 30), (26, 30, -10, -46), (-32, 22, 56, -46),(-32, 30, -75, 77), (-32, -54, 56, 30).
  • 相关阅读:
    利用.net Core 对程序集中的类 进行统一依赖注入
    接口中定义异步的方法
    在efcore 中创建类 通过实现IEntityTypeConfiguration<T>接口 实现实体类的伙伴类 实现FluentApi
    在vs2017 版本15.7.6中不支持2.1.0以上版本的net core sdk
    在.net core不同的版本中 webabi引用的包不同
    SQL语句中使用Group by
    DDD实战12 值对象不创建表,而是直接作为实体中的字段
    src与href的区别。
    cookies,sessionStorage 和 localStorage 的区别
    javascript阻止事件冒泡和浏览器的默认行为
  • 原文地址:https://www.cnblogs.com/cjshuang/p/4646207.html
Copyright © 2011-2022 走看看