time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

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.

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 thei-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.

这道题我一开始想的很简单,不就是比较字符串大小吗,但是题目中的“字符串”保存的不是char,而是数字(最大为106),直接运用系统自带的字符串比较运算符可能会出现问题(写的第一发就是这种思路,没过,明天在完善完善试试这种写法可不可以)。

另一种思路是分析数字变化的规律。由于题目求的是按照字典序排列,需从两个字符串的0位置开始,依次向下比较,直至某一位的字符可以分出前后。

如果没有题目中c(数字的种类数,即字符串中每个数字的定义域为1...c, c+1即为1)的限制,无论怎样round这些字符串的字典序都不会改变(所有数字同时偏移同样的量,原先的差值不会改变)。

但因为有了上述的限制条件,会使字典序改变成为可能,举个例子:

“字符串”a与b,0位的数字分别为x,y且x>y(a的字典序大于b), 定义域为c, 要使x<y(a的字典序小于b),需要将他们偏移 c-x+1 至 c-y 个单位(偏移的单位数不会超过c,否则又会回到原始状态)

由此,问题就转化为了求多个区间交的操作

可以利用前缀和方便快速的完成

下面是代码

#include<bits/stdc++.h>
#define maxn 500010
#define maxc 1000010
using namespace std; int n,c;
vector<int>vec[maxn];
int qj[maxc]; void get()
{
scanf("%d %d",&n,&c);
int temp;
for(int i=; i<=n; i++) {
scanf("%d",&temp);
vec[i].push_back(temp);
for(int j=; j<=vec[i][]; j++) {
scanf("%d",&temp);
vec[i].push_back(temp);
}
}
} void add(int l,int r)
{
qj[l]++;
qj[r+]--;//[a,b]++可化为cnt[a]++与cnt[b]--,处理前缀和
} int pre()
{
for(int i=; i<n; i++) {
bool ok=false;
int MIN=min(vec[i][],vec[i+][]);
for(int j=; j<=MIN; j++) {
int x=vec[i][j];
int y=vec[i+][j];
if(x!=y) {
if(x<y) add(c-y+,c-x);
else if(x>y) {
add(,c-x);
add(c-y+,c-);
}
ok=true;
break;
}
}
if(!ok&&vec[i][]>vec[i+][]) return -;
}
int temp=;
for(int i=; i<c; i++) {
temp+=qj[i];
if(!temp) return i;
}
return -;
} int main()
{
get();
printf("%d\n",pre());
return ;
}

巧的是,同一场的F题也可以利用前缀和来进行优化,感觉很神奇啊

time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

Little Vlad is fond of popular computer game Bota-2. Recently, the developers announced the new add-on named Bota-3. Of course, Vlad immediately bought only to find out his computer is too old for the new game and needs to be updated.

There are n video cards in the shop, the power of the i-th video card is equal to integer value ai. As Vlad wants to be sure the new game will work he wants to buy not one, but several video cards and unite their powers using the cutting-edge technology. To use this technology one of the cards is chosen as the leading one and other video cards are attached to it as secondary. For this new technology to work it's required that the power of each of the secondary video cards is divisible by the power of the leading video card. In order to achieve that the power of any secondary video card can be reduced to any integer value less or equal than the current power. However, the power of the leading video card should remain unchanged, i.e. it can't be reduced.

Vlad has an infinite amount of money so he can buy any set of video cards. Help him determine which video cards he should buy such that after picking the leading video card and may be reducing some powers of others to make them work together he will get the maximum total value of video power.

Input

The first line of the input contains a single integer n (1 ≤ n ≤ 200 000) — the number of video cards in the shop.

The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 200 000) — powers of video cards.

Output

The only line of the output should contain one integer value — the maximum possible total power of video cards working together.

Examples
input
4
3 2 15 9
output
27
input
4
8 2 2 7
output
18
Note

In the first sample, it would be optimal to buy video cards with powers 3, 15 and 9. The video card with power 3 should be chosen as the leading one and all other video cards will be compatible with it. Thus, the total power would be 3 + 15 + 9 = 27. If he buys all the video cards and pick the one with the power 2 as the leading, the powers of all other video cards should be reduced by 1, thus the total power would be 2 + 2 + 14 + 8 = 26, that is less than 27. Please note, that it's not allowed to reduce the power of the leading video card, i.e. one can't get the total power 3 + 1 + 15 + 9 = 28.

In the second sample, the optimal answer is to buy all video cards and pick the one with the power 2 as the leading. The video card with the power 7 needs it power to be reduced down to 6. The total power would be 8 + 2 + 2 + 6 = 18.

可以借鉴筛素数的思想,数组里存储的是不小于此下标的数的个数,求能成为某个数倍数的数的个数,可以分段转化为如下的问题:

设当前下标为n,[ x*n , (x+1)*n ) 区间中符合要求的个数,即可以转化x*n的数的个数

然后遍历base,找出最优解即可

代码如下(代码来自网络,星期六自己敲一版,加深记忆):

#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <algorithm>
#include <vector>
#define ll long long
using namespace std;
const int N=2e5+;
int a[N],pre[N*];
int main()
{
int n;
memset(pre,,sizeof(pre));
scanf("%d",&n);
for(int i=;i<=n;i++)
{
scanf("%d",&a[i]);
pre[a[i]]++;
}
for(int i=;i<=N*;i++)
{
pre[i]+=pre[i-];
}
sort(a+,a+n+);
int m=unique(a+,a+n+)-a-;
ll ans=;
for(int i=;i<=m;i++)
{
ll sum=;
for(int j=;(j-)*a[i]<=a[m];j++)
{
int p=(j-)*a[i];
int q=j*a[i];
sum+=(ll)(pre[q-]-pre[p-])*a[i]*(j-);
}
ans=max(ans,sum);
}
printf("%I64d\n",ans);
}

