题意:输入一个数字n,代表有n行a,b,c,d,求a+b+c+d=0有多少组情况。

思路:先求出前两个数字的所有情况,装在一个数组里面,再去求后两个数字的时候二分查找第一个大于等于这个数的位置和第一个大于这个数的位置相减,得出有多少个答案,累加得出最终答案.

过程: 刚开始写的时候,用的是map把原来的答案存起来,后来发现超时,因为在存数的时候和取数的时候都消耗时间,所以就用容器直接存,二分查找得出答案,结果二分函数又忘了,于是自己尝试着写了一个,就过了。

lower_bound(v.begin(),v.edn(),x)-v.begin;

upper_bound(v.begin(),v.end(),x)-v.begin;

#include<stdio.h>
#include<string.h>
#include<queue>
#include<vector>
#include<algorithm>
#include<map>
using namespace std;
int a[4005],b[4005],c[4005],d[4005],v[16000025];
bool comp(int a,int b)
{
return a<b;
}
int yao(int v[],int len,int t)
{
int l=0,r=len,mid;
while(l<r)
{
mid=(l+r)/2;
if(t<v[mid])
r=mid;
else
l=mid+1;
}
return r;
}
int main()
{
int n;
scanf("%d",&n);
int w=0;
for(int i=1; i<=n; i++)
scanf("%d%d%d%d",&a[i],&b[i],&c[i],&d[i]);
for(int i=1; i<=n; i++)
for(int j=1; j<=n; j++)
v[w++]=a[i]+b[j];
sort(v,v+w,comp);
int k,kk,t1,t;
long long ans=0;
for(int i=1; i<=n; i++)
for(int j=1; j<=n; j++)
{
t=c[i]+d[j];
t=t*-1;
k=lower_bound(v,v+w,t)-v;
kk=yao(v,w,t);
ans+=(kk-k);
}
printf("%lld\n",ans);
return 0;
}

4 Values whose Sum is 0 POJ - 2785(二分应用)的更多相关文章

  1. 4 Values whose Sum is 0 POJ - 2785

    4 Values whose Sum is 0 Time Limit: 15000MS   Memory Limit: 228000K Total Submissions: 29243   Accep ...

  2. 4 Values whose Sum is 0 POJ 2785 (折半枚举)

    题目链接 Description The SUM problem can be formulated as follows: given four lists A, B, C, D of intege ...

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

    找四个数的和为0 题目大意:给定四个集合,要你每个集合选4个数字,组成和为0 这题是3977的简单版,只要和是0就可以了 #include <iostream> #include < ...

  4. [poj2785]4 Values whose Sum is 0(hash或二分)

    4 Values whose Sum is 0 Time Limit: 15000MS Memory Limit: 228000K Total Submissions: 19322 Accepted: ...

  5. 4 Values whose Sum is 0(枚举+二分)

    The SUM problem can be formulated as follows: given four lists A, B, C, D of integer values, compute ...

  6. POJ 2785 4 Values whose Sum is 0(想法题)

    传送门 4 Values whose Sum is 0 Time Limit: 15000MS   Memory Limit: 228000K Total Submissions: 20334   A ...

  7. POJ 2785 4 Values whose Sum is 0

    4 Values whose Sum is 0 Time Limit: 15000MS   Memory Limit: 228000K Total Submissions: 13069   Accep ...

  8. POJ - 2785 4 Values whose Sum is 0 二分

    4 Values whose Sum is 0 Time Limit: 15000MS   Memory Limit: 228000K Total Submissions: 25615   Accep ...

  9. POJ 2785 4 Values whose Sum is 0(折半枚举+二分)

    4 Values whose Sum is 0 Time Limit: 15000MS   Memory Limit: 228000K Total Submissions: 25675   Accep ...

随机推荐

  1. python爬虫之Appium手机APP爬虫

    一.Appium工作原理(详情见:https://www.cnblogs.com/sophia194910/p/7515165.html) Appium的功能其实很简单:监听一个端口,然后接收由cli ...

  2. python自己做计算器

    题目: exp = '1 - 2 * ( (60-30 +(-40/5) * (9-2*5/3 + 7 /3*99/4*2998 +10 * 568/14 )) - (-4*3)/ (16-3*2) ...

  3. ASP.NET CORE 内置的IOC解读及使用

    在我接触IOC和DI 概念的时候是在2016年有幸倒腾Java的时候第一次接触,当时对这两个概念很是模糊:后来由于各种原因又回到.net 大本营,又再次接触了IOC和DI,也算终于搞清楚了IOC和DI ...

  4. MVC03

    1.添加model model 的作用是什么? 处理项目的数据模型,与数据库交互 .net推荐的处理数据的方式:使用 idd framework 1)新建model 右键models文件夹,选择添加, ...

  5. 前端、HTML+CSS+JS编写规范(终极版)

    HTMLCSS文档规范 HTML和CSS文档必须采用UTF-8编码格式: HTML文档必须使用HTML5的标准文档格式: HTMLCSS编写规范 HTML和CSS的标签.属性.类名.ID都必须使用小写 ...

  6. vue.js 中使用(...)运算符报错

    今天在起别人项目的时候, 发现报错. 这个错误是,项目中不识别es6的扩展运算符, 解决方式很简单. // 第一步 cnpm install babel-plugin-transform-object ...

  7. elasticsearch-head 安装

    一.安装phantomjs(由于入坑多写一步,此步骤可省掉) 1.下载phantomjs 安装npm的时候会依赖phantomjs 所以我们先安装phantomjs phantomjs 下载地址:ht ...

  8. GPS同步时钟装置应用及选择

    GPS同步时钟装置应用及选择 GPS是全球定位系统的简称,GPS具有全天时.全天候.高精度.定位和授时服务,GPS卫星授时成本低.安全可靠.覆盖范围广.GPS同步时钟装置,是指从GPS卫星上获取时间信 ...

  9. MVC项目中存储公用链接方法

    小白成长记第一篇,随手记录一下小常识 将固定地址写在web.config文件appSettings中 Controller调用方法:

  10. js获得用户网络状况API

    js获得用户网络状况API 这是一个实验中的功能,目前还有许多浏览器不兼容此功能某些浏览器尚在开发中 1. 网络类型 effectiveType: 可以得到2g,3g,4g connectionInf ...