SP1296 SUMFOUR - 4 values whose sum is 0
解题思路
四个数组一起做有点炸。先把他们合并成两个数组,然后让一个数组有序,枚举另一个数组的元素,二分即可。时间复杂度\(O(n^2logn^2)\)
代码
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
const int MAXN = 4005;
inline int rd(){
int x=0,f=1;char ch=getchar();
while(!isdigit(ch)) {f=ch=='-'?0:1;ch=getchar();}
while(isdigit(ch)) {x=(x<<1)+(x<<3)+ch-'0';ch=getchar();}
return f?x:-x;
}
int A[MAXN*MAXN],B[MAXN*MAXN],n,a[MAXN],b[MAXN],c[MAXN],d[MAXN];
int num[MAXN*MAXN],cnt,cnt2,cpy[MAXN*MAXN];
long long ans;
int main(){
n=rd();int x,y,w,z;
for(int i=1;i<=n;i++)
a[i]=rd(),b[i]=rd(),c[i]=rd(),d[i]=rd();
for(register int i=1;i<=n;i++)
for(register int j=1;j<=n;j++) A[++cnt]=a[i]+b[j],B[cnt]=c[i]+d[j];
sort(A+1,A+1+cnt);A[0]=(1<<28)+1;
for(register int i=1;i<=cnt;i++){
if(A[i]==A[i-1]) num[cnt2]++;
else cpy[++cnt2]=A[i],num[cnt2]=1;
}
// for(int i=1;i<=cnt2;i++) cout<<cpy[i]<<" ";
// A[++cnt2]=1;A[++cnt2]=2;A[++cnt2]=8;B[++cnt]=1;B[++cnt]=-2;B[++cnt]=-8;
for(register int i=1;i<=cnt;i++) {
x=lower_bound(cpy+1,cpy+1+cnt2,-B[i])-cpy;
// cout<<x<<endl;
if(cpy[x]==-B[i]) ans+=num[x];
}
printf("%lld\n",ans);
return 0;
}
SP1296 SUMFOUR - 4 values whose sum is 0的更多相关文章
- 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 ...
- 哈希-4 Values whose Sum is 0 分类: POJ 哈希 2015-08-07 09:51 3人阅读 评论(0) 收藏
4 Values whose Sum is 0 Time Limit: 15000MS Memory Limit: 228000K Total Submissions: 17875 Accepted: ...
- [poj2785]4 Values whose Sum is 0(hash或二分)
4 Values whose Sum is 0 Time Limit: 15000MS Memory Limit: 228000K Total Submissions: 19322 Accepted: ...
- K - 4 Values whose Sum is 0(中途相遇法)
K - 4 Values whose Sum is 0 Crawling in process... Crawling failed Time Limit:9000MS Memory Limi ...
- UVA 1152 4 Values whose Sum is 0 (枚举+中途相遇法)(+Java版)(Java手撕快排+二分)
4 Values whose Sum is 0 题目链接:https://cn.vjudge.net/problem/UVA-1152 ——每天在线,欢迎留言谈论. 题目大意: 给定4个n(1< ...
- UVA1152-4 Values whose Sum is 0(分块)
Problem UVA1152-4 Values whose Sum is 0 Accept: 794 Submit: 10087Time Limit: 9000 mSec Problem Desc ...
- 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 ...
随机推荐
- Pregel Aggregator
- Dockfile中的命令如何在.sh中执行
有类似如下内容的Dokefile文件.1 RUN cd /tmp/patch \ && /lib/python3./site-packages/moduleA/a.* \ && ...
- Superset安装出错 sqlalchemy.exc.InvalidRequestError: Can't determine which FROM clause to join from, ...
$ superset db upgrade ... Traceback (most recent call last): File "/home/jhadmin/.pyenv/version ...
- 【优化】碎片OPTIMIZE
来看看手册中关于 OPTIMIZE 的描述: OPTIMIZE [LOCAL | NO_WRITE_TO_BINLOG] TABLE tbl_name [, tbl_name] ... 如果您已经删除 ...
- NX二次开发-UFUN工程图表格注释获取某一行的tag函数UF_TABNOT_ask_nth_row
NX9+VS2012 #include <uf.h> #include <uf_tabnot.h> #include <NXOpen/Part.hxx> #incl ...
- P2004 领地选择
P2004 领地选择 题目描述 作为在虚拟世界里统帅千军万马的领袖,小Z认为天时.地利.人和三者是缺一不可的,所以,谨慎地选择首都的位置对于小T来说是非常重要的. 首都被认为是一个占地C*C的正方形. ...
- Linux服务器上监控网络带宽的18个常用命令nload, iftop,iptraf-ng, nethogs, vnstat. nagios,运用Ntop监控网络流量
Linux服务器上监控网络带宽的18个常用命令 本文介绍了一些可以用来监控网络使用情况的Linux命令行工具.这些工具可以监控通过网络接口传输的数据,并测量目前哪些数据所传输的速度.入站流量和出站流量 ...
- 2019 牛客多校第一场 E ABBA
题目链接:https://ac.nowcoder.com/acm/contest/881/E 题目大意 问有多少个由 (n + m) 个 ‘A’ 和 (n + m) 个 ‘B’,组成的字符串能被分割成 ...
- hbase 集群搭建(公司内部测试集群)
我用的是cdh4.5版本:配置文件:$HBASE_HOME/conf/hbase-env.shexport JAVA_HOME=$JAVA_HOMEexport JAVA_HOME=/home/had ...
- 关于strtok函数
函数原型: char *strtok(char * strToken, const char *strDelimit) 参数说明: strToken:源字符串,即待分割的串 strDelimit:st ...