D. 80-th Level Archeology 区间扫描

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 hieroglyph 1.

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.

题意:给你几个单词,你有一种操作,将所有单词的所有字母+1,当然字母最大号为c,c再加上1就变成1了(就1~c轮着转)。问你最少操作几次使得所有的单词满足字典序?如果根本不能满足输出-1

前后两两找第一个不同,计算不可行区间,注意找不到时的长度情况,最后区间扫描

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<queue>

using namespace std;

int a[500005];
int pr[500005][2];
struct node
{
    int l,r;
}que[500005*2];

bool cmp(node x,node y)
{
    return x.l<y.l;
}


int main()
{
    int n,c;
    while(~scanf("%d%d",&n,&c))
    {
        int cnt=0;
        int len,prelen;
        int ff=0;
        for(int i=0;i<n;i++)
        {
            int dif=-1;
            int l,r;
            scanf("%d",&len);
            for(int j=0;j<len;j++)
            {
                int tp;
                scanf("%d",&tp);
                if(tp!=a[j] && dif==-1 && i!=0 && j<prelen)
                {
                    dif=0;
                    if(a[j]<=tp)
                    {
                       que[cnt].l=c+1-tp;
                       que[cnt].r=c-a[j];
                       cnt++;
                    }
                    else
                    {
                        que[cnt].l=0;
                        que[cnt].r=c-a[j];
                        cnt++;

                        que[cnt].l=c+1-tp;
                        que[cnt].r=c-1;
                        cnt++;
                    }
                }
                a[j]=tp;
            }
            if(i!=0 && dif==-1 && len<prelen)
            {
                ff=1;
            }
            prelen=len;
        }

        if(ff==1)
        {
            printf("-1\n");
        }
        else
        {
            int fg=-1;
            sort(que,que+cnt,cmp);
            int las=-1;
            for(int i=0;i<cnt;i++)
            {
                if(que[i].l>(las+1))
                {
                    fg=las+1;
                }

                if(que[i].r>las)
                {
                    las=que[i].r;
                }
            }

            if(fg!=-1)
            {
                printf("%d\n",fg);
            }
            else if(las!=(c-1))
            {
                printf("%d\n",las+1);
            }
            else
            {
                printf("-1\n");
            }

        }

    }
}


 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值