UVa 1152 (中途相遇法) 4 Values whose Sum is 0

题意:

要从四个数组中各选一个数,使得这四个数之和为0,求合法的方案数。

分析:

首先枚举A+B所有可能的值,排序。

然后枚举所有-C-D的值在其中用二分法查找。

 1 #include <cstdio>
 2 #include <algorithm>
 3 using namespace std;
 4
 5 const int maxn = 4000 + 10;
 6 int A[maxn], B[maxn], C[maxn], D[maxn], sum[maxn*maxn], cnt;
 7
 8 int main()
 9 {
10     //freopen("in.txt", "r", stdin);
11
12     int T;
13     scanf("%d", &T);
14     while(T--)
15     {
16         int n;
17         scanf("%d", &n);
18         for(int i = 0; i < n; ++i) scanf("%d%d%d%d", &A[i], &B[i], &C[i], &D[i]);
19         cnt = 0;
20         for(int i = 0; i < n; ++i)
21             for(int j = 0; j < n; ++j)
22                 sum[cnt++] = A[i] + B[j];
23         sort(sum, sum + cnt);
24         long long ans = 0;
25         for(int i = 0; i < n; ++i)
26             for(int j = 0; j < n; ++j)
27                 ans += upper_bound(sum, sum + cnt, -C[i]-D[j]) - lower_bound(sum, sum + cnt, -C[i]-D[j]);
28         printf("%lld\n", ans);
29         if(T) puts("");
30     }
31
32     return 0;
33 }

代码君

时间: 2024-10-05 11:57:29

UVa 1152 (中途相遇法) 4 Values whose Sum is 0的相关文章

uva 6757 Cup of Cowards(中途相遇法,貌似)

uva 6757 Cup of CowardsCup of Cowards (CoC) is a role playing game that has 5 di?erent characters (Mage, Tank, Fighter,Assassin and Marksman). A team consists of 5 players (one from each kind) and the goal is to kill amonster with L life points. The

uva1152 - 4 Values whose Sum is 0(枚举,中途相遇法)

用中途相遇法的思想来解题.分别枚举两边,和直接暴力枚举四个数组比可以降低时间复杂度.可是我不会写...看了紫书作者刘汝佳老师的代码,真是太美了!简单明了,就像看吕钦下的棋一样.我就模仿的写了一下: #include<iostream> #include<cstdio> #include<cstdlib> #include<cstring> #include<cmath> #include<map> #include<set>

K - 4 Values whose Sum is 0(中途相遇法)

K - 4 Values whose Sum is 0 Crawling in process... Crawling failed Time Limit:9000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Submit Status Practice UVA 1152 Appoint description: System Crawler (2015-03-12) Description The SUM problem can

UVA 1326 Jurassic Remains 中途相遇法

题目链接:点击打开链接 题意:给定n个字符串,选尽可能多的字符串使得每种字母出现的次数为偶数次 思路: 中途相遇法 import java.io.PrintWriter; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.Comparator; import java.util.Iterator; import java.util.LinkedLi

J 中途相遇法,求和

---恢复内容开始--- J - 中途相遇法 Time Limit:9000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Submit Status Description The SUM problem can be formulated as follows: given four lists A, B, C, D<tex2html_verbatim_mark> of integer values, compute

POJ 1840 Eqs Hash + 中途相遇法

把等式分成两拨算用中途相遇法就好了. 不过要注意的是这里不能用map,会超时,要自己手写hash,我重载了[]操作符之后用起来和map差不多,很随意 #include <cstdio> #include <cstring> #include <iostream> #include <map> #include <set> #include <vector> #include <string> #include <qu

Codeforces Round #297 (Div. 2) E题. Anya and Cubes (中途相遇法)

题目地址:Anya and Cubes 比赛的时候居然没想起中途相遇法...这题也是属于想起来就很简单系列. 中途相遇法也叫折半搜索.就是处理前一半,把结果储存起来,再处理后一半,然后匹配前一半存储的结果. 代码如下: #include <iostream> #include <string.h> #include <math.h> #include <queue> #include <algorithm> #include <stdlib

HDU 5936 Difference 【中途相遇法】(2016年中国大学生程序设计竞赛(杭州))

Difference Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 62    Accepted Submission(s): 19 Problem Description Little Ruins is playing a number game, first he chooses two positive integers y an

【中途相遇法】【STL】BAPC2014 K Key to Knowledge

题目链接: http://codeforces.com/gym/100526 http://acm.hunnu.edu.cn/online/?action=problem&type=show&id=11674&courseid=0 题目大意: N个学生M道题(1<=N<=12,1<=M<=30),每道题只有正误两种选项(0 1),每个学生的答题情况和正确题数已知,求标准答案可能有多少种. 如果标准答案只有一种则输出标准答案,否则输出解的个数. 题目思路: [