hdu-2141 Can you find it?---暴力+二分
2024-10-18 07:16:22
题目链接:
http://acm.hdu.edu.cn/showproblem.php?pid=2141
题目大意:
给ABC三个数组,给一个X,求是否存在Ai+Bj+Ck = X
思路:
等式转化成Ai+Bj = X-Ck
这样预处理出Ai+Bj的所有数字,然后循环k,二分查找X-Ck是否存在。
首先用set超内存,然后用数组结果一直WA,二分的时候少了个”=“
这真是醉了。
果然深夜不适合刷题
#include<iostream>
#include<vector>
#include<queue>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<set>
using namespace std;
typedef pair<int, int> Pair;
typedef long long ll;
const int INF = 0x3f3f3f3f;
int T, n, m1, m2, m3, cases;
ll a[], b[], c[];
ll cnt[];
int tot;
bool Find(ll x)
{
int l = , r = tot-;
while(l <= r)
{
int mid = (l + r) / ;
if(cnt[mid] == x)return true;
else if(cnt[mid] < x)l = mid + ;
else r = mid - ;
}
return false;
}
int main()
{
while(scanf("%d%d%d", &m1, &m2, &m3) != EOF)
{
memset(cnt, , sizeof(cnt));
tot = ;
for(int i = ; i < m1; i++)cin >> a[i];
for(int i = ; i < m2; i++)cin >> b[i];
for(int i = ; i < m3; i++)cin >> c[i];
for(int i = ; i < m1; i++)
for(int j = ; j < m2; j++)cnt[tot++] = (a[i] + b[j]);
sort(cnt, cnt + tot);
sort(c, c + m3);
scanf("%d", &n);
printf("Case %d:\n", ++cases);
while(n--)
{
ll x;
cin >> x;
bool flag = ;
for(int i = ; i < m3; i++)
{
if(Find(x - c[i]))
{
flag = ;
break;
}
}
if(flag){printf("YES\n");continue;}
printf("NO\n");
}
}
}
最新文章
- [c++] constexpr and literal class
- mysql--sqlalchemy.exc.IntegrityError: (IntegrityError) (1215, &#39;Cannot add foreign key constraint&#39;
- nginx应用总结(1)--基础认识和应用配置
- ecshop换用redis做缓存
- 面向对象编程(七)——Static关键字
- sdut 2609 A-Number and B-Number
- 统计nginx日志里流量
- [置顶] 2014年八大最热门IT技能
- IO流(随机流,数组内存流
- C++ 中获取 可变形參函数中的參数
- JS函数参数
- OS模块的常用内置方法
- oracle权限列表
- 51 NOd 1459 迷宫游戏 (最短路径)
- zabbix链接规则
- [CF1082E] Increasing Frequency
- linux内核空间和用户空间详解
- Mac和iOS开发资源汇总—更新于2013-10-14
- Linux内核触摸屏驱动--多点触摸 【转】
- Python开发【模块】:Requests(一)