Educational Codeforces Round 40 (Rated for Div. 2) C. Matrix Walk (一定要细心啊)

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

  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, ..., 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
Copy
8
1 2 3 6 9 8 5 2
Output
YES
3 3
Input
Copy
6
1 2 1 2 5 3
Output
NO
Input
Copy
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.

这题我是比赛完才做出来的,代码比较乱,但是把坑点全找出来了
坑点一:考虑只有一个元素时    1    1000
坑点二:考虑给出的数组并不一定是从一开始的   2  99999 100000
坑点三:考虑数组中相邻元素相差一并不一定合法如    3    1   4  5
代码比较乱


#include <bits/stdc++.h>

using namespace std;
long long arr[200010];
int main()
{
    long long n,a,b,p=0,m=0,zz=0;
    cin >>n;
    for(int i=0;i<n;i++)
    {
        cin >> arr[i];
        if(i!=0&&(arr[i]>arr[i-1]+1||arr[i]<arr[i-1]-1)&&p==0)
        {
            b=abs(arr[i]-arr[i-1]);
            p=1;
        }
        if(i!=0&&arr[i]==arr[i-1])
        {
           zz=1;
        }
        m=max(m,arr[i]);
    }
    if(zz==1)
    {
         cout << "NO"<<endl;
        return 0;
    }
    if(n==1)
    {
        cout <<"YES"<<endl;
        cout << arr[0] << " " << 1 << endl;
        return 0;
    }
    if(p==0)
    {
        cout <<"YES"<<endl;
        cout << 1<<" " <<m << endl;
        return 0;
    }
    for(int i=0;i<n;i++)
    {
        if(i==0) continue;
        if((arr[i]==arr[i-1])||(!(arr[i]==arr[i-1]+1||arr[i]==arr[i-1]-1||arr[i]==arr[i-1]+b||arr[i]==arr[i-1]-b)))
        {
            //cout << 1 << endl;
            cout << "NO"<<endl;
            return 0;
        }
        if(arr[i-1]%b==0&&arr[i]==arr[i-1]+1)
        {
             //cout << 3 << endl;
            cout << "NO"<<endl;
            return 0;
        }
         if(arr[i-1]!=1&&arr[i-1]%b==1&&arr[i]==arr[i-1]-1)
        {
            cout << "NO"<<endl;
            return 0;
        }
    }
    cout <<"YES"<<endl;
    cout << (m-1)/b+1 << " "<< b <<endl;
    return 0;

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值