hdoj-2141-Can you find it?

题目链接

 1 /*
 2     Name:HDU-2141-Can you find it?
 3     Copyright:
 4     Author:
 5     Date: 2018/4/12 17:15:46
 6     Description:
 7         暴力,复杂度n^3,不存在的
 8         将公式变换为A-B=X-C
 9         枚举A-B,复杂度n^2
10         二分查找复杂度O(log2n),A-B是否存在A-B==X-C
11 */
12 #include <iostream>
13 #include <algorithm>
14 using namespace std;
15 const int MAXN = 1e6 + 10;
16 int a[MAXN], b[MAXN], c[MAXN], ab[MAXN], x[MAXN];
17 int main()
18 {
19 //    freopen("in.txt", "r", stdin);
20
21     int l, n, m;
22     int cas = 0;
23     while (cin>>l>>n>>m) {
24         cout<<"Case "<<++cas<<":"<<endl;
25         for (int i=0; i<l; i++) {
26             cin>>a[i];
27         }
28         for (int i=0; i<n; i++) {
29             cin>>b[i];
30         }
31         int k = 0;
32         for (int i=0; i<l; i++) {
33             for(int j=0; j<n; j++) {
34                 ab[k++] = a[i] + b[j];
35             }
36         }
37         k--;
38         sort(ab, ab+k);
39         for (int i=0; i<m; i++) {
40             cin>>c[i];
41         }
42         int s;
43         cin>>s;
44         for (int i=0; i<s; ++i) {
45             cin>>x[i];
46         }
47         for (int i=0; i<s; i++) {
48             int flag = 0;
49             for (int j=0; j<m; ++j) {
50                 int tmp = x[i] - c[j];
51                 if (binary_search(ab, ab+k, tmp) == true) {
52                     flag = 1;
53                     break;
54                 }
55             }
56             if (flag) cout<<"YES"<<endl;
57             else cout<<"NO"<<endl;
58         }
59     }
60     return 0;
61 }

原文地址:https://www.cnblogs.com/evidd/p/8809884.html

时间: 2024-10-22 02:52:42

hdoj-2141-Can you find it?的相关文章

二分查找 HDOJ 2141 Can you find it?

题目传送门 1 /* 2 题意:给出一个数,问是否有ai + bj + ck == x 3 二分查找:首先计算sum[l] = a[i] + b[j],对于q,枚举ck,查找是否有sum + ck == x 4 */ 5 #include <cstdio> 6 #include <algorithm> 7 #include <cmath> 8 using namespace std; 9 10 typedef long long ll; 11 const int MAX

hdoj 2141 Can you find it?【二分查找+暴力】

Can you find it? Time Limit: 10000/3000 MS (Java/Others)    Memory Limit: 32768/10000 K (Java/Others)Total Submission(s): 17036    Accepted Submission(s): 4337 Problem Description Give you three sequences of numbers A, B, C, then we give you a number

【HDOJ】4328 Cut the cake

将原问题转化为求完全由1组成的最大子矩阵.挺经典的通过dp将n^3转化为n^2. 1 /* 4328 */ 2 #include <iostream> 3 #include <sstream> 4 #include <string> 5 #include <map> 6 #include <queue> 7 #include <set> 8 #include <stack> 9 #include <vector>

POJ Xiangqi 4001 &amp;&amp; HDOJ 4121 Xiangqi

题目链接(POJ):http://poj.org/problem?id=4001 题目链接(HDOJ):http://acm.hdu.edu.cn/showproblem.php?pid=4121 Xiangqi Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 1108   Accepted: 299 Description Xiangqi is one of the most popular two-player boa

【HDOJ】4956 Poor Hanamichi

基本数学题一道,看错位数,当成大数减做了,而且还把方向看反了.所求为最接近l的值. 1 #include <cstdio> 2 3 int f(__int64 x) { 4 int i, sum; 5 6 i = sum = 0; 7 while (x) { 8 if (i & 1) 9 sum -= x%10; 10 else 11 sum += x%10; 12 ++i; 13 x/=10; 14 } 15 return sum; 16 } 17 18 int main() { 1

HDOJ 4901 The Romantic Hero

DP....扫两遍组合起来 The Romantic Hero Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) Total Submission(s): 547    Accepted Submission(s): 217 Problem Description There is an old country and the king fell in love with a

【HDOJ】1099 Lottery

题意超难懂,实则一道概率论的题目.求P(n).P(n) = n*(1+1/2+1/3+1/4+...+1/n).结果如果可以除尽则表示为整数,否则表示为假分数. 1 #include <cstdio> 2 #include <cstring> 3 4 #define MAXN 25 5 6 __int64 buf[MAXN]; 7 8 __int64 gcd(__int64 a, __int64 b) { 9 if (b == 0) return a; 10 else return

【HDOJ】2844 Coins

完全背包. 1 #include <stdio.h> 2 #include <string.h> 3 4 int a[105], c[105]; 5 int n, m; 6 int dp[100005]; 7 8 int mymax(int a, int b) { 9 return a>b ? a:b; 10 } 11 12 void CompletePack(int c) { 13 int i; 14 15 for (i=c; i<=m; ++i) 16 dp[i]

HDOJ 3790 双权值Dijkstra

1 #include <iostream> 2 #include <stdio.h> 3 #include <string.h> 4 #include <cstring> 5 using namespace std; 6 7 const int INF = 1000000; 8 const int MAXSIZE = 1005; 9 10 int map[MAXSIZE][MAXSIZE]; 11 int price[MAXSIZE][MAXSIZE]; 1

HDOJ 1217 Floyed Template

1 #include <iostream> 2 #include <stdio.h> 3 #include <string.h> 4 #include <cstring> 5 #include<map> 6 using namespace std; 7 8 map<string,int>name; 9 const int INF = 1000000; 10 const int MAXSIZE = 1005; 11 const int