首页 > 其他分享 >HDU 4334 Trouble

HDU 4334 Trouble

时间:2024-08-01 16:38:50浏览次数:10  
标签:HDU int s2 4334 maxn Trouble s1

题目链接:HDU 4334【Trouble】



思路

       哈希+贪心,直接将五个数组分成两个或者三个数组,此时数组相加的时间复杂度为O(n2)或者O(n3),然后双重循环数组e和s1并遍历找出s2中是否有满足题意的元素,这个步骤可以使用二分代替还能降低时间复杂度。


代码

#include <iostream>
#include <vector>
#include <algorithm>
#define maxn 2550
using namespace std;
typedef long long ll;
ll a[maxn], b[maxn], c[maxn], d[maxn], e[maxn];

vector<ll> s1, s2, s3;
int main() {
  int t;
  cin >> t;
  while (t--) {
    int n;
    s1.clear(), s2.clear();
    scanf("%d", &n);
    for (int i = 1; i <= n; i++)
      scanf("%lld", &a[i]);
    for (int i = 1; i <= n; i++)
      scanf("%lld", &b[i]);
    for (int i = 1; i <= n; i++)
      scanf("%lld", &c[i]);
    for (int i = 1; i <= n; i++)
      scanf("%lld", &d[i]);
    for (int i = 1; i <= n; i++)
      scanf("%lld", &e[i]);

    for (int i = 1; i <= n; i++) {
      for (int j = 1; j <= n; j++) {
        s1.push_back(a[i] + b[j]);
        s2.push_back(c[i] + d[j]);
      }
    }
    sort(s1.begin(), s1.end());

    sort(s2.begin(), s2.end());

    bool vis = true;
    for (int i = 1; i <= n; i++) {
      int p = s2.size() - 1;
      for (int j = 0; j < s1.size() && p >= 0; j++) {
        while (e[i] + s1[j] + s2[p] > 0)
          p--;
        if (e[i] + s1[j] + s2[p] == 0) {
          vis = false;
          cout << "Yes" << endl;
          break;
        }
      }
      if (!vis)
        break;
    }
    if (vis)
      puts("No");
  }
  return 0;
}

标签:HDU,int,s2,4334,maxn,Trouble,s1
From: https://www.cnblogs.com/againss/p/18336970

相关文章

  • HDU2024 R4
    T4先把四组分成两组,左边两组叫\(L\),右边两组叫\(R\)。直接爆搜每个数属于\(L\)还是\(R\),过程中顺带\(01\)背包出两组分别能取到哪些子集异或和。接下来不妨设\(w_{f(A)}\gew_{f(B)}\),\(w_{f(C)}\gew_{f(D)}\)。若\(w_{f(A)}\gew_{f(C)}\),则答案为\(w_{f(A)}-w_......
  • HDU2024 R2 T9 题解
    考虑维护一下每个点的速度。把区间加拆成后缀加和后缀减,然后考虑后缀加。减就同理。考虑在一段后缀的目标速度增加之后,哪些时刻的加速度会变化。这里加速度必然只会变大\(1\),因此在这个时刻之后的速度都会增加\(1\),又由于目标速度也增加了\(1\),所以这个位置之后的加速度都不再......
  • HDU 1020 Encoding
    题目链接:HDU1020【Encoding】思路    简单模拟,计算相同字母的连续子串个数。代码#include<iostream>#include<algorithm>#include<queue>usingnamespacestd;#definelllonglongconstintN=500+10;voidsolve(){strings;cin>>s;int......
  • HDU多校 2024R1
    T1把\(A\)的所有循环位移哈希一下扔set里,拿\(B\)的所有长为\(|A|\)的子串查一遍即可。代码#include<iostream>#include<set>usingnamespacestd;set<unsignedlonglong>st;constintB=2333;unsignedlonglongpw[2000005];intmain(){inttc;......
  • HDU1000,HDU1001,HDU1002,HDU1003,HDU1004
    目录HDU1000——A+BProblem题目描述运行代码代码思路HDU1001——SumProblem题目描述运行代码代码思路HDU1002——A+BProblemII(高精度加法)题目描述运行代码代码思路高精度加法模板HDU1003——MaxSum题目描述运行代码代码思路HDU1004——LettheBall......
  • hdu2601
    /*题意:给n求满足i*j+i+j=n(0<i<=j)方案数思路:xy+x+y=n(x+1)(y+1)=x*y+x+y+1=n+1;即求n+1的因子对数参考:https://blog.csdn.net/Puppettt/article/details/83030925?ops_request_misc=%257B%2522request%255Fid%2522%253A%25221720964219168001821369......
  • HDU 1213 How Many Tables
    题目链接:HDU1213HowManyTables思路    经典并查集,将互相认识的人全部放在一个集合内,然后计算有几个集合就有几个桌子。代码#include<iostream>usingnamespacestd;#definelllonglongconstintN=1e3+10;intfa[N];voidinit(intn){for(i......
  • HDU 2570 迷瘴
    题目链接:HDU2570【迷障】思路    简单贪心,需要算出尽可能大的体积,所以先将浓度数组按从小到大的顺序排列,然后从小到大依次取出药水配置,直到浓度大于w,回溯到前一个状态并输出代码#include<bits/stdc++.h>#include<exception>usingnamespacestd;#definelllo......
  • HDU 2037 今年暑假不AC
    题目链接:HDU2037【今年暑假不AC】’思路    典型区间贪心,按节目结束时间升序排序,结束时间相等时按开始时间升序排序,然后逐个查找满足要求的节目,下一个观看的节目开始时间要大于当前观看节目的结束时间。代码#include<bits/stdc++.h>usingnamespacestd;#define......
  • HDU 1240 Asteroids!
    题目链接:HDU1240【Asteroids!】思路    代码#include<iostream>#include<queue>#include<stdlib.h>#include<cstring>#definelllonglongusingnamespacestd;constintN=20;constintM=1e4;structpoint{intx,y,z,st......