代码改变世界

杭电 3833 数论

2011-11-15 09:21  javaspring  阅读(279)  评论(0编辑  收藏  举报

       话说这道题是昨天中午看的,,,当时看了后想了个n*n的方法,,,悲剧的TLE了,,,后来又和几个队友讨论了一下,,也没想出来什么好的方法,,想的都是n*n的,,悲催,,就这样,,从中午一直TLE到昨天晚上10点,,,,今天早上来了后,看了看其他人的思路,,,,,ac了,,不过也是n*n的复杂度。。。。暴力也需要技巧啊,,如果暴力的够艺术,,就能ac。。。。题目:

YY's new problem

Time Limit: 12000/4000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 1936    Accepted Submission(s): 627


Problem Description
Given a permutation P of 1 to N, YY wants to know whether there exists such three elements P[i1], P[i2], P[i3] that 
P[i1]-P[i2]=P[i2]-P[i3], 1<=i1<i2<i3<=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.
 

Output
For each test case, just output 'Y' if such i1, i2, i3 can be found, else 'N'.
 

Sample Input
2 3 1 3 2 4 3 2 4 1
 

Sample Output
N Y
 

ac代码:

#include <iostream>
#include <cstdio>
using namespace std;
class dengcha{
public:
	void setvalue(int x);
	void panduan();
private:
	int num;
};
void dengcha::setvalue(int x){
  num=x;
}
void dengcha::panduan(){
  int pos,a[10010];
  for(int i=1;i<=num;++i){
    scanf("%d",&pos);
	a[pos]=i;
  }
  int flag=0;
  for(int i=2;i<=num;++i){
    pos=a[i];
	int m=(i-1)<(num-i)?(i-1):(num-i);
	for(int j=i-m;j<i;++j){
		if((a[j]-pos)*(a[i*2-j]-pos)<0){
		  flag=1;break;
		}  
	}
	if(flag)
		break;
  }
  if(flag)
  {printf("Y\n");return;}
  printf("N\n");return;
}
int main(){
	dengcha mydengcha;
	int kk;
	scanf("%d",&kk);
	int n;
	while(kk--){
	  scanf("%d",&n);
	  mydengcha.setvalue(n);
	  mydengcha.panduan();
	}
  return 0;
}