杭电 3833 数论

       话说这道题是昨天中午看的,,,当时看了后想了个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[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.
 

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 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
 

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;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值