UVA1152_4 Values whose Sum is 0
中途相遇法,这题目总结后我感觉和第一篇博客很像,他们都取了中间,也许这就是二分的魅力吧
这题题意就是从ABCD四个集合中选四个元素使他们的和为0
题意很简单,但是实现起来很容易超时,不能一个一个枚举
然后就想到了枚举两个,那么新生成EF两个集合大小为n方,然后如何查找?
最容易想到的就是用map标记,但汝佳说了这会超时,我不信,试了一发,超时了!!!
除了用map还能用什么呢,联想到之前那个UVA对称的题目,我们可以sort一下,然后查找e的相反数时候,二分查找-e,记住,找到还不行,有可能存在多个-e.所以upper_bound-lower_bound 就可以了,然后就这样过了
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<string>
#include<queue>
#include<cstdlib>
#include<algorithm>
#include<stack>
#include<map>
#include<queue>
#include<vector>
using namespace std;
int t,n,a,b,c,d,kase,cnt;
int num[4][4040],sum1[16000040],sum2[16000040];
map<int,int> list;
int main(){
// freopen("in.txt","r",stdin);
// freopen("out.txt","w",stdout);
kase=0;
cin>>t;
while(t--){
memset(num,0,sizeof num);
memset(sum1,0,sizeof sum1);
memset(sum2,0,sizeof sum2);
cnt=0;
if(kase++) cout<<"\n";
cin>>n;
for(int i=0;i<n;i++)
scanf("%d%d%d%d",&num[0][i],&num[1][i],&num[2][i],&num[3][i]);
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
sum1[i*n+j]=num[0][i]+num[1][j];
sum1[i*n+j]=-sum1[i*n+j];
}
}
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
sum2[i*n+j]=num[2][i]+num[3][j]; }
}
sort(sum1,sum1+n*n);
sort(sum2,sum2+n*n);
int end=n*n,pos1=0,pos2=0;
for(int i=0;i<end;i++){
pos1 = lower_bound(sum2+pos1,sum2+end,sum1[i]) -sum2;
pos2 = upper_bound(sum2+pos1,sum2+end,sum1[i]) -sum2;
if(pos1==end) break;
cnt+=pos2-pos1;
pos1=max(pos1-1,0);
}
cout<<cnt<<endl;
}
return 0;
}
UVA1152_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 ...
随机推荐
- 工控PLC中,关于定时器TON,TOF,的一点新认知,或者说醒悟吧!
PLC 中的定时器,都是放在一个具体PRG任务单元中的,而PRG单元需要放在具体固定的周期循环任务中才能被执行,而这个周期循环任务的循环周期 T: 与定时器的定时时间T0: T与T0 的数量级 ...
- Java开发用H2数据库
#JPA Configuration:#spring.jpa.database=MySQLspring.datasource.url=jdbc:h2:mem:jpaspring.datasource. ...
- python基础--选择排序
1.首先用一张图来描述选择排序的具体过程 2.废话不多说,上代码 # 1.定义函数:选择排序 def choose_sort(list): list_len = len(list) for i in ...
- Codeforces 498A Crazy Town
C. Crazy Town time limit per test 1 second memory limit per test 256 megabytes input standard input ...
- fiddler抓取手机端的数据流量包
1.首先下载安装fiddler 2.然后打开fiddler,进入到tools-->options-->connections 3.然后进入到https 4.设置完成后,查找本机ip 然后打 ...
- css的9个常用选择器
1.类选择器(通过类名进行选择) <!DOCTYPE html> <html> <head> <title></title> </he ...
- 将word文档中的回车符进行删除
- 【串线篇】SpringMvc之强大的annotation-driven标签
只要请求不好使就召唤mvc:annotation-driven <mvc:default-servlet-handler/> <mvc:annotation-driven/> ...
- matlab 代码分析
在command window中输入 >> profile on>> profile clear>> profile viewer 就会出现如下窗口 在将头所指向的 ...
- java--ArrayList,LinkedList应用比较
import java.util.ArrayList; import java.util.LinkedList; import java.util.List; public class ListDem ...