---恢复内容开始---

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 ABCD<tex2html_verbatim_mark> of integer values, compute how many quadruplet (abcd ) AxBxCxD<tex2html_verbatim_mark> are such that a + b + c + d = 0<tex2html_verbatim_mark> . In the following, we assume that all lists have the same size n<tex2html_verbatim_mark> .

Input

The input begins with a single positive integer on a line by itself indicating the number of the cases following, each of them as described below. This line is followed by a blank line, and there is also a blank line between two consecutive inputs.

The first line of the input file contains the size of the lists n<tex2html_verbatim_mark> (this value can be as large as 4000). We then have n<tex2html_verbatim_mark> lines containing four integer values (with absolute value as large as 228<tex2html_verbatim_mark> ) that belong respectively to ABC<tex2html_verbatim_mark> and D<tex2html_verbatim_mark> .

Output

For each test case, the output must follow the description below. The outputs of two consecutive cases will be separated by a blank line.

For each input file, your program has to write the number quadruplets whose sum is zero.

Sample Input

  1. 1
  2.  
  3. 6
  4. -45 22 42 -16
  5. -41 -27 56 30
  6. -36 53 -37 77
  7. -36 30 -75 -46
  8. 26 -38 -10 62
  9. -32 -54 -6 45

Sample Output

  1. 5

Sample Explanation: Indeed, the sum of the five following quadruplets is zero: (-45, -27, 42, 30), (26, 30, -10, -46), (-32, 22, 56, -46),(-32, 30, -75, 77), (-32, -54, 56, 30).

题目思路:

大意:每列找一个数,得到和为0的序列,有几种不同的方案
分析:
如果进行四重循环,毫无疑问会超时,这时需要用到枚举案例,
对1,2列的数求一个和,3,4列的数求一个和,然后进行二分查找
程序代码:
  1. #include<cstdio>
  2. #include<algorithm>
  3. using namespace std;
  4. int a[];
  5. int b[];
  6. int p[][];
  7. int t;
  8. int erfen(int x)
  9. {
  10. int cnt=;
  11. int l=,r=t-,mid;
  12. while(r>l)
  13. {
  14. mid=(l+r)>>;
  15. if(b[mid]>=x) r=mid;
  16. else l=mid+;
  17. }
  18.  
  19. while(b[l]==x&&l<t)
  20. {
  21. cnt++;
  22. l++;
  23. }
  24. return cnt;
  25. }
  26. int main()
  27. {
  28. int n,i,j,T;
  29. long long res;
  30. scanf("%d",&T);
  31. while(T--)
  32. {
  33.  
  34. scanf("%d",&n);
  35. res=;
  36. for(i=;i<n;i++)
  37. for(j=;j<;j++)
  38. scanf("%d",&p[i][j]);
  39. t=;
  40. for(i=;i<n;i++)
  41. for(j=;j<n;j++)
  42. a[t++]=p[i][]+p[j][];
  43. sort(a,a+t);
  44. t=;
  45. for(i=;i<n;i++)
  46. for(j=;j<n;j++)
  47. b[t++]=p[i][]+p[j][];
  48. sort(b,b+t);
  49. for(i=;i<t;i++)
  50. res+=erfen(-a[i]);
  51. printf("%d\n",res);
  52. if(T) printf("\n");
  53. }
  54. return ;
  55. }

---恢复内容结束---

高效算法——J 中途相遇法,求和的更多相关文章

  1. 【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) 解法:首先我们从最直接最暴力的方法开始思考:四重循 ...

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

    uva 6757 Cup of CowardsCup of Cowards (CoC) is a role playing game that has 5 different characters (M ...

  3. LA 2965 Jurassic Remains (中途相遇法)

    Jurassic Remains Paleontologists in Siberia have recently found a number of fragments of Jurassic pe ...

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

    Difference Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total ...

  5. 【中途相遇法】【STL】BAPC2014 K Key to Knowledge (Codeforces GYM 100526)

    题目链接: http://codeforces.com/gym/100526 http://acm.hunnu.edu.cn/online/?action=problem&type=show& ...

  6. 【UVALive】2965 Jurassic Remains(中途相遇法)

    题目 传送门:QWQ 分析 太喵了~~~~~ 还有中途相遇法这种东西的. 嗯 以后可以优化一些暴力 详情左转蓝书P58 (但可能我OI生涯中都遇不到正解是这个的题把...... 代码 #include ...

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

    用中途相遇法的思想来解题.分别枚举两边,和直接暴力枚举四个数组比可以降低时间复杂度. 这里用到一个很实用的技巧: 求长度为n的有序数组a中的数k的个数num? num=upper_bound(a,a+ ...

  8. LA 2965 中途相遇法

    题目链接:https://vjudge.net/problem/UVALive-2965 题意: 有很多字符串(24),选出一些字符串,要求这些字符串的字母都是偶数次: 分析: 暴力2^24也很大了, ...

  9. 中途相遇法 解决 超大背包问题 pack

    Description [题目描述] 蛤布斯有n个物品和一个大小为m的背包,每个物品有大小和价值,它希望你帮它求出背包里最多能放下多少价值的物品. [输入数据] 第一行两个整数n,m.接下来n行每行两 ...

随机推荐

  1. Asp.net主题(theme)和皮肤(skin)的使用

    asp.net 的服务器端控件提供了多种样式的设计,如果对每个控件都单独设置,是比较繁琐的事情,所以微软也提供了针对这些服务器端控件的样式管理,其实也可以通过 css来控制部分服务器端控件的样式,比如 ...

  2. CSS Clip剪切元素动画实例

    1.CSS .fixed { position: fixed; width: 90px; height: 90px; background: red; border: 0px solid blue; ...

  3. 在oracle中怎么把一张表的数据插入到另一张表中

    把table2表的数据插入到table1中 insert   into   table1   select   *   from   table2

  4. Shell case正则匹配法

    Shell case正则匹配法   case $BOOLEAN in [yY][eE][sS]) echo 'Thanks' $BOOLEAN ;; [yY]|[nN]) echo 'Thanks' ...

  5. 项目中常用SQL语句总结

    1.项目中常常需要修改字段长度,但需要保留数据--增加业务受理 项目名称 字段长度alter table t_ywsl add aa varchar2(200);update t_ywsl set a ...

  6. C#winform程序自定义鼠标样式

    public void SetCursor(Bitmap cursor, Point hotPoint) { int hotX = hotPoint.X; int hotY = hotPoint.Y; ...

  7. js实现滑动解锁功能(PC+Moblie)

    http://dummyimage.com/600x400/ http://placehold.it/140x70 实现效果: css样式代码略. html代码: 页面上导入了jquery.mobil ...

  8. xamp配置多域名站点

    xampp配置多站点出现,htdocs目录和虚拟目录二者只能选其一的情况,我的xampp安装在D:\xampp\,默认web根目录在D:\xampp\htdocs,然后我在D:\magento安装了m ...

  9. [Winfrom] 捕获窗体最大化、最小化和关闭按钮的事件

    const int WM_SYSCOMMAND = 0x112;const int SC_CLOSE = 0xF060;const int SC_MINIMIZE = 0xF020;const int ...

  10. MySql数据库3【优化2】sql语句的优化

    1.SELECT语句优化 1).利用LIMIT 1取得唯一行[控制结果集的行数] 有时,当你要查询一张表是,你知道自己只需要看一行.你可能会去的一条十分独特的记录,或者只是刚好检查了任何存在的记录数, ...