Numbers
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/262144 K (Java/Others)Total Submission(s): 68 Accepted Submission(s): 58
Problem Description
There are n numbers
A1,A2....An
,your task is to check whether there exists there different positive integers i, j, k (
1≤i,j,k≤n
) such that
Ai−Aj=Ak
Input
There are multiple test cases, no more than 1000 cases.
First line of each case contains a single integer n. (3≤n≤100) .
Next line contains n integers A1,A2....An . (0≤Ai≤1000)
First line of each case contains a single integer n. (3≤n≤100) .
Next line contains n integers A1,A2....An . (0≤Ai≤1000)
Output
For each case output "YES" in a single line if you find such i, j, k, otherwise output "NO".
Sample Input
3 3 1 2 3 1 0 2 4 1 1 0 2
Sample Output
YES NO YES 大意:找到三个数满足Ai-Aj=Ak 思路:数据较小,可以直接DFS ac代码:#include<stdio.h> #include<string.h> #include<iostream> #include<algorithm> #define MAXN 3000001 #define MAX(a,b) a>b?a:b #define LL long long #define fab(a) ((a)>0?(a):-(a)) using namespace std; int num[MAXN]; int v[MAXN],a[MAXN]; int n,bz,k; void dfs(int k) { if(bz) return; if(k==4) { if((a[1]-a[2])==a[3]) { bz=1; return; } return; } for(int i=0;i<n;i++) { if(!v[i]) { a[k]=num[i]; v[i]=1; dfs(k+1); if(bz) return; v[i]=0; } } } int main() { while(scanf("%d",&n)!=EOF) { for(int i=0;i<n;i++) scanf("%d",&num[i]); memset(v,0,sizeof(v)); bz=0; dfs(1); if(bz) printf("YES\n"); else printf("NO\n"); } return 0; }