Codeforces Round #376 (Div. 2) D. 80-th Level Archeology —— 差分法 + 线段扫描法
题目链接:http://codeforces.com/contest/731/problem/D
2 seconds
256 megabytes
standard input
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 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.
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.
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.
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
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.
题意:
给出n个单词(字母的取值范围为1~c),以及一种操作:将每个单词的每个字母+1(如果字母为c,则变为1)。
问:能否经过若干次操作后,这些单词以字典序排列?若能形成字典序,那么操作次数的范围为:0~c-1 , 因为操作c次后又变回原来的值;若不能形成字典序,则输出-1。
题解:
对于上下两个单词的第一次出现字母不相等的位置,为了使其形成升序,有相应的操作次数的区间。
然后每一对单词,都有相应的区间,最后答案就在所有区间的交集上取得(区间用差分法实现)。
线段扫描法:在差分法的基础上,求前缀和,当前缀和刚好等于n-1时(总共有n-1对单词),即表明此处满足每一对单词的取值。
学习之处:
1.选定了某一操作对象,如果很难下手,那么可以更换操作对象。
2.差分法 + 线段扫描法 求交集。
代码如下:
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const double eps = 1e-6;
const int INF = 2e9;
const LL LNF = 9e18;
const int mod = 1e9+7;
const int maxn = 1e6+10; vector<int>v[maxn>>1];
int range[maxn];
int n,c; void init()
{
scanf("%d%d",&n,&c);
memset(range,0,sizeof(range));
for(int i = 1; i<=n; i++)
v[i].clear();
for(int i = 1; i<=n; i++)
{
int tot,x;
scanf("%d",&tot);
v[i].push_back(tot);
for(int j = 1; j<=tot; j++)
{
scanf("%d",&x);
v[i].push_back(x);
}
}
} void modify(int l, int r) //差分法
{
range[l]++;
range[r+1]--;
} int solve()
{
for(int i = 2; i<=n; i++)
{
int tot = min(v[i-1][0], v[i][0]);
int j = 1;
while(j<=tot && v[i-1][j]==v[i][j]) j++;
if(j>tot)
{
if(v[i][0]<v[i-1][0]) return -1;
else modify(0, c-1);
}
else
{
int x = v[i-1][j];
int y = v[i][j]; if(x<y) modify(0, c-y), modify(c-x+1, c-1);
else modify(c-x+1, c-y);
}
} int cnt = 0;
for(int i = 0; i<c; i++) //线段扫描法
{
cnt += range[i];
if(cnt==n-1) //(1,2)、(2,3)……(n-1,n),所以总数为n-1
return i;
}
return -1;
} int main()
{
init();
cout<< solve() <<endl;
}
以下是自家学校有关差分法的题目:
18100 奇怪的光照植物
该题有题解
时间限制:500MS 内存限制:65535K
提交次数:547 通过次数:64
题型: 编程题 语言: G++;GCC;VC
Description
一种光照植物,用光照射一天就长1厘米。现在将这种植物种在一条直线型的
培养容器中,坐标从0到n-1,每一个整点位置种植一棵这种植物,即共n棵。
开始的时候所有植物的高度都为0。实验过程中,进行了m组,每组选择对范围为
[L,R]的植物进行光照a天,每组参数L、R、a由输入数据给出。
当所有实验结束后,我们想知道所有光照了奇数天的植物总的高度是多少(单位:厘米)。
输入格式
输入第一行包含一个整数T(T<=10),表示有T组测试数据。 对每一组测试数据,第一行是两个数n(1<=n<=10^6)和m(0<=m<=10^5),用空格分隔。
接下来的m行中每一行包含三个数L,R和a(0<=L<=R<=n-1,0<=a<=100),用空格分隔,表示一个操作。
输出格式
对于每组测试数据输出一行,所有实验操作后所有光照了奇数天的植物总的高度
代码如下:
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
int val[1000050];
int main()
{
int T,n,l,r,a,m,sum,ans;
scanf("%d",&T);
while(T--)
{
memset(val,0,sizeof(val));
scanf("%d %d",&n,&m);
for(int i = 0;i<m; i++)
{
scanf("%d %d %d",&l,&r,&a);
val[l] += a;
val[r+1] -= a;
}
sum = 0; ans = 0;
for(int i = 0;i<=n+1;i++)
{
sum += val[i];
if(sum%2==1)
ans += sum;
}
printf("%d\n",ans);
}
return 0;
}
Codeforces Round #376 (Div. 2) D. 80-th Level Archeology —— 差分法 + 线段扫描法的更多相关文章
- Codeforces Round #538 (Div. 2) F 欧拉函数 + 区间修改线段树
https://codeforces.com/contest/1114/problem/F 欧拉函数 + 区间更新线段树 题意 对一个序列(n<=4e5,a[i]<=300)两种操作: 1 ...
- Codeforces Round #376 (Div. 2) C题 Socks(dsu+graphs+greedy)
Socks Problem Description: Arseniy is already grown-up and independent. His mother decided to leave ...
- Codeforces Round #376 (Div. 2)F. Video Cards(前缀和)
题目链接:http://codeforces.com/contest/731/problem/F 题意:有n个数,从里面选出来一个作为第一个,然后剩下的数要满足是这个数的倍数,如果不是,只能减小为他的 ...
- Codeforces Round #376 (Div. 2) C. Socks---并查集+贪心
题目链接:http://codeforces.com/problemset/problem/731/C 题意:有n只袜子,每只都有一个颜色,现在他的妈妈要去出差m天,然后让他每天穿第 L 和第 R 只 ...
- Codeforces Round #376 (Div. 2) F. Video Cards 数学 & 暴力
http://codeforces.com/contest/731/problem/F 注意到一个事实,如果你要找一段区间中(从小到大的),有多少个数是能整除左端点L的,就是[L, R]这样.那么,很 ...
- Codeforces Round #376 (Div. 2) F. Video Cards —— 前缀和 & 后缀和
题目链接:http://codeforces.com/contest/731/problem/F F. Video Cards time limit per test 1 second memory ...
- Codeforces Round #376 (Div. 2) A. Night at the Museum —— 循环轴
题目链接: http://codeforces.com/contest/731/problem/A A. Night at the Museum time limit per test 1 secon ...
- Codeforces Round #376 (Div. 2) C. Socks —— 并查集 + 贪心
题目链接:http://codeforces.com/contest/731/problem/C 题解: 1.看题目时,大概知道,不同的袜子会因为要在同一天穿而差生了关联(或者叫相互制约), 其中一条 ...
- Codeforces Round #376 (Div. 2)
A 模拟 #include <cstdio> #include <cstring> int Ans; ]; inline ?x:-x;} inline int Min(int ...
随机推荐
- 基于sklearn的分类器实战
已迁移到我新博客,阅读体验更佳基于sklearn的分类器实战 完整代码实现见github:click me 一.实验说明 1.1 任务描述 1.2 数据说明 一共有十个数据集,数据集中的数据属性有全部 ...
- 利用例子来理解spring的面向切面编程(使用@Aspect)
上篇的例子,自动装配和自动检测Bean是使用注解的方式处理的,而面向切面编程是使用aop标签处理的,给我感觉就像中西医参合一样. 现在就来优化优化,全部使用注解的方式处理. 1.工程图:
- C# MD5加密(16进制)
MD5加密(16进制) vs会提示引用 using System.Security.Cryptography; 代码如下: public static string MD5Encrypt32(stri ...
- Pixhawk之姿态解算篇(1)_入门篇(DCM Nomalize)
一.开篇 慢慢的.慢慢的.慢慢的就快要到飞控的主要部分了,飞控飞控就是所谓的飞行控制呗,一个是姿态解算一个是姿态控制,解算是解算,控制是控制,各自负责各自的任务.我也不懂.还在学习中~~~~ 近期看姿 ...
- Oracle Apex 有用笔记系列 6 - 可编辑交互报告 Editable Interactive Report
据笔者所知.Apex 4.x 是没有提供可编辑交互报告组件的.这就须要我们手动实现. 事实上这也并非非常复杂,仅仅须要简单几步. 1. 依据向导建立一个interactive report.查询语句能 ...
- 怎样隐藏Windows7 系统保留分区
安装Windows7操作系统时须要预留出100MB左右的系统保留盘分区.在Windows7激活是必须给它分配盘符,否则无法将其成功激活,但是激活后该盘符永久地显示了出来,怎样将其隐藏掉呢? 1.隐藏前 ...
- 【转载】Web Service和WCF的到底有什么区别
[1]Web Service:严格来说是行业标准,也就是Web Service 规范,也称作WS-*规范,既不是框架,也不是技术. 它有一套完成的规范体系标准,而且在持续不断的更新完善中. 它使用XM ...
- Android学习系列(二)布局管理器之线性布局的3种实现方式
转载请注明出处:http://blog.csdn.net/lhy_ycu/article/details/39643669 LinearLayout是Android控件中的线性布局控件,它包括的子控件 ...
- MySQL提示Access denied for user ''@'localhost'”的解决
记得那时由于没有网络,把rootpassword改错了写成了: update user set password="122" where user="root" ...
- 【转】VMware 11.0 简体中文版|附永久密钥
VMware 11.0 简体中文版|附永久密钥 昨天,VMware虚拟机11.0 简体中文版正式发布,值得注意的是新版抛弃了32位系统支持,安装包体积大幅减小, 新增Windows 10 技术预览版支 ...