HDU--4324--Trouble

//很考想法啊,把五个区间分成200*200,200*200,200来搞。注意:过的时候要用__int64为来过(输出:%I64d)

AC代码:

#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
#define N 205
__int64 num[6][N];
__int64 s0[N*N];
__int64 s1[N*N];
__int64 s2[N];
int main()
{
    int x;
    scanf("%d",&x);
    while(x--)
    {
        int n;
        scanf("%d",&n);
        int i,j;
        for(i=1;i<=5;i++)
        {
            for(j=1;j<=n;j++)
            {
                scanf("%I64d",&num[i][j]);
            }
        }
        int cnt=0;
        for(i=1;i<=n;i++)
        {
            for(j=1;j<=n;j++)
            {
                s0[cnt++]=num[1][i]+num[2][j];
            }
        }
        int l0=cnt;
        cnt=0;
        for(i=1;i<=n;i++)
        {
            for(j=1;j<=n;j++)
            {
                s1[cnt++]=num[3][i]+num[4][j];
            }
        }
        int l1=cnt;
        cnt=0;
        for(i=1;i<=n;i++)
        {
            s2[cnt++]=num[5][i];
        }
        int l2=cnt;
        sort(s0,s0+l0);
        sort(s1,s1+l1);
        sort(s2,s2+l2);
        int ok=0;
        int k;
        for(i=0;i<l2&&ok==0;i++)
        {
                j=0,k=l0-1;
                while(j<l1&&k>=0)
                {
                    if((s0[k]+s1[j]+s2[i])==0)
                    {
                        ok=1;
                        break;
                    }
                    else if((s0[k]+s1[j]+s2[i])>0)
                    {
                        k--;
                    }
                    else
                    {
                        j++;
                    }
                }
        }
        if(ok)
        {
            printf("Yes\n");
        }
        else
        {
            printf("No\n");
        }
    }
    return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2025-01-29 02:13:28

HDU--4324--Trouble的相关文章

hdu 4324

Triangle LOVE Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 2478    Accepted Submission(s): 1011 Problem Description Recently, scientists find that there is love between any of two people. For

HDU 4324 Triangle LOVE (拓扑排序)

Triangle LOVE Problem Description Recently, scientists find that there is love between any of two people. For example, between A and B, if A don't love B, then B must love A, vice versa. And there is no possibility that two people love each other, wh

HDU 4324:Triangle LOVE( 拓扑排序 )

Triangle LOVE Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 2271    Accepted Submission(s): 946 Problem Description Recently, scientists find that there is love between any of two people. For

hdu 4324 Triangle LOVE(拓扑排序)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4324 Triangle LOVE Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 3858    Accepted Submission(s): 1516 Problem Description Recently, scientists f

HDU 4324 Triangle LOVE(拓扑排序判环)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4324 题目: Problem Description Recently, scientists find that there is love between any of two people. For example, between A and B, if A don’t love B, then B must love A, vice versa. And there is no possi

HDU 4334 Trouble(hash + 枚举)

HDU 4334 题意: 给五个数的集合,问能否从每个集合中取一个数,使五个数之和为0. 思路: 集合大小是200,直接枚举的复杂度是200^5,一定会超时. 直接枚举的上限是3层,我们可以将枚举剩下两个集合各任取一个元素可能组成的元素和,并将其作hash处理,使我们能很快判断枚举出来的三个集合元素和在剩下的两个集合里是否有相应元素匹配. code: /* * @author Novicer * language : C++/C */ #include<iostream> #include&l

HDU 4334——Trouble——————【贪心&amp;水题】

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 pro

HDU 4334 Trouble

合并排序再枚举. 题意问五个数组中各抽一个加起来 和是否为 0. 开始想用 200*200*200 和 200*200 比.果然TLE了. 后来知道 得 200*200,200*200 ,200 . 先200*200 的枚举,排序,去重.然后三个元素加起来,微调 两个 200*200 的指针. 这题用 %lld 就WA.得用 %I64d .贡献N多TLE.N多WA.怒刷存在感. #include<cstdio> #include<cstring> #include<strin

hdu 4324 Triangle LOVE

本题链接:点击打开链接 本题大意: 题意分析(转载):此题可以一遍拓扑排序判环求解 即只需要找到一个环,就必定存在三元环 证明如下: 假设存在一个n元环,因为a->b有边,b->a必定没边,反之也成立所以假设有环上三个相邻的点a-> b-> c,那么如果c->a间有边,就已经形成了一个三元环,如果c->a没边,那么a->c肯定有边,这样就形成了一个n-1元环....所以只需证明n大于3时一定有三元环即可,显然成立. 具体请参见代码: #include<std

HDU 4324 (拓扑排序) Triangle LOVE

因为题目说了,两个人之间总有一个人喜欢另一个人,而且不会有两个人互相喜欢.所以只要所给的图中有一个环,那么一定存在一个三元环. 所以用拓扑排序判断一下图中是否有环就行了. 1 #include <cstdio> 2 #include <cstring> 3 4 const int maxn = 2000 + 10; 5 char G[maxn][maxn]; 6 int c[maxn]; 7 int n; 8 9 bool dfs(int u) 10 { 11 c[u] = -1;