zoukankan      html  css  js  c++  java
  • 80-th Level Archeology

    80-th Level Archeology
    time limit per test
    2 seconds
    memory limit per test
    256 megabytes
    input
    standard input
    output
    standard output

    Archeologists have found a secret pass in the dungeon of one of the pyramids of Cycleland. To enter the treasury they have to open an unusual lock on the door. The lock consists of n words, each consisting of some hieroglyphs. The wall near the lock has a round switch. Each rotation of this switch changes the hieroglyphs according to some rules. The instruction nearby says that the door will open only if words written on the lock would be sorted in lexicographical order (the definition of lexicographical comparison in given in notes section).

    The rule that changes hieroglyphs is the following. One clockwise rotation of the round switch replaces each hieroglyph with the next hieroglyph in alphabet, i.e. hieroglyph x (1 ≤ x ≤ c - 1) is replaced with hieroglyph (x + 1), and hieroglyph c is replaced with hieroglyph1.

    Help archeologist determine, how many clockwise rotations they should perform in order to open the door, or determine that this is impossible, i.e. no cyclic shift of the alphabet will make the sequence of words sorted lexicographically.

    Input

    The first line of the input contains two integers n and c (2 ≤ n ≤ 500 000, 1 ≤ c ≤ 106) — the number of words, written on the lock, and the number of different hieroglyphs.

    Each of the following n lines contains the description of one word. The i-th of these lines starts with integer li (1 ≤ li ≤ 500 000), that denotes the length of the i-th word, followed by li integers wi, 1, wi, 2, ..., wi, li (1 ≤ wi, j ≤ c) — the indices of hieroglyphs that make up the i-th word. Hieroglyph with index 1 is the smallest in the alphabet and with index c — the biggest.

    It's guaranteed, that the total length of all words doesn't exceed 106.

    Output

    If it is possible to open the door by rotating the round switch, print integer x (0 ≤ x ≤ c - 1) that defines the required number of clockwise rotations. If there are several valid x, print any of them.

    If it is impossible to open the door by this method, print  - 1.

    Examples
    input
    4 3
    2 3 2
    1 1
    3 2 3 1
    4 2 3 1 2
    output
    1
    input
    2 5
    2 4 2
    2 4 2
    output
    0
    input
    4 4
    1 2
    1 3
    1 4
    1 2
    output
    -1
    Note

    Word a1, a2, ..., am of length m is lexicographically not greater than word b1, b2, ..., bk of length k, if one of two conditions hold:

    • at first position i, such that ai ≠ bi, the character ai goes earlier in the alphabet than character bi, i.e. a has smaller character in the first position where they differ;
    • if there is no such position i and m ≤ k, i.e. the first word is a prefix of the second or two words are equal.

    The sequence of words is said to be sorted in lexicographical order if each word (except the last one) is lexicographically not greater than the next word.

    In the first sample, after the round switch is rotated 1 position clockwise the words look as follows:


    1 3
    2
    3 1 2
    3 1 2 3

    In the second sample, words are already sorted in lexicographical order.

    In the last sample, one can check that no shift of the alphabet will work.

    分析:运用差分思想,给区间[a,b]+1等价于cnt[a]++,cnt[b+1]--,处理前缀和即可;

    代码:

    #include <iostream>
    #include <cstdio>
    #include <cstdlib>
    #include <cmath>
    #include <algorithm>
    #include <climits>
    #include <cstring>
    #include <string>
    #include <set>
    #include <map>
    #include <queue>
    #include <stack>
    #include <vector>
    #include <list>
    #define rep(i,m,n) for(i=m;i<=n;i++)
    #define rsp(it,s) for(set<int>::iterator it=s.begin();it!=s.end();it++)
    #define mod 1000000007
    #define inf 0x3f3f3f3f
    #define llinf 0x3f3f3f3f3f3f3f3fLL
    #define vi vector<int>
    #define pb push_back
    #define mp make_pair
    #define fi first
    #define se second
    #define ll long long
    #define pi acos(-1.0)
    #define pii pair<ll,int>
    #define Lson L, mid, ls[rt]
    #define Rson mid+1, R, rs[rt]
    #define sys system("pause")
    const int maxn=5e5+10;
    using namespace std;
    ll gcd(ll p,ll q){return q==0?p:gcd(q,p%q);}
    ll qpow(ll p,ll q){ll f=1;while(q){if(q&1)f=f*p;p=p*p;q>>=1;}return f;}
    inline ll read()
    {
        ll x=0;int f=1;char ch=getchar();
        while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
        while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
        return x*f;
    }
    int n,m,k,t,c,now,pre,cnt[maxn<<1],a[2][maxn];
    int main()
    {
        int i,j;
        scanf("%d%d",&n,&c);
        rep(i,1,n)
        {
            scanf("%d",&now);
            rep(j,1,now)scanf("%d",&a[i&1][j]);
            if(i>1)
            {
                bool flag=true;
                rep(j,1,max(now,pre))
                {
                    if(!flag)break;
                    if(a[i&1][j]>a[i&1^1][j])
                    {
                        flag=false;
                        if(!a[i&1^1][j])
                        {
                            cnt[0]++;
                            cnt[c]--;
                        }
                        else
                        {
                            int fb=a[i&1][j],fa=a[i&1^1][j];
                            cnt[0]++;
                            cnt[c-fb+1]--;
                            cnt[c-fa+1]++;
                            cnt[c]--;
                        }
                    }
                    else if(a[i&1][j]<a[i&1^1][j])
                    {
                        flag=false;
                        if(!a[i&1][j])
                        {
                            return 0*puts("-1");
                        }
                        else
                        {
                            int fb=a[i&1][j],fa=a[i&1^1][j];
                            cnt[c-fa+1]++;
                            cnt[c-fb+1]--;
                        }
                    }
                }
                if(flag)
                {
                    cnt[0]++;
                    cnt[c]--;
                }
            }
            rep(j,1,pre)a[i&1^1][j]=0;
            pre=now;
        }
        rep(i,1,c)cnt[i]+=cnt[i-1];
        rep(i,0,c-1)if(cnt[i]==n-1)return 0*printf("%d
    ",i);
        puts("-1");
        //system("Pause");
        return 0;
    }
  • 相关阅读:
    了解PCI Express的Posted传输与Non-Posted传输
    最强加密算法?AES加解密算法Matlab和Verilog实现
    校招必看硬核干货:IC前端这样学,秒变offer收割机!
    一次压力测试Bug排查-epoll使用避坑指南
    硬核干货 | C++后台开发学习路线
    Web服务器项目详解
    O准备如何苟进复赛圈?华为软挑开挂指南(附赛题预测)
    Linux最大文件句柄(文件描述符)限制和修改
    linux中对EINTR错误的处理
    C/C++实现单向循环链表(尾指针,带头尾节点)
  • 原文地址:https://www.cnblogs.com/dyzll/p/5968268.html
Copyright © 2011-2022 走看看