题目链接:

codeforces731d

题意:

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

数据范围:

1≤n≤5∗105,1≤c≤106,字符串的总长度≤1061\leq n\leq 5*10^5,1\leq c \leq 10^6,字符串的总长度\leq 10^6

题解:

一开始我去比较每一个位,傻逼了。。直接比较相邻两个字符串就可以了!
我们通过比较相邻两个字符串的通过循环多少次不能得到答案,我们得到了若干个区间,然后我们找到没有被这些区间包含的最小的数,就可以了!至于怎么找呢,我们假设在[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;
}

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

  1. Codeforces Round #376 (Div. 2) D. 80-th Level Archeology —— 差分法 + 线段扫描法

    题目链接:http://codeforces.com/contest/731/problem/D D. 80-th Level Archeology time limit per test 2 sec ...

  2. Codeforces Round #376 (Div. 2) B. Coupons and Discounts

    题目链接:http://codeforces.com/contest/731/problem/B #include<bits/stdc++.h> #define MX 200007 usi ...

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

    原题网址:http://codeforces.com/contest/731/problem/D 对于相邻的两行,我们可以求出合法的答案区间,然后对所有的区间取一下区间并.因为只有最后一次询问,所以对 ...

  4. Codeforces Round #376 (Div. 2) C. Socks

    可能英语水平比较差,这个题我刚读起来是懵逼的. C. Socks time limit per test 2 seconds memory limit per test 256 megabytes i ...

  5. hash+set Codeforces Round #291 (Div. 2) C. Watto and Mechanism

    题目传送门 1 /* 2 hash+set:首先把各个字符串的哈希值保存在set容器里,然后对于查询的每一个字符串的每一位进行枚举 3 用set的find函数查找是否存在替换后的字符串,理解后并不难. ...

  6. BFS Codeforces Round #297 (Div. 2) D. Arthur and Walls

    题目传送门 1 /* 2 题意:问最少替换'*'为'.',使得'.'连通的都是矩形 3 BFS:搜索想法很奇妙,先把'.'的入队,然后对于每个'.'八个方向寻找 4 在2*2的方格里,若只有一个是'* ...

  7. Codeforces Round #434 (Div. 2)【A、B、C、D】

    Codeforces Round #434 (Div. 2) codeforces 858A. k-rounding[水] 题意:已知n和k,求n的最小倍数x,要求x后缀至少有k个0. 题解:答案就是 ...

  8. Codeforces Round #510 (Div. 2)

    Codeforces Round #510 (Div. 2) https://codeforces.com/contest/1042 A 二分 1 #include<iostream> 2 ...

  9. Codeforces Round #257 (Div. 2)

    Codeforces Round #257 (Div. 2) https://codeforces.com/contest/450/ A 模拟 1 #include<bits/stdc++.h& ...

最新文章

  1. 移动互联网漫谈(3)
  2. Android之getSystemService
  3. java数组空指针一场_Java使用自定义类数组报空指针异常
  4. codeigniter详细笔记
  5. Greedy Sequence(2019南京icpc网络预选赛)主席树求区间小于k的最大值
  6. 10月15日 第一天申请成功
  7. 惠普继续大裁员:未来3年计划裁撤7000-9000个岗位
  8. MACAPP中引入ffmpeg库完成具体功能
  9. 使用 TensorFlow 做文本情感分析
  10. NAS媒体库资源归集整理工具nas-tools
  11. 最小生成树算法之Prim算法
  12. python 全栈什么意思_python全栈指的是什么意思
  13. 6. 工业大数据的实施策略
  14. EXCEL中数字显示为E+18且尾数为0的解决方法
  15. 亚马逊测评做单总是被砍单封号是什么原因?
  16. STM32 keil5 报错:flash download failed-cortex M3解决方法
  17. HANA学习季(一) :HANA的基本概念
  18. 服务器性能评分,服务器CPU排行榜之服务器CPU性能评分
  19. ibeacon和微信周边介绍
  20. 求Deepin litexp v6.2 帮帮忙吧!!

热门文章

  1. ChatGPT商业前景如何?人工智能未来会如何发展?
  2. Delphi 版的 Ping
  3. 【C++】逆波兰表达式 + 反向迭代器的实现
  4. Flask 吐血400错误
  5. ubuntu命令查看CPU信息
  6. python波形图怎么像MATLAB一样叠在一起对比分析
  7. 【转载】Iptables详解
  8. 白盒测试案例设计(我爷爷都能看懂)
  9. 互联网产品设计进阶(17)设计良好的UGC激励机制
  10. 人体建模--骨骼--maya--unity人物控制