UVa 1152 (中途相遇法) 4 Values whose Sum is 0
题意:
要从四个数组中各选一个数,使得这四个数之和为0,求合法的方案数。
分析:
首先枚举A+B所有可能的值,排序。
然后枚举所有-C-D的值在其中用二分法查找。
#include <cstdio>
#include <algorithm>
using namespace std; const int maxn = + ;
int A[maxn], B[maxn], C[maxn], D[maxn], sum[maxn*maxn], cnt; int main()
{
//freopen("in.txt", "r", stdin); int T;
scanf("%d", &T);
while(T--)
{
int n;
scanf("%d", &n);
for(int i = ; i < n; ++i) scanf("%d%d%d%d", &A[i], &B[i], &C[i], &D[i]);
cnt = ;
for(int i = ; i < n; ++i)
for(int j = ; j < n; ++j)
sum[cnt++] = A[i] + B[j];
sort(sum, sum + cnt);
long long ans = ;
for(int i = ; i < n; ++i)
for(int j = ; j < n; ++j)
ans += upper_bound(sum, sum + cnt, -C[i]-D[j]) - lower_bound(sum, sum + cnt, -C[i]-D[j]);
printf("%lld\n", ans);
if(T) puts("");
} return ;
}
代码君
UVa 1152 (中途相遇法) 4 Values whose Sum is 0的更多相关文章
- 【uva 1152】4 Values Whose Sum is Zero(算法效率--中途相遇法+Hash或STL库)
题意:给定4个N元素几个A,B,C,D,要求分别从中选取一个元素a,b,c,d使得a+b+c+d=0.问有多少种选法.(N≤4000,D≤2^28) 解法:首先我们从最直接最暴力的方法开始思考:四重循 ...
- 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< ...
- 紫书 例题8-3 UVa 1152(中途相遇法)
这道题要逆向思维, 就是求出答案的一部分, 然后反过去去寻找答案存不存在. 其实很多其他题都用了这道题目的方法, 自己以前都没有发现, 这道题专门考这个方法.这个方法可以没有一直往下求, 可以省去很多 ...
- uva 6757 Cup of Cowards(中途相遇法,貌似)
uva 6757 Cup of CowardsCup of Cowards (CoC) is a role playing game that has 5 different characters (M ...
- K - 4 Values whose Sum is 0(中途相遇法)
K - 4 Values whose Sum is 0 Crawling in process... Crawling failed Time Limit:9000MS Memory Limi ...
- uva1152 - 4 Values whose Sum is 0(枚举,中途相遇法)
用中途相遇法的思想来解题.分别枚举两边,和直接暴力枚举四个数组比可以降低时间复杂度. 这里用到一个很实用的技巧: 求长度为n的有序数组a中的数k的个数num? num=upper_bound(a,a+ ...
- 紫书 习题 8-16 UVa 1618 (中途相遇法)
暴力n的四次方, 然而可以用中途相遇法的思想, 分左边两个数和右边两个数来判断, 最后合起来判断. 一边是n平方logn, 合起来是n平方logn(枚举n平方, 二分logn) (1)两种比较方式是相 ...
- 高效算法——J 中途相遇法,求和
---恢复内容开始--- J - 中途相遇法 Time Limit:9000MS Memory Limit:0KB 64bit IO Format:%lld & %llu Su ...
- LA 2965 Jurassic Remains (中途相遇法)
Jurassic Remains Paleontologists in Siberia have recently found a number of fragments of Jurassic pe ...
随机推荐
- asp.net项目中通过Web.config配置文件及文件夹的访问权限---forms
十分全面的forms验证配置: http://blog.csdn.net/qingyun1029/article/details/6184723
- ScheduledExecutorService的用法——定时执行两个任务
package control; import java.text.DateFormat; import java.text.ParseException; import java.text.Simp ...
- Have Fun with Numbers (大数)
Notice that the number 123456789 is a 9-digit number consisting exactly the numbers from 1 to 9, wit ...
- 在ubuntu下给eclipse创建桌面快捷方式
在桌面进行编辑 编辑eclipse.desktop [Desktop Entry] Encoding=UTF-8 Name=Eclipse Platform Comment=Eclipse IDE E ...
- 在fedora 桌面上添加应用程序
在网上下了个android studio,这个程序只是的压缩包,解压后程序也只能在SHELL下敲入studio.sh才能运行 能不能向其他程序一样,在fedora桌面上找到应用程序,点击执行呢.在网上 ...
- python 读写INI配置文件
# -*- coding: utf-8 -*-import ConfigParserimport os '''读写配置文件的类[section]logpath = D:\log\imageminsiz ...
- [转载]深入理解ASP.NET MVC之ActionResult
Action全局观 在上一篇最后,我们进行到了Action调用的“门口”: 1 if (!ActionInvoker.InvokeAction(ControllerContext, actionNam ...
- PAT-乙级-1032. 挖掘机技术哪家强(20)
1032. 挖掘机技术哪家强(20) 时间限制 200 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN, Yue 为了用事实说明挖掘机技术到底 ...
- The 9th Zhejiang Provincial Collegiate Programming Contest->Problem D:D - Draw Something Cheat
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3603 题意:在给出的字符串中找出每行都出现的字母按字典序排序. #incl ...
- 安卓 DevOps:从一次推送命令到生产
DevOps 是一种广为人知的活动,其主要目的是使软件交付自动化.的确,DevOps 的目标是持续测试.代码质量.功能开发和更轻松地进行维护更新.因此,DevOps 的终极目标之一是让开发者可以执行快 ...