CodeForces 731D (差分+线段扫描)
Description
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.
Sample Input
4 3
2 3 2
1 1
3 2 3 1
4 2 3 1 2
1
2 5
2 4 2
2 4 2
0
4 4
1 2
1 3
1 4
1 2
-1
给你几个单词,你有一种操作,将所有单词的所有字母+1,当然字母最大号为c,c再加上1就变成1了(就1~c轮着转)。问你最少操作几次使得所有的单词满足字典序?如果根本不能满足输出-1。
1.首先介绍下什么是差分法。比如给你一个长度为n的数组cnt,一开始全是0。现在如果让你从下标2~4的位置都+1,怎么做?cnt[2]++,cnt[5]--
数组变成了0 0 1 0 0 -1 0....,我们再进行for(int i=0;i<n;++i)cnt[i]+=cnt[i-1]; 现在变成了0 0 1 1 1 0 0...是不是2~4都+1了?
总结一下,差分的想法就是在区间a~b中+1,等价于cnt[a]++,cnt[b+1]--,然后再处理一边前缀和就行了。
2.什么是线段扫描法?给你n个区间,问你有没有一个公共的区间是这所有n个区间的公共子区间。
首先把这几个区级排好,然后找一条竖着的直线,从左向右平移,然后看有没有一瞬间这个直线与所有区间都相交,及有n个交点。
PS:CF的题目质量真高啊!感觉受益匪浅,这个题是我队友教我的,感谢他。我也要努力超过他!
代码如下:
#include <bits/stdc++.h> using namespace std;
vector <int> words[];
int cnt[],n,c;//cnt表示转几次是否满足要求
void calc (int a,int b)
{
int idx=;//idx表示失配位置
while (idx<words[a].size()&&idx<words[b].size())
{
if (words[a][idx]!=words[b][idx])
break;
idx++;
}
if (idx<words[a].size()&&idx<words[b].size())//失配的位置在a,b的中部
{
if (words[a][idx]<words[b][idx])//在失配位置是b的大于a的
{
cnt[]++;
cnt[c-words[b][idx]+]--;
cnt[c+-words[a][idx]]++;
cnt[c]--;
}
else
{
cnt[c+-words[a][idx]]++;
cnt[c-words[b][idx]+]--;
}
}
else if (idx==words[a].size()&&idx!=words[b].size())//a比b短
{
cnt[]++; //a 123
cnt[c]--; //b 123456
}
else if (idx!=words[a].size()&&idx==words[b].size())//a比b长
//a 12345
//b 123
;
else //a和b完全一样
{
cnt[]++;
cnt[c]--;
}
}
int main()
{
//freopen("de.txt","r",stdin);
while (~scanf("%d%d",&n,&c))
{
memset(cnt,,sizeof cnt);
for (int i=;i<;++i)
words[i].clear();
for (int i=;i<n;++i)
{
int len,w;
scanf("%d",&len);
while (len--)
{
scanf("%d",&w);
words[i].push_back(w);
}
}
for (int i=;i<n-;++i)
calc(i,i+);
bool ok=false;
int sum=;
for (int i=;i<c;++i)
{
sum+=cnt[i];
if (sum==n-)
{
ok=true;
printf("%d\n",i);
break;
}
}
if (!ok)
printf("-1\n");
}
return ;
}
CodeForces 731D (差分+线段扫描)的更多相关文章
- G - Greg and Array CodeForces - 296C 差分+线段树
题目大意:输入n,m,k.n个数,m个区间更新标记为1~m.n次操作,每次操作有两个数x,y表示执行第x~y个区间更新. 题解:通过差分来表示某个区间更新操作执行的次数.然后用线段树来更新区间. #i ...
- 【bzoj5028】小Z的加油店 扩展裴蜀定理+差分+线段树
题目描述 给出 $n$ 个瓶子和无限的水,每个瓶子有一定的容量.每次你可以将一个瓶子装满水,或将A瓶子内的水倒入B瓶子中直到A倒空或B倒满.$m$ 次操作,每次给 $[l,r]$ 内的瓶子容量增加 $ ...
- [Luogu5327][ZJOI2019]语言(树上差分+线段树合并)
首先可以想到对每个点统计出所有经过它的链的并所包含的点数,然后可以直接得到答案.根据实现不同有下面几种方法.三个log:假如对每个点都存下经过它的链并S[x],那么每新加一条路径进来的时候,相当于在路 ...
- Greg and Array CodeForces 296C 差分数组
Greg and Array CodeForces 296C 差分数组 题意 是说有n个数,m种操作,这m种操作就是让一段区间内的数增加或则减少,然后有k种控制,这k种控制是说让m种操作中的一段区域内 ...
- [BZOJ3307] 雨天的尾巴(树上差分+线段树合并)
[BZOJ3307] 雨天的尾巴(树上差分+线段树合并) 题面 给出一棵N个点的树,M次操作在链上加上某一种类别的物品,完成所有操作后,要求询问每个点上最多物品的类型. N, M≤100000 分析 ...
- LUOGU P1438 无聊的数列 (差分+线段树)
传送门 解题思路 区间加等差数列+单点询问,用差分+线段树解决,线段树里维护的就是差分数组,区间加等差数列相当于在差分序列中l位置处+首项的值,r+1位置处-末项的值,中间加公差的值,然后单点询问就相 ...
- CodeForces - 587E[线段树+线性基+差分] ->(线段树维护区间合并线性基)
题意:给你一个数组,有两种操作,一种区间xor一个值,一个是查询区间xor的结果的种类数 做法一:对于一个给定的区间,我们可以通过求解线性基的方式求出结果的种类数,而现在只不过将其放在线树上维护区间线 ...
- CodeForces 91B Queue (线段树,区间最值)
http://codeforces.com/problemset/problem/91/B B. Queue time limit per test: 2 seconds memory limit p ...
- P1438 无聊的数列 (差分+线段树)
题目 P1438 无聊的数列 解析: 先考虑修改,用差分的基本思想,左端点加上首项\(k\),修改区间\((l,r]\)内每个数的差分数组都加上公差\(d\),最后的\(r+1\)再减去\(k+(r- ...
随机推荐
- 接口参数校验之@Valid与BindingResult
接口方法往往需要对入参做一些校验,从而判断入参是否合格,而javax.validation包为我们提供了一些常用的参数校验注解,使用起来很方便. 下面这个示例是检验入参对象中的password是否为空 ...
- sql 用语句还原多数据文件的数据库
/* 还原bak数据库文件*/ RESTORE DATABASE userdb_cs /*还原为userdb名的新数据库*/ FROM DISK = 'D:\工作资料\数据库文件\userdb.bak ...
- php str_split()函数 语法
php str_split()函数 语法 str_split()函数怎么用 php str_split()函数用于把字符串分割到数组中,语法是str_split(string,length),将字符串 ...
- 【Web API]无法添加AttributeRoutes的解决方案
1.按照微软官方文档,如果要使用AttributeRoutes,需要在APP_START里的WebApiConfig.cs的Register方法中添加一行:config.MapHttpAttribut ...
- Html5 学习笔记 --》html基础 css 基础
HTML5 功能 HTML5特点 <!DOCTYPE html> <html lang="zh-cn"> <head> <meta cha ...
- l1和l2正则化
https://blog.csdn.net/tianguiyuyu/article/details/80438630 以上是莫烦对L1和L2的理解 l2正则:权重的平方和,也就是一个圆 l1正则:权重 ...
- Numpy的基本运算及操作
import numpy as np ''' 一.算术运算 元素级 1.标量 加减乘除 数组(元素级:位置对应) 自增和自减 通用函数 2.数组 +-*/ 数组 (元素级) 3.条件和布尔运算 a&g ...
- JNDI+Tomcat配置数据源的两种方式
非全局jndi配置步骤 :此种配置方式不需要在server.xml中配置数据源,而只在tomcat/conf/Catalina/localhost下的启动配置中配置即可.注意红色字体名称必须和相同. ...
- 【转】vim 配置文件 ,高亮+自动缩进+行号+折叠+优化
将一下代码copy到 用户目录下 新建文件为 .vimrc保存即可生效: 如果想所有用户生效 请修改 /etc/vimrc (建议先cp一份) "===================== ...
- js实现图片延迟加载原理
<img src="image/1188695.png" alt="taobao" trueImg="image/1.jpg" id= ...