前缀和挺好玩的,总结总结我会再写一个博客

codeforces--376D--376F(前缀和优化)的更多相关文章

  1. Student's Camp CodeForces - 708E (dp,前缀和优化)

    大意: $n$行$m$列砖, 白天左侧边界每块砖有$p$概率被摧毁, 晚上右侧边界有$p$概率被摧毁, 求最后上下边界连通的概率. 记${dp}_{i,l,r}$为遍历到第$t$行时, 第$t$行砖块 ...

  2. Codeforces 479E. Riding in a Lift (dp + 前缀和优化)

    题目链接:http://codeforces.com/contest/479/problem/E 题意:         给定一个启示的楼层a,有一个不能去的楼层b,对于你可以去的下一个楼层必须满足你 ...

  3. Codeforces 712 D. Memory and Scores (DP+滚动数组+前缀和优化)

    题目链接:http://codeforces.com/contest/712/problem/D A初始有一个分数a,B初始有一个分数b,有t轮比赛,每次比赛都可以取[-k, k]之间的数,问你最后A ...

  4. Codeforces 1132C - Painting the Fence - [前缀和优化]

    题目链接:https://codeforces.com/contest/1132/problem/C 题意: 栅栏有 $n$ 个节,有 $q$ 个人可以雇佣来涂栅栏,第 $i$ 个人可以涂第 $l_i ...

  5. P5241 序列(滚动数组+前缀和优化dp)

    P5241 序列 挺神仙的一题 看看除了dp好像没什么其他办法了 想着怎么构个具体的图出来,然鹅不太现实. 于是我们想办法用几个参数来表示dp数组 加了几条边肯定要的吧,于是加个参数$i$表示已加了$ ...

  6. HDU 2829 区间DP & 前缀和优化 & 四边形不等式优化

    HDU 2829 区间DP & 前缀和优化 & 四边形不等式优化 n个节点n-1条线性边,炸掉M条边也就是分为m+1个区间 问你各个区间的总策略值最少的炸法 就题目本身而言,中规中矩的 ...

  7. CDOJ 1307 ABCDE 前缀和优化dp

    ABCDE 题目连接: http://acm.uestc.edu.cn/#/problem/show/1307 Description Binary-coded decimal (BCD) is a ...

  8. bzoj 1044 [HAOI2008]木棍分割——前缀和优化dp

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1044 前缀和优化. 但开成long long会T.(仔细一看不用开long long) #i ...

  9. 算法技巧讲解》关于对于递推形DP的前缀和优化

    这是在2016在长沙集训的第三天,一位学长讲解了“前缀和优化”这一技巧,并且他这一方法用的很6,个人觉得很有学习的必要. 这一技巧能使线性递推形DP的速度有着飞跃性的提升,从O(N2)优化到O(N)也 ...

  10. bzoj 3398 [Usaco2009 Feb]Bullcow 牡牛和牝牛——前缀和优化dp / 排列组合

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=3398 好简单呀.而且是自己想出来的. dp[ i ]表示最后一个牡牛在 i 的方案数. 当前 ...

随机推荐

  1. keepalived问题

    Sep 30 11:41:34 XSXSH-LB2 Keepalived[2735]: Starting Keepalived v1.2.12 (04/08,2014) Sep 30 11:41:34 ...

  2. 利用pscp命令实现linux与windows文件互传

    windows==>linux(单个文件) PrivateKey.ppk(私钥)可以是相对路径或者绝对路径pscp -i D:\PrivateKey.ppk D:\xxx.xx root@123 ...

  3. android中对Bitmap图片设置任意角为圆角

    http://blog.csdn.net/l448288137/article/details/48276681 最近项目开发中使用到了圆角图片,网上找到的圆角图片控件大多比较死板,只可以全圆角.其中 ...

  4. nginx安装与应用

    一.nginx的安装与启动: 1.安装依赖库.nginx的一些模块需要依赖其他第三方库,通常有pcre库(perl compatible regular expression,perl兼容正则表达式, ...

  5. hadoop pig入门总结

    在这里贴一个pig源码的分析,做pig很长时间没做笔记,不包含任何细节,以后有机会再说吧 http://blackproof.iteye.com/blog/1769219 hadoop pig入门总结 ...

  6. getScrollX()理解

  7. ios隐藏键盘的方式简单应用

    iOS开发中经常要用到输入框,默认情况下点击输入框就会弹出键盘,但是必须要实现输入框return的委托方法才能取消键盘的显示,对于用户体验来说很不友好,我们可以实现点击键盘以外的空白区域来将键盘隐藏, ...

  8. LINUX修改IP地址

    以前都是使用自动IP动态分配获取IP的,虽然每次获得的ip都是相同的,但我还是决定自己设置一个IP.输入命令:[root@localhost ~]# ifconfig eth0 219.246.177 ...

  9. 常用的linux系统监控命令整理

    找到最耗CPU的java线程ps命令 命令:ps -mp pid -o THREAD,tid,time 或者 ps -Lfp pid 结果展示: 这个命令的作用,主要是可以获取到对应一个进程下的线程的 ...

  10. Linux中seq命令的用法

    用于产生从某个数到另外一个数之间的所有整数 例一: # seq 1 10 结果是1 2 3 4 5 6 7 8 9 10 例二: #!/bin/bash for i in `seq 1 10`; do ...