POJ2785-4 Values whose Sum is 0
传送门:http://poj.org/problem?id=2785
Description
Input
Output
Sample Input
6
-45 22 42 -16
-41 -27 56 30
-36 53 -37 77
-36 30 -75 -46
26 -38 -10 62
-32 -54 -6 45
Sample Output
5
Hint
给你一个数字n,然后有n组每组4个数,求每一列取出一个数,使最终四个数的和为0,求有多少种组合方式
解题思路:先求出来前两列和后两列的和,然后二分就可以了,对于这道题来说结果没有去重
#include<vector>
#include<set>
#include<stdio.h>
#include<algorithm>
using namespace std; vector <int > a;
vector <int > b;
vector <int > c;
vector <int > d;
vector <int > sum1;
vector <int > sum2;
int main()
{
int n, i, j, a1, b1, c1, d1, sum;
while(scanf("%d", &n)!=EOF) {
sum = ;
for(i = ; i < n; i++)
{
scanf("%d%d%d%d", &a1, &b1, &c1, &d1);
a.push_back(a1);
b.push_back(b1);
c.push_back(c1);
d.push_back(d1);
}
for(i = ; i < n; i++) {
for(j = ; j < n; j++) {
sum1.push_back(a[i] + b[j]);
sum2.push_back(c[i] + d[j]);
}
} /* for(i = 0; i < sum1.size(); i++) {
for(j = 0; j < sum2.size(); j++) {
if(sum1[i]+sum2[j] == 0) {
sum++;
}
}
}*/
n = sum2.size();
sort(sum2.begin(),sum2.end()); for(i = ; i < n; i++) {
int l = , r = n-, mid;
while(l <= r) {
mid = (l + r) / ;
if(sum1[i] + sum2[mid] == ) {
sum++;
for(j = mid + ; j < n; j++) {
if(sum1[i] + sum2[j] != )
break;
else
sum++;
}
for(j = mid - ; j >= ; j--) {
if(sum1[i] + sum2[j] != )
break;
else
sum++;
}
break;
}
if(sum1[i] + sum2[mid] < )
l = mid + ;
else
r = mid - ;
}
}
printf("%d\n", sum);
}
return ;
}
POJ2785-4 Values whose Sum is 0的更多相关文章
- [poj2785]4 Values whose Sum is 0(hash或二分)
4 Values whose Sum is 0 Time Limit: 15000MS Memory Limit: 228000K Total Submissions: 19322 Accepted: ...
- POJ-2785 4 Values whose Sum is 0(折半枚举 sort + 二分)
题目链接:http://poj.org/problem?id=2785 题意是给你4个数列.要从每个数列中各取一个数,使得四个数的sum为0,求出这样的组合的情况个数. 其中一个数列有多个相同的数字时 ...
- POJ2785 4 Values whose Sum is 0 (二分)
题意:给你四组长度为\(n\)序列,从每个序列中选一个数出来,使得四个数字之和等于\(0\),问由多少种组成情况(仅于元素的所在位置有关). 题解:\(n\)最大可以取4000,直接暴力肯定是不行的, ...
- 折半枚举(双向搜索)poj27854 Values whose Sum is 0
4 Values whose Sum is 0 Time Limit: 15000MS Memory Limit: 228000K Total Submissions: 23757 Accep ...
- 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: ...
- 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 ...
随机推荐
- Wannafly挑战赛27-A/B
链接:https://www.nowcoder.com/acm/contest/215/A来源:牛客网 题目描述 “White shores, and beyond. A far green coun ...
- 实战dataguard主从切换
前言: 众所周知DataGuard一般的切换分成两种,一种是系统正常的情况下的切换这种方式为:switchover是无损切换,不会丢失数据:另外一种方式属于灾难情况下的切换,这种情况下一般主库已经启动 ...
- Consider defining a bean of type 'org.springframework.data.redis.connection.RedisConnectionFactory' in your configuration
Description: Parameter 0 of method redisTemplate in com.liaojie.cloud.auth.server.config.redis.Redis ...
- [CodeForces - 197A] A - Plate Game
A - Plate Game You've got a rectangular table with length a and width b and the infinite number of p ...
- ActiveMQ topic 普通订阅和持久订阅
直观的结果:当生产者向 topic 发送消息, 1. 若不存在持久订阅者和在线的普通订阅者,这个消息不会保存,当普通订阅者上线后,它是收不到消息的. 2. 若存在离线的持久订阅者,broker 会为该 ...
- windows mfc 程序,不同程序通信和互斥
1. 共享内存(项目中使用过) 我转备份文章:http://www.cnblogs.com/swing07/p/8087686.html CreateFileMapping 或 OpenFileMap ...
- zhuan 常用图像数据集:标注、检索
目录(?)[+] 1.搜狗实验室数据集: http://www.sogou.com/labs/dl/p.html 互联网图片库来自sogou图片搜索所索引的部分数据.其中收集了包括人物.动物. ...
- jquery自定义类的封装
如何用jquery自定义一个类?(demo参考) /*简单使用*/ (function($){ //el操纵对象,option属性值 $.love = function(el,option){ var ...
- html盒子水平和垂直居中
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...
- flask项目结构(一)mariadb
简介: 本文主要是根据自己所学,创建一个flask项目,使用sqlalchemy,alembic,mariadb,bootstrap,APScheduler,selenium,request…………技 ...