C. Matrix Walk
time limit per test1 second
memory limit per test256 megabytes
There is a matrix A of size x × y filled with integers. For every , Ai, j = y(i - 1) + j. Obviously, every integer from [1..xy] occurs exactly once in this matrix.
You have traversed some path in this matrix. Your path can be described as a sequence of visited cells a1, a2, …, an denoting that you started in the cell containing the number a1, then moved to the cell with the number a2, and so on.
From the cell located in i-th line and j-th column (we denote this cell as (i, j)) you can move into one of the following cells:
(i + 1, j) — only if i < x;
(i, j + 1) — only if j < y;
(i - 1, j) — only if i > 1;
(i, j - 1) — only if j > 1.
You don’t know x and y exactly, but you have to find any possible values for these numbers such that you could start in the cell containing the integer a1, then move to the cell containing a2 (in one step), then move to the cell containing a3 (also in one step) and so on. Can you choose x and y so that they don’t contradict with your sequence of moves?
Input
The first line contains one integer number n (1 ≤ n ≤ 200000) — the number of cells you visited on your path (if some cell is visited twice, then it’s listed twice).
The second line contains n integers a1, a2, …, an (1 ≤ ai ≤ 109) — the integers in the cells on your path.
Output
If all possible values of x and y such that 1 ≤ x, y ≤ 109 contradict with the information about your path, print NO.
Otherwise, print YES in the first line, and in the second line print the values x and y such that your path was possible with such number of lines and columns in the matrix. Remember that they must be positive integers not exceeding 109.
Examples
input
8
1 2 3 6 9 8 5 2
output
YES
3 3
input
6
1 2 1 2 5 3
output
NO
input
2
1 10
output
YES
4 9
Note
The matrix and the path on it in the first test looks like this:
Also there exist multiple correct answers for both the first and the third examples.
题意:已知该人的旅游路线,编号的规则为 Ai, j = y(i - 1) + j
,让你求是否存在某个矩阵的满足路线,不懂的可以看下样例1
分析: 如果满足的话任意两个点相邻要么是1要么是y,我们可以先把y拿出来,然后依次判断,这里有个坑点是 如果 是3*3的图,给你3 4
就是不符合的,因为在图上他们不相连,再加个check即可
参考代码
#include<bits/stdc++.h>
using namespace std;
const int N = 3e5 + 10;
int a[N];
int main(){
ios_base::sync_with_stdio(0);
int n;cin>>n;
int y = 1;
for (int i = 0; i < n; i++) {
cin>>a[i];
if(i == 0) continue;
if(a[i] == a[i - 1]) {
cout<<"NO"<<endl;return 0;
}
if(abs(a[i] - a[i - 1]) != 1) {
y = abs(a[i] - a[i - 1]);
}
}
for (int i = 1; i < n; i++) {
if((abs(a[i] - a[i - 1]) != 1 && abs(a[i] - a[i - 1]) != y)){
// cout<<"++"<<endl;
cout<<"NO"<<endl;return 0;
}
if(a[i] % y == 0 && a[i - 1] - a[i] == 1 && y != 1) {
//cout<<"--"<<endl;
cout<<"NO"<<endl;return 0;
} else if(a[i] % y == 1 && a[i] - a[i - 1] == 1 && y != 1) {
// cout<<"**"<<endl;
cout<<"NO"<<endl;return 0;
}
}
cout<<"YES"<<endl<<1000000000<<' '<<y<<endl;
return 0;
}