Codeforces Round #376 (Div. 2) D. 80-th Level Archeology(乱搞)

题目链接:

codeforces731d

题意:

给你n个字符串,每个字符串的元素x取值范围在[1,c],问你能否将所有的x同时进行若干次加1(如果x==c,x变成1)使得字符串是按字典序从小到大排序的。输出最少加的次数,如果没解,输出-1。

数据范围:

1n5105,1c106106

题解:

一开始我去比较每一个位,傻逼了。。直接比较相邻两个字符串就可以了!
我们通过比较相邻两个字符串的通过循环多少次不能得到答案,我们得到了若干个区间,然后我们找到没有被这些区间包含的最小的数,就可以了!至于怎么找呢,我们假设在[l,r]区间不能得到答案,我们a[l]++,a[r+1]–,我们可以容易得到如果a的前缀和为0,就是没有被任何区间包含!

代码:

#include<cstdio>
#include<cstring>
#include<iostream>
#include<sstream>
#include<algorithm>
#include<vector>
#include<bitset>
#include<set>
#include<queue>
#include<stack>
#include<map>
#include<cstdlib>
#include<cmath>
#define PI 2*asin(1.0)
#define LL long long
#define pb push_back
#define pa pair<int,int>
#define clr(a,b) memset(a,b,sizeof(a))
#define lson lr<<1,l,mid
#define rson lr<<1|1,mid+1,r
#define bug(x) printf("%d++++++++++++++++++++%d\n",x,x)
#define key_value ch[ch[root][1]][0]
const int  MOD = 1000000007;
const int N = 5E5+15;
const int maxn = 400+ 14;
const int mm=100000+15;
const int letter = 130;
const int INF = 1e9;
const double pi=acos(-1.0);
const double eps=1e-8;
using namespace std;
inline int read()
{
    int x=0,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,c,a[N],ps[N];
vector<int>g[N];
void add(int l,int r){
    ps[l]++,ps[r+1]--;
}
int main(){
    int x,y;
    scanf("%d%d",&n,&c);
    for(int i=0;i<n;i++){
        scanf("%d",&y);
        while(y--){
            scanf("%d",&x);
            g[i].pb(x);
        }
    }
    for(int i=0;i<n-1;i++){
        int sz=min(g[i].size(),g[i+1].size());
        int flag=1;
        for(int j=0;j<sz;j++){
            int a=g[i][j],b=g[i+1][j];
            if(a==b) continue;
            else if(a>b) add(0,c-a),add(c-b+1,c-1);
            else add(c-b+1,c-a);
            flag=0;
            break;
        }
        if(flag){
            if(g[i].size()>g[i+1].size()){
                printf("%d\n",-1);
                return 0;
            }
        }
    }
    int cc=0;
    for(int i=0;i<=c-1;i++){
        cc+=ps[i];
        if(cc==0){
            printf("%d\n",i);
            return 0;
        }
    }
    puts("-1");
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值