二分基础

<span style="color:#3333ff;">/*A - 二分 基础
Time Limit:15000MS     Memory Limit:228000KB     64bit IO Format:%I64d & %I64u
Submit

Status
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).
By Grant Yuan
2014.7.14
二分
*/
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<algorithm>
using namespace std;
long long a[4002],b[4002],c[4002],d[4002];
int n;
long long num1[16000004];
long long num2[16000004];
int top;
long long sum;
int binary(long k,int left,int right)
{
     int i;
    while(left<=right){
      int mid=(left+right)/2;
      int num=0;
           if(num2[mid]==k)
       {
         num=1;
         for(i=mid-1;i>=0&&num2[i]==k;i--)  num++;
         for(i=mid+1;i<n*n&&num2[i]==k;i++)  num++;
         return num;
      }
     else if(num2[mid]>k)
       right=mid-1;
      else left=mid+1;
   }
   return 0;

}

int main()
{
    cin>>n;
    long long flag;
    for(int i=0;i<n;i++)
       cin>>a[i]>>b[i]>>c[i]>>d[i];
    top=-1;
    sum=0;
    for(int i=0;i<n;i++)
      for(int j=0;j<n;j++)
     {
         num1[++top]=a[i]+b[j];
         num2[top]=c[i]+d[j];
     }
     sort(num1,num1+top+1);
     sort(num2,num2+top+1);
     for(int i=0;i<=top;i++)
       {
           flag=-num1[i];
           sum+=binary(flag,0,top+1);
       }
       cout<<sum<<endl;
       return 0;
}
</span>

二分基础,布布扣,bubuko.com

时间: 2024-11-15 01:19:59

二分基础的相关文章

POJ 2456 Aggressive cows (二分 基础)

Aggressive cows Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7924   Accepted: 3959 Description Farmer John has built a new long barn, with N (2 <= N <= 100,000) stalls. The stalls are located along a straight line at positions x1,...

二分基础(模板题)

二分强化——全面查询 TimeLimit:2000MS  MemoryLimit:128MB 64-bit integer IO format:%lld 已解决 | 点击收藏 | 已有5人收藏了本题 Problem Description 给出一个从递增的正整数序列a0,a1……an-1对不同的查询输出对应结果 Input 只有一组数据第一行是一个整数n,代表序列的长度(下标从0开始一直到n-1)接下来n个整数,代表序列对应位置上的数再接下来一行是一个整数m代表查询次数接下来m行接下来的每行是这

Cable master HDU - 1551 —— 二分基础模板(精度问题)

Inhabitants of the Wonderland have decided to hold a regional programming contest. The Judging Committee has volunteered and has promised to organize the most honest contest ever. It was decided to connect computers for the contestants using a "star&

二分基础题

2146 分割绳子http://www.51nod.com/Challenge/Problem.html#problemId=2146 #include <bits/stdc++.h> using namespace std; double a[1005]; int n,k; int check(double x){ int num = 0; for(int i = 0; i < n; i++) num += (int)(a[i] / x); return num >= k; }

二分查找的应用

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

B二分

<span style="color:#330099;">/* B - 二分 基础 Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Submit Status Description Farmer John has built a new long barn, with N (2 <= N <= 100,000) stalls. The stalls are loc

HDU-2298 Toxophily (三分法入门系列)

题意: 意大利炮射出炮弹的速度为v,求在(0,0)击中(x,y)处的目标,发射炮弹的角度. 题解: 设f(α)表示:角度为α,炮弹的横坐标与目标相同时,炮弹的高度. f(α) = vsin(α) * t - 4.9 * t * t   ① t = x / ( v * cos(α) )               ② 然后,一顿乱搞得f(α) = x*tan(α) -  (4.9 * x * x / v / v) *  (tan(α) + 1) 妥妥的单峰函数,使用三分得出f(α)取max时的角度

Find Minimum in Rotated Sorted Array 旋转数组中找最小值 @LeetCode

O(n)的算法就不说了,这题主要考查的是 O(logn)的算法. 有序数组容易想到使用二分查找解决,这题就是在二分基础上做一些调整.数组只有一次翻转,可以知道原有序递增数组被分成两部分,这俩部分都是有序递增的(这题只需要考虑有序数组的递增情况). 假如翻转后的数组以第 x 个结点分为两部分 A[0..x] 和 A[x+1..n].则 A[0..x] 这一段是有序递增的, A[x+1..n] 这一段也是有序递增的.并且因为原数组是有序递增的,A[0..x] 中所有数都会大于 A[x+1..n] 中

大话数据结构—顺序表、有序表、线性索引查找

查找 根据给定的某个值,在查找表中确定一个其关键字(唯一的标识一个记录)等于给定值的数据元素或数据记录. 静态查找:只查找,不修改元素[线性表.顺序查找.二分查找] 动态查找:查找时,插入或者删除元素[二叉排序树] 顺序表查找 顺序查找(针对静态查找表),也叫线性查找O(n),从头开始遍历,直到最后一个记录. 优化:添加哨兵 //有哨兵的顺序查找 int foo(int *a,int n,int key) { int i; a[0]=key;//哨兵 i=n; while(a[i]!=key)