Problem Description
In a military parade, the King sees lots of new things, including an Andriod Phone. He becomes interested in the pattern lock screen. The pattern interface is a × square lattice, the three points in the first line are labeled as ,,, the three points in the second line are labeled as ,,, and the three points in the last line are labeled as ,,。The password itself is a sequence, representing the points in chronological sequence, but you should follow the following rules: - The password contains at least four points. - Once a point has been passed through. It can't be passed through again. - The middle point on the path can't be skipped, unless it has been passed through(3427 is valid, but 3724 is invalid). His password has a length for a positive integer k(≤k≤), the password sequence is s1,s2...sk(≤si<INT_MAX) , he wants to know whether the password is valid. Then the King throws the problem to you.
Input
The first line contains a number T(<T≤), the number of the testcases. For each test case, there are only one line. the first first number k,represent the length of the password, then k numbers, separated by a space, representing the password sequence s1,s2...sk.
Output
Output exactly T lines. For each test case, print `valid` if the password is valid, otherwise print `invalid`
Sample Input
Sample Output
invalid
valid
valid hint:
For test case #:The path $\rightarrow $ skipped the middle point $$, so it's invalid. For test case #:The path $\rightarrow $ doesn't skipped the middle point $2$, because the point 2 has been through, so it's valid. For test case #:The path $\rightarrow \rightarrow \rightarrow $ doesn't have any the middle point $2$, so it's valid.
Source
一个简单的模拟题,首先判断序列长度是否合法,接着判断 sis_isi 是否在 [1,9],最后扫一遍看看有没有重复以及跨过中间点的情况即可。
复杂度:O(nT)。
AC代码:
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<math.h>
#include<algorithm>
#include<queue>
#include<set>
#include<bitset>
#include<map>
#include<vector>
#include<stdlib.h>
#include <stack>
using namespace std;
#define PI acos(-1.0)
#define max(a,b) (a) > (b) ? (a) : (b)
#define min(a,b) (a) < (b) ? (a) : (b)
#define ll long long
#define eps 1e-10
#define MOD 1000000007
#define N 16
#define inf 1e12
int n;
int a[N],vis[N];
bool judge(int num1,int num2){
if(vis[num2]) return false;
vis[num2]=;
if(num1==){
if(num2== && vis[]==){
return false;
}
if(num2== && vis[]==){
return false;
}
if(num2== && vis[]==){
return false;
}
}
if(num1==){
if(num2== && vis[]==){
return false;
}
}
if(num1==){
if(num2== && vis[]==){
return false;
}
if(num2== && vis[]==){
return false;
}
if(num2== && vis[]==){
return false;
}
}
if(num1==){
if(num2== && vis[]==){
return false;
}
}
if(num1==){
if(num2== && vis[]==){
return false;
}
}
if(num1==){
if(num2== && vis[]==){
return false;
}
if(num2== && vis[]==){
return false;
}
if(num2== && vis[]==){
return false;
}
}
if(num1==){
if(num2== && vis[]==){
return false;
}
}
if(num1==){
if(num2== && vis[]==){
return false;
}
if(num2== && vis[]==){
return false;
}
if(num2== && vis[]==){
return false;
}
} return true;
}
int main()
{
int t;
scanf("%d",&t);
while(t--){
memset(vis,,sizeof(vis));
scanf("%d",&n);
int flag=;
//int num_valid=0;
for(int i=;i<n;i++){
scanf("%d",&a[i]);
if(a[i]< || a[i]>) flag=;
}
if(flag==){
printf("invalid\n");
continue;
}
flag=;
for(int i=;i<n;i++){
if(vis[a[i]]){
flag=;
break;
}
vis[a[i]]=;
}
if(flag==) {
printf("invalid\n");
continue;
}
if(n<) {
printf("invalid\n");
continue;
}
memset(vis,,sizeof(vis));
vis[a[]]=;
int ok=;
for(int i=;i<n-;i++){
if(judge(a[i],a[i+])==false){
ok=;
break;
}
}
if(ok) printf("valid\n");
else printf("invalid\n");
}
return ;
}