Trouble

Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 5388    Accepted Submission(s): 1494

Problem Description
Hassan is in trouble. His mathematics teacher has given him a very difficult problem called 5-sum. Please help him.
The 5-sum problem is defined as follows: Given 5 sets S_1,...,S_5 of n integer numbers each, is there a_1 in S_1,...,a_5 in S_5 such that a_1+...+a_5=0?
 
Input
First line of input contains a single integer N (1≤N≤50). N test-cases follow. First line of each test-case contains a single integer n (1<=n<=200). 5 lines follow each containing n integer numbers in range [-10^15, 1 0^15]. I-th line denotes set S_i for 1<=i<=5.
 
Output
For each test-case output "Yes" (without quotes) if there are a_1 in S_1,...,a_5 in S_5 such that a_1+...+a_5=0, otherwise output "No".
 
Sample Input
2
2
1 -1
1 -1
1 -1
1 -1
1 -1
3
1 2 3
-1 -2 -3
4 5 6
-1 3 2
-4 -10 -1
 
Sample Output
No
Yes
 
题目大意:给你5行每行n个数,问你是否可以在每行中取出一个数,让a1+a2+a3+a4+a5=0。

解题思路:开始写了搜索,但是一直超时。然而超时好像是必然的,没办法剪枝。其实可以将前1、2行合成一行a,将3、4行合成一行b,从小到大排序。枚举剩下的最后一行c,枚举a,逆序枚举b。贪心思想在如果b的当前值跟a和c的和小于0,那么可以直接让a的指针加一,如果大于0,可以让b的指针减一,如果逆序遍历完b或正序遍历完a以后还没找到,那么就继续枚举c。

#include<bits/stdc++.h>
using namespace std;
typedef long long INT;
INT Map[6][210];
INT a[42000];
INT b[42000];
INT c[210];
int main(){
int t,n;
scanf("%d",&t);
while(t--){
scanf("%d",&n);
for(int i=1;i<=5;i++){
for(int j=0;j<n;j++){
scanf("%lld",&Map[i][j]);
}
}
int na=0,nb=0,nc=0;
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
a[na++]=Map[1][i]+Map[2][j];//合并1、2行
b[nb++]=Map[3][i]+Map[4][j];//合并3、4行
}
}
int nn=n*n;
sort(a,a+nn); //递增排序
sort(b,b+nn); //递增排序
na=1,nb=1,nc=1;
for(int i=1;i<nn;i++){ //去重
if(a[i]!=a[i-1]){
a[na++]=a[i];
}
}
for(int i=1;i<nn;i++){ //去重
if(b[i]!=b[i-1]){
b[nb++]=b[i];
}
}
c[0]=Map[5][0];
for(int i=1;i<n;i++){ //去重
if(Map[5][i]!=Map[5][i-1]){
c[nc++]=Map[5][i];
}
}
int flag=0;
for(int i=0;i<nc&&(!flag);i++){ //n的复杂度
for(int j=0,k=nb-1;k>=0&&j<na; ){//n*n的复杂度
INT cc=c[i]+a[j]+b[k];
if(cc<0){ //降低复杂度。模拟跳出一层循环
j++;
}else if(cc==0){
flag=1;
break;
}else{
k--;
}
}
}
if(flag){
puts("Yes");
}else {
puts("No");
}
}
return 0;
}

  

HDU 4334——Trouble——————【贪心&水题】的更多相关文章

  1. HDU—2021-发工资咯(水题,有点贪心的思想)

    作为杭电的老师,最盼望的日子就是每月的8号了,因为这一天是发工资的日子,养家糊口就靠它了,呵呵  但是对于学校财务处的工作人员来说,这一天则是很忙碌的一天,财务处的小胡老师最近就在考虑一个问题:如果每 ...

  2. HDU 4950 Monster (水题)

    Monster 题目链接: http://acm.hust.edu.cn/vjudge/contest/123554#problem/I Description Teacher Mai has a k ...

  3. LightOJ 1166 Old Sorting 置换群 或 贪心 水题

    LINK 题意:给出1~n数字的排列,求变为递增有序的最小交换次数 思路:水题.数据给的很小怎么搞都可以.由于坐标和数字都是1~n,所以我使用置换群求循环节个数和长度的方法. /** @Date : ...

  4. DP+贪心水题合集_C++

    本文含有原创题,涉及版权利益问题,严禁转载,违者追究法律责任 本次是最后一篇免费的考试题解,以后的考试题目以及题解将会以付费的方式阅读,题目质量可以拿本次作为参考 本来半个月前就已经搞得差不多了,然后 ...

  5. hdu 1106:排序(水题,字符串处理 + 排序)

    排序 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submissi ...

  6. HDU 4813 Hard Code 水题

    Hard Code Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.hust.edu.cn/vjudge/contest/view.act ...

  7. HDU 4593 H - Robot 水题

    H - RobotTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hust.edu.cn/vjudge/contest/view.act ...

  8. HDOJ/HDU 2560 Buildings(嗯~水题)

    Problem Description We divide the HZNU Campus into N*M grids. As you can see from the picture below, ...

  9. HDOJ(HDU) 1859 最小长方形(水题、、)

    Problem Description 给定一系列2维平面点的坐标(x, y),其中x和y均为整数,要求用一个最小的长方形框将所有点框在内.长方形框的边分别平行于x和y坐标轴,点落在边上也算是被框在内 ...

  10. HDU - 1716 排列2 水题

    排列2 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submis ...

随机推荐

  1. C#检测系统是否激活[转自StackOverFlow]

    using System; using System.Collections.Generic; using System.Text; using System.Runtime.InteropServi ...

  2. bat 操作数据库(附加,分离,删除,还原)

    BAT代码: @echo off Title DataBase Color 0A :caozuo echo. echo ═══════════════════════════════════════ ...

  3. Weekly Contest 114

    955. Delete Columns to Make Sorted II We are given an array A of N lowercase letter strings, all of ...

  4. FullCalendar插件的基本使用

    我的另一博客地址:https://segmentfault.com/u/lyrfighting/articles 前段时间,一直在开发考勤系统,当时为满足设计的需求,选了好几个插件,最后决定采用Ful ...

  5. jdk 1.6.0_39 下载

    Java SE Development Kit 6u39 Product / File Description File Size Download password Linux x86 65.42 ...

  6. Jenkins 相关

    手动下载Jenkins plugin 的地址, 下载后的是zip 文件,然后再手动修改为.hpi 文件,然后再手动上传 https://plugins.jenkins.io/

  7. HP EliteBook 8570p TouchPad Issue

    最近更换了笔记本,当时TouchPad总是不能用,有个指示灯总是亮着:同事同型号的就没有这样的问题. Google了好久终于找到了一篇帖子能解决我的问题.分享给朋友们. Link: http://h3 ...

  8. Django 09 博客小案例

    Django 09 博客小案例 urls.py from django.urls import path from . import views urlpatterns = [ path('index ...

  9. Luogu P5201 [USACO19JAN]Shortcut 最短路树???

    最短路树...开眼界了...之前想也没想过.... 先跑出来1到每个点最短路,然后建树时要标记点的入度,否则会多连边...然后深搜时更新新答案就是 #include<cstdio> #in ...

  10. 2018年 CCPC 网络赛 赛后总结

    历程:由于只是网络赛,所以今天就三开了.一开始的看题我看了d题,zz和jsw从头尾看起来,发现c题似乎可做,和费马大定理有关,于是和zz一起马上找如何计算勾股数的方法,比较慢的A掉了,而jsw此时看了 ...