YY's new problem
Time Limit: 12000/4000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 5422 Accepted Submission(s): 1522
Problem Description
Given a permutation P of 1 to N, YY wants to know whether there exists such three elements P[i
1], P[i
2], P[i
3] that
P[i 1]-P[i 2]=P[i 2]-P[i 3], 1<=i 1<i 2<i 3<=N.
P[i 1]-P[i 2]=P[i 2]-P[i 3], 1<=i 1<i 2<i 3<=N.
Input
The first line is T(T<=60), representing the total test cases.
Each test case comes two lines, the former one is N, 3<=N<=10000, the latter is a permutation of 1 to N.
Each test case comes two lines, the former one is N, 3<=N<=10000, the latter is a permutation of 1 to N.
Output
For each test case, just output 'Y' if such i
1, i
2, i
3 can be found, else 'N'.
Sample Input
2 3 1 3 2 4 3 2 4 1
Sample Output
N Y
Source
Recommend
xubiao
题意:就是找出数列中是否有满足p[i1]-p[i2]=p[i2]-p[i3], 1<=i
1<i
2<i
3<=N.的存在
题解:先将公式转化一下,转化为(p[i1]+p[i3])=p[i2]/2。即找到两个数中间存在一个数为他们的一半。
用数组嵌套记录下数的位置和值,a数组代表输入的值,b数组代表输入的值的位置。二重i,j循环
遍历数组,如果a[i]和a[j]的和s是奇数那么肯定不存在一个数c的二倍等于他们的和,因为c的二倍
是偶数。所以continue。如果是偶数那么数c的值肯定是一定的,一定是(a[i]+a[j])/2,现在找数
c是不是在a[i]和a[j]中间,如果在就存在这种情况了。这个时候就要用到b数组了,因为b数组是记
录元素下标的。用b数组判断是不是在中间就行了,如果找到了就用flag标记一下break输出就行了。
#include <iostream> #include <stdio.h> using namespace std; int main() { int i,j,n,t,a[10005],b[10005],flag,s; scanf("%d",&t); while(t--) { scanf("%d",&n); flag=0; for(i=0;i<n;i++) { scanf("%d",&a[i]); b[a[i]]=i; } for(i=0;i<n;i++) for(j=i+1;j<n;j++) { s=a[i]+a[j]; if(s%2) continue; if(b[s/2]>i&&b[s/2]<j) { flag=1; break; } if(flag) break; } if(flag) printf("Y\n"); else printf("N\n"); } return 0; }