列表

详情


NC25728. Flag

描述

    Two integer sequences existed initially, both of them are arithmetic progressions.
    Arithmetic progression is a sequence of numbers such that the difference between the consecutive terms is constant. Difference here means the second minus the first. For instance, the sequence 5, 7, 9, 11, 13, 15 . . . is an arithmetic progression with common difference of 2. \textbf{Note that the empty sequence and the sequence consisting of one element also can be considered as arithmetic progressions}.
    Elements of the first sequence are inserted between elements of the second one (and, possibly, before its first element and after its last element) \textbf{without changing the order}. For example, sequences [1,2,3] and [6,5,4] can produce the following resulting sequences: [1,6,2,5,3,4], [1,2,3,6,5,4]. The following sequence cannot be the result of these insertions: [1,3,6,2,5,4] because the order of elements in the first sequence was changed.
    As an acmer with ambitious goals, Flag Qing always sets plenty of flags. This time, Flag Qing supposes that for each given sequence A, he can find two suitable arithmetic sequences, so that A can be produced by them according to the above rules.
Now given a sequence A, please tell Flag Qing if he can realize his flag.

输入描述

    The first line contains an integer number T, the number of test cases.
    For each test case:
    The first line contains an integer n(), the number of sequence A.
    The second line contains n integers (), the elements of sequence A.

输出描述

For each test case print“YES”(without quotes) if it is possible, and“NO”(without quotes) otherwise.

示例1

输入:

2
6
1 6 2 5 3 4
6
1 2 7 3 5 1

输出:

YES
NO

原站题解

上次编辑到这里,代码来自缓存 点击恢复默认模板

C++14(g++5.4) 解法, 执行用时: 644ms, 内存消耗: 8264K, 提交时间: 2019-05-13 19:41:11

#include<cstdio>
using namespace std;
const int maxn = 1e5+9;
int n, a[maxn];
bool w(int pos, int nx, int dx, int ny, int dy){
	if(pos>n){
		return true;
	}
	if(a[pos]==nx){
		if(w(pos+1, nx+dx, dx, ny, dy)){
			return true;
		}	
	}
	if(a[pos]==ny){
		if(w(pos+1, nx, dx, ny+dy, dy)){
			return true;
		}
	}
	return false;
} 
void yes(){
	puts("YES");
}
bool sol(int pos, int nx, int dx, int y1){
	for(int i=pos; i<=n; ++i){
		if(w(i, nx, dx, a[i], a[i]-y1)){
			return true;
		}
		if(a[i]!=nx){
			break;
		}
		nx+=dx;
	}
	return false;
}
void solve(){
	scanf("%d", &n);
	for(int i=1; i<=n; ++i){
		scanf("%d", &a[i]);
	}
	if(n<=4){
		yes();
		return ;
	}
	bool flag=true;
	for(int i=2; i<=n; ++i){
		if(a[i]-a[i-1]!=a[2]-a[1]){
			flag=false;
			break;
		}
	}
	if(flag){
		yes();
		return ;
	}
	for(int k=2; k<n; ++k){
		if(a[k]-a[k-1]!=a[2]-a[1]){
			break;
		}
		if(sol(k+2, a[k]+a[k]-a[k-1], a[k]-a[k-1], a[k+1])){
			yes();
			return ;
		}
	}
	if(sol(4, a[3]+a[3]-a[1], a[3]-a[1], a[2])){
		yes();
		return ;
	}
	if(sol(4, a[3]+a[3]-a[2], a[3]-a[2], a[1])){
		yes();
		return ;
	}
	puts("NO");
	return ;
}
int main(){
	int T;
	scanf("%d", &T);
	while(T--){
		solve();
	} 
	return 0;
} 

上一题