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

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

题意:

有一个很大的方格,x行,y列,以及a(i,j)=(i-1)*y+j

现在给你n个数的数组,代表在方格中只走相邻节点的路径经过的节点数值,,让你是否能确定一个x和y,如果有,则输出对应的x和y,否则输出no。

思路:

如果是一个合法的路径序列,那么相邻的节点的数值之差的绝对值只可能是1和y,

如果差的绝对值size有多个值(即大于2),或者有2个值但没有1,那么一定是不存在的。

接下来就是size<=2的情况了,

假设y=size 中较大的那一个(如果相等,即都为1,那么直接特判输出答案即可,),

去再从1到n扫check下如果y是该值,是否满足该序列,

注意一下情况:

当前在左边界num,向num-1走

当前在右边界num,向num+1走

都是不合法的(已经排除了y=1的情况)

然后输出即可。

get:一般给你一些信息,让你确定一些值的时候,一般还会问你是否不存在满足该信息的数值,如果是输出no,那么我们就可以在找到假设的数值之后,再去过一遍给的信息,判定是否信息和数值对的上。这是一个很好的处理方法和思路。

细节见代码:

#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';
        }
    }
}



全部评论

相关推荐

2025-11-21 22:25
门头沟学院 HTML5
我是个没天赋的人,努力学习也只考上了个一本,家里条件也不怎么样。大一玩了一年,没怎么学技术,也没有卷绩点,全在游戏小说抖音中度过。大二上接触了牛客,看到了许多优秀的同龄人。很多双非的同学,甚至不少学院本的同学都进了大厂实习。我把他们作为榜样,决定好好学习。我每天都至少学八九个小时,很多次都想要放弃,想哭,我都坚持了下来。我总是告诉自己,只要努力,就一定能有好的结果。这几个月过的很累,但也很充实。转眼就到大二下了,我决定去找实习了,但是学校的认可度让我感到心底发凉,明明和广工这种知名双非分差不多,结果总被问是不是公办本科。两个月投了一千份实习,只有四个面试,最终去了个中小厂实习。结果就是改了两个月bug,虽然mt人挺好,但是实在学不了什么东西,所以就离职开始面试。凭借这段实习,确实多了不少中小厂面试,但是大厂依旧没有面试机会。除了字节腾讯所有大厂都投了,结果依旧是0面试。最终有幸获得美团的面试机会,面试也幸运的通过,然后入职了。为了省钱坐十几个小时硬座到北京,到北京的第一天,由于太激动想要租房,结果被坑了2600,之前实习的地方,房东也故意不退押金,加起来总共损失3000多。虽然很难过,但是我还是忍受了下来,我想着实习才刚开始,会好起来的。实习了大半个月,跟学校这边沟通一直不成功,我每天都寝食难安,精神都快崩溃了,经常凌晨两三点才睡着,想要跳楼。最后迫于无奈,我一大早我坐高铁回去,恳求院主任给我一个机会,我怎么恳求讲理都没用,甚至都磕头下跪了,还是没用。院主任一点机会都没给我,连让我跟各科老师沟通机会都不给,要不休学要不辞职。我没得选择,这段实习我看的比我的生命还重要,这不仅是我这大半年的心血,更是未来的一份希望。我只能休学,我想着现在好好实习,多学点技术,到时候秋招早点拿到offer,然后再补这学期的课也不是不行。但是,现实总是事与愿违。这三个月说实话并没有学到什么东西,前一个月很闲,这两个月事很多,每天基本都是九点后下班,但都是杂活。产出都是靠我看文档加上代码写上去的。我真的很想锻炼一下技术,但是总是不尽人意。三个月了,我到现在都还没做过一个像样点的需求。产出是能编,但有破绽不说还没锻炼到技术。我好想真正的做一下需求啊,我好想真正的走完一遍流程,去上线一次啊。接下来两个月,我不知道该怎么坚持下去了,现在每天都想哭,很焦虑,很难受。冒着将来可能延毕的风险,我赌上了一切,结果输的这么彻底,可能我就只是个小丑吧。如果家庭好点就不用卷了,如果我聪明一点就能上个好学校了,如果大一有人带我,我就不会摆烂了,如果院主任给我个机会,我就不用这么苦了,如果我实习能有机会好好锻炼自己,我就不用这么难受焦虑了。但是没办法,我又能怎么办呢,无非是咬紧牙关罢了,毕竟没人能够帮助我,只能靠自己我可真是个小丑啊
HasonoCell:你很棒了bro....其实我看网上休学一年的人很多的也都顺利毕业了,真的不用特别焦虑这个事。另外实习也是,有一段大厂实习已经比很多很多人厉害了,跟你一届的很多人现在估计都没意识到未来的压力呢,实习就算没产出也不用特别焦虑,好好总结一下已经做过的事情,然后趁着休学这年继续冲一下,要相信未来会有好结果的。你应该也挺眼熟我的,我之前字节横向挂的时候也是难过的不行,觉得自己好没用,结果百度出乎意料的offer了,很多事其实都很顺其自然,认真做事,好结果也许就在下个路口等着你。 很喜欢的一句话是:木已成舟。不要老是沉浸在过去的遗憾中无法自拔噢,要努力过好当下。 好好休息一下吧,辛苦了,你已经很棒了噢
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务