<span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);">今天学长讲完二分法打了一场队内赛,</span>
6道题目截止到刚才AC了5道,
还有一道真的不会 = =
讲一下总结
其实rank可以更高
B题换了个double就过了
之前无限WA。。。
然后,A题二分写cuo了= =
然后强烈的感觉就是自己真的是弱渣,
可以随便被虐的那种 = =
时间复杂度这个东西知道有一年多了
然而真的有直观的感受还是今天= =
于是我就拿A题出来说一下子。
Description
Give you three sequences of numbers A, B, C, then we give you a number X. Now you need to calculate if you can find the three numbers Ai, Bj, Ck, which satisfy the formula Ai+Bj+Ck = X.
Input
There are many cases. Every data case is described as followed: In the first line there are three integers L, N, M, in the second line there are L integers represent the sequence A, in the third line there are N integers represent
the sequences B, in the forth line there are M integers represent the sequence C. In the fifth line there is an integer S represents there are S integers X to be calculated. 1<=L, N, M<=500, 1<=S<=1000. all the integers are 32-integers.
Output
For each case, firstly you have to print the case number as the form "Case d:", then for the S queries, you calculate if the formula can be satisfied or not. If satisfied, you print "YES", otherwise print "NO".
Sample Input
3 3 3 1 2 3 1 2 3 1 2 3 3 1 4 10
Sample Output
Case 1: NO YES NO
三个序列开的大小分别是 500 500 500 和序列开的是1000
相乘的话 就是125*10^9
1s大概是 10^8的样子
暴力肯定TLE
于是想到一种方法
A B所有的相加情况变成一个序列E
然后和序列D减去另一个序列C
再在E序列中去查找
这样的话
时间复杂度就变成了
log(500*500)*500*1000
直接降了好几个数量级
嗯 然后二分查找的模板还是要记清楚的
比如是low>=high
最后贴代码
#include <stdio.h>
#include <algorithm>
using namespace std;
#define maxn 1001
int a[maxn],b[maxn],c[maxn],x[maxn],d[maxn*maxn];
int icq(int a[],int n,int key)
{
int low=0;
int high=n-1;
int mid;
while(low<=high)
{
mid=(low+high)/2;
if(a[mid]<key)
{
low=mid+1;
}
else if(a[mid]>key)
{
high=mid-1;
}
else if(a[mid]==key)
return 1;
}
return -1;
}
int main()
{
int d0,m,n,s,i,j,k,l,flag;
d0=0;
while(scanf("%d %d %d",&l,&m,&n)!=EOF)
{
d0++;
for(i=0; i<l; i++)
scanf("%d",&a[i]);
for(i=0; i<m; i++)
scanf("%d",&b[i]);
for(i=0; i<n; i++)
scanf("%d",&c[i]);
scanf("%d",&s);
for(i=0; i<s; i++)
scanf("%d",&x[i]);
printf("Case %d:\n",d0);
k=0;
for(i=0; i<l; i++)
for(j=0; j<m; j++)
{
d[k]=a[i]+b[j];
++k;
}
sort(d,d+k);
for(j=0; j<s; j++)
{
flag=0;
i=0;
for(i=0; i<n; i++)
{
l=x[j]-c[i];
if(icq(d,k,l)==1)
{
printf("YES\n");
flag=1;
break;
}
}
if(flag==0)printf("NO\n");
}
}
return 0;
}