Binary enumeration

Binary enumeration

  Binary enumeration enumeration method is a good use, such as a collection of sometimes too large to all of the search, but only just need something or other and they can handle out, the search can be half and half.

Exercise

poj 2785 4 Values whose Sum is 0

#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
const int maxn=4005;
int a[maxn],b[maxn],c[maxn],d[maxn],sum1[maxn*maxn],sum2[maxn*maxn];
int main()
{
  int i,j,n,cnt1=0,cnt2=0,ans=0;
  scanf("%d",&n);
  for(i=1;i<=n;i++) scanf("%d%d%d%d",&a[i],&b[i],&c[i],&d[i]);
  
  for(i=1;i<=n;i++)
   for(j=1;j<=n;j++)
    sum1[cnt1++]=a[i]+b[j];
  for(i=1;i<=n;i++)
   for(j=1;j<=n;j++)
    sum2[cnt2++]=c[i]+d[j];    
  sort(sum2,sum2+cnt2);
  for(i=0;i<cnt1;i++)
    ans+=upper_bound(sum2,sum2+cnt2,-sum1[i])-sum2-(lower_bound(sum2,sum2+cnt2,-sum1[i])-sum2);
  printf("%d\n",ans);
  system("pause");
  return 0;
}

 

More than 2019 cattle off summer school camp (ninth field) Knapsack Cryptosystem

Guess you like

Origin www.cnblogs.com/VividBinGo/p/11407988.html