Educational Codeforces Round 40 C. Matrix Walk( 思维)

Educational Codeforces Round 40 (Rated for Div. 2)

C. Matrix Walk

time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

There is a matrix A of size x × y filled with integers. For every img, img *A**i, 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, ..., *a**n* 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:

  1. (i + 1, j) — only if i < x;
  2. (i, j + 1) — only if j < y;
  3. (i - 1, j) — only if i > 1;
  4. (i, j - 1) — only if j > 1.

Notice that making a move requires you to go to an adjacent cell. It is not allowed to stay in the same cell. 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, ..., *a**n* (1 ≤ *a**i* ≤ 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

Copy

81 2 3 6 9 8 5 2

output

Copy

YES3 3

input

Copy

61 2 1 2 5 3

output

Copy

NO

input

Copy

21 10

output

Copy

YES4 9

Note

The matrix and the path on it in the first test looks like this:

img

Also there exist multiple correct answers for both the first and the third examples.

Meaning of the questions:

There is a large square, x rows, y columns, and a (i, j) = (i-1) * y + j

Now to give you an array of n numbers, representing only take the adjacent nodes in the grid path through the node values ​​,, so that you can determine whether x and y, if there is, then the output corresponding x and y, otherwise output no.

Ideas:

If the sequence is a valid path, then the absolute value of difference values ​​of adjacent nodes may only be 1 and y,

If the absolute value of the difference in size of the plurality of values ​​(i.e., greater than 2), or have two values, but not 1, then there must not exist.

The next step is size <= 2 in the case,

Y = size is assumed that a larger (if they are equal, i.e. are both 1, then the Japanese sentence output directly to the answer,),

If y is to the value n then swept from 1 to Check, meets this sequence,

Note about the situation:

Currently in the left margin num, num-1 to go

Currently in the right border num, num + 1 to go

It is illegal (have ruled out the case y = 1)

Then the output can.

get: to give you some general information, let you identify some value, the general will ask you if you value the information does not exist to meet, if output is no, then we can assume that after the find of value, go over it again for information, it is determined whether the value of the information. This is a good approach and ideas.

See details Code:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <vector>
#include <iomanip>
#define ALL(x) (x).begin(), (x).end()
#define sz(a) int(a.size())
#define rep(i,x,n) for(int i=x;i<n;i++)
#define repd(i,x,n) for(int i=x;i<=n;i++)
#define pii pair<int,int>
#define pll pair<long long ,long long>
#define gbtb ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define MS0(X) memset((X), 0, sizeof((X)))
#define MSC0(X) memset((X), '\0', sizeof((X)))
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define eps 1e-6
#define gg(x) getInt(&x)
#define chu(x) cout<<"["<<#x<<" "<<(x)<<"]"<<endl
#define du3(a,b,c) scanf("%d %d %d",&(a),&(b),&(c))
#define du2(a,b) scanf("%d %d",&(a),&(b))
#define du1(a) scanf("%d",&(a));
using namespace std;
typedef long long ll;
ll gcd(ll a, ll b) {return b ? gcd(b, a % b) : a;}
ll lcm(ll a, ll b) {return a / gcd(a, b) * b;}
ll powmod(ll a, ll b, ll MOD) {a %= MOD; if (a == 0ll) {return 0ll;} ll ans = 1; while (b) {if (b & 1) {ans = ans * a % MOD;} a = a * a % MOD; b >>= 1;} return ans;}
void Pv(const vector<int> &V) {int Len = sz(V); for (int i = 0; i < Len; ++i) {printf("%d", V[i] ); if (i != Len - 1) {printf(" ");} else {printf("\n");}}}
void Pvl(const vector<ll> &V) {int Len = sz(V); for (int i = 0; i < Len; ++i) {printf("%lld", V[i] ); if (i != Len - 1) {printf(" ");} else {printf("\n");}}}

inline void getInt(int* p);
const int maxn = 1000010;
const int inf = 0x3f3f3f3f;
/*** TEMPLATE CODE * * STARTS HERE ***/
set<int> st;
int y = 0;
int a[maxn];
int ans1 = 1e9;
int n;
int main()
{
    //freopen("D:\\code\\text\\input.txt","r",stdin);
    //freopen("D:\\code\\text\\output.txt","w",stdout);
    gbtb;
    cin >> n;
    repd(i, 1, n)
    {
        cin >> a[i];
    }
    if (n == 1)
    {
        cout << "YES" << endl;
        cout << ans1 << " " << 1 << endl;
        return 0;
    }

    repd(i, 2, n)
    {
        st.insert(abs(a[i] - a[i - 1]));
        y = max(y, abs(a[i] - a[i - 1]));
        if (a[i] == a[i - 1])
        {
            st.insert(10);
            st.insert(11);
            st.insert(14);
            break;
        }
    }
    if (st.size() > 2)
    {
        cout << "NO" << endl;
    } else
    {
        if (st.size() == 2 && (*st.begin()) != 1)
        {
            cout << "NO" << endl;
        }
        else if (y == 1)
        {
            cout << "YES" << endl;
            cout << ans1 << " " << 1 << endl;
        } else
        {
            int isok = 1;

            repd(i, 1, n - 1)
            {
                int cha = a[i + 1] - a[i];
                if ((a[i] % y) == 0 && cha == 1)
                {
                    isok = 0;
                    break;
                } else if ((a[i] % y) == 1 && cha == -1)
                {
                    isok = 0;
                    break;
                }
            }
            if (isok)
            {
                cout << "YES" << endl;
                cout << ans1 << " " << y << endl;
            } else
            {
                cout << "NO" << endl;
            }
        }
    }
    return 0;
}

inline void getInt(int* p) {
    char ch;
    do {
        ch = getchar();
    } while (ch == ' ' || ch == '\n');
    if (ch == '-') {
        *p = -(getchar() - '0');
        while ((ch = getchar()) >= '0' && ch <= '9') {
            *p = *p * 10 - ch + '0';
        }
    }
    else {
        *p = ch - '0';
        while ((ch = getchar()) >= '0' && ch <= '9') {
            *p = *p * 10 + ch - '0';
        }
    }
}



Guess you like

Origin www.cnblogs.com/qieqiemin/p/11701569.html