题意:输入一个数字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. flask 返回json数据

    ret={ 'a':1 } return jsonify(ret) 不能直接返回ret,应该用jsonify().这样返回的Content-Type: application/json;charset ...

  2. 30分钟学会Objective-C

    注: 本文首发于我的个人博客:https://evilpan.com/2019/04/05/objc-basics/ 请原谅我的标题党.但是如果你有其他语言的学习经验,要学习Objective-C的语 ...

  3. 位运算基础(Uva 1590,Uva 509题解)

    逻辑运算 规则 符号 与 只有1 and 1 = 1,其他均为0 & 或 只有0 or 0 = 0,其他均为1 | 非 也就是取反 ~ 异或 相异为1相同为0 ^ 同或 相同为1相异为0,c中 ...

  4. 解决oninput事件在中文输入法下会取得拼音的值的问题

    在做搜索等功能时,很多时候我们需要实时获取用户输入的值,而常常会得到类似 w'm 这样的拼音.为了解决这个问题,我在网上搜索了下相关问题,发现了两个陌生的事件:compositionstart 和 c ...

  5. almost最好的Vue + Typescript系列02 项目结构篇

    基于vue-cli 3.x,配合typescript的环境构建的新vue项目,跟以前的结构相比,有了一些变化,下面我们来简单的了解一下 基本结构: node_modules: 项目中安装的依赖模块 p ...

  6. Python 【基础常识概念】

    深浅拷贝 浅copy与deepcopy 浅copy: 不管多么复杂的数据结构,浅拷贝都只会copy一层 deepcopy : 深拷贝会完全复制原变量相关的所有数据,在内存中生成一套完全一样的内容,我们 ...

  7. ZXingObjC二维码扫描

    #import "QRScanViewController.h" #import "AppDelegate.h" @interface QRScanViewCo ...

  8. Alibaba Sentinel 限流与熔断初探(技巧篇)

    目录 1.Sentinel 是什么 ?主要能解决什么问题? 2.限流与熔断的使用场景 3.Sentinel 源码结构 4.在 IntelliJ IDEA 中运行 Sentine Demo 温馨提示:源 ...

  9. Nginx之负载均衡配置(二)

    前文我们聊到了nginx作为负载均衡的配置,前端nginx作为调度器调度http或https请求,回顾请参考https://www.cnblogs.com/qiuhom-1874/p/12458159 ...

  10. python社区要放弃了pip?版本信息里带警告很不寻常哦

    pip是python的一个包管理器. 今天再查询Pip3 -V 时,除了正常的版本信息外,多了几行信息 WARNING: pip is being invoked by an old script w ...