4 Values whose Sum is 0 POJ - 2785(二分应用)
题意:输入一个数字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(二分应用)的更多相关文章
- 4 Values whose Sum is 0 POJ - 2785
4 Values whose Sum is 0 Time Limit: 15000MS Memory Limit: 228000K Total Submissions: 29243 Accep ...
- 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 ...
- Divide and conquer:4 Values whose Sum is 0(POJ 2785)
找四个数的和为0 题目大意:给定四个集合,要你每个集合选4个数字,组成和为0 这题是3977的简单版,只要和是0就可以了 #include <iostream> #include < ...
- [poj2785]4 Values whose Sum is 0(hash或二分)
4 Values whose Sum is 0 Time Limit: 15000MS Memory Limit: 228000K Total Submissions: 19322 Accepted: ...
- 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 ...
- POJ 2785 4 Values whose Sum is 0(想法题)
传送门 4 Values whose Sum is 0 Time Limit: 15000MS Memory Limit: 228000K Total Submissions: 20334 A ...
- POJ 2785 4 Values whose Sum is 0
4 Values whose Sum is 0 Time Limit: 15000MS Memory Limit: 228000K Total Submissions: 13069 Accep ...
- POJ - 2785 4 Values whose Sum is 0 二分
4 Values whose Sum is 0 Time Limit: 15000MS Memory Limit: 228000K Total Submissions: 25615 Accep ...
- POJ 2785 4 Values whose Sum is 0(折半枚举+二分)
4 Values whose Sum is 0 Time Limit: 15000MS Memory Limit: 228000K Total Submissions: 25675 Accep ...
随机推荐
- C++走向远洋——(项目二、存储班长信息的学生类、派生)
*/ * Copyright (c) 2016,烟台大学计算机与控制工程学院 * All rights reserved. * 文件名:text.cpp * 作者:常轩 * 微信公众号:Worldhe ...
- ts文件的编译和运行
hello.ts代码 function sayHello(person: string): string { return 'Hello, ' + person; } let user = 'Tom' ...
- pycharm在windows中如何安装dlib?
pycharm在windows下安装dlib库的时候,出现以下错误(等等类似的问题): 解决办法: 1.这个方法无关紧要,只是记录一下:将清华源替换为阿里源: C:\Users\Administrat ...
- 关于Spring注解@Component、@Repository、@Service、@Controller @Resource、@Autowired、@Qualifier 解析
1.Spring 2.5 中除了提供 @Component 注释外,还定义了几个拥有特殊语义的注释,它们分别是:@Repository.@Service和 @Controller 其实这三个跟@Com ...
- iview2+ 表单密码验证
<Form :model="formItem" v-model="formItem.id" ref="formItem" :label ...
- nsq 初学使用日记
win下更加直观一些,所以不使用liunx 第一步下载 nsq 下载地址 https://github.com/nsqio/nsq.git 使用git clone或者go get 下载下来 第二部 编 ...
- Redis 服务端程序实现原理
上篇我们简单介绍了 redis 客户端的一些基本概念,包括其 client 数据结构中对应的相关字段的含义,本篇我们结合这些,来分析分析 redis 服务端程序是如何运行的.一条命令请求的完成,客户端 ...
- 组件/ 外层数据初始化时候,不应该触发 on-change 事件
组件/ 外层数据初始化时候,不应该触发 on-change 事件 watch: { value (value) { this.noOnChange = true // 外层传值 不触发on-chang ...
- Python习题集(二)
每天一习题,提升Python不是问题!!有更简洁的写法请评论告知我! https://www.cnblogs.com/poloyy/category/1676599.html 题目 a = [1, 2 ...
- OO课程的完结,软件工程学习的开始
目录 UML小结 阅读学习 大象:Thinking in UML UML精粹 UML和模式应用 本单元作业的架构设计 四个单元中架构设计及OO方法的演进 四个单元中测试与实践的演进 课程收获 三个具体 ...