codeforces--376D--376F(前缀和优化)
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard 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.
InputThe 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.
OutputIf 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.
Examplesinput4 3
2 3 2
1 1
3 2 3 1
4 2 3 1 2output1input2 5
2 4 2
2 4 2output0input4 4
1 2
1 3
1 4
1 2output-1NoteWord 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 3In 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 test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard 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.
InputThe 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.
OutputThe only line of the output should contain one integer value — the maximum possible total power of video cards working together.
Examplesinput4
3 2 15 9output27input4
8 2 2 7output18NoteIn 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(前缀和优化)的更多相关文章
- Student's Camp CodeForces - 708E (dp,前缀和优化)
大意: $n$行$m$列砖, 白天左侧边界每块砖有$p$概率被摧毁, 晚上右侧边界有$p$概率被摧毁, 求最后上下边界连通的概率. 记${dp}_{i,l,r}$为遍历到第$t$行时, 第$t$行砖块 ...
- Codeforces 479E. Riding in a Lift (dp + 前缀和优化)
题目链接:http://codeforces.com/contest/479/problem/E 题意: 给定一个启示的楼层a,有一个不能去的楼层b,对于你可以去的下一个楼层必须满足你 ...
- Codeforces 712 D. Memory and Scores (DP+滚动数组+前缀和优化)
题目链接:http://codeforces.com/contest/712/problem/D A初始有一个分数a,B初始有一个分数b,有t轮比赛,每次比赛都可以取[-k, k]之间的数,问你最后A ...
- Codeforces 1132C - Painting the Fence - [前缀和优化]
题目链接:https://codeforces.com/contest/1132/problem/C 题意: 栅栏有 $n$ 个节,有 $q$ 个人可以雇佣来涂栅栏,第 $i$ 个人可以涂第 $l_i ...
- P5241 序列(滚动数组+前缀和优化dp)
P5241 序列 挺神仙的一题 看看除了dp好像没什么其他办法了 想着怎么构个具体的图出来,然鹅不太现实. 于是我们想办法用几个参数来表示dp数组 加了几条边肯定要的吧,于是加个参数$i$表示已加了$ ...
- HDU 2829 区间DP & 前缀和优化 & 四边形不等式优化
HDU 2829 区间DP & 前缀和优化 & 四边形不等式优化 n个节点n-1条线性边,炸掉M条边也就是分为m+1个区间 问你各个区间的总策略值最少的炸法 就题目本身而言,中规中矩的 ...
- CDOJ 1307 ABCDE 前缀和优化dp
ABCDE 题目连接: http://acm.uestc.edu.cn/#/problem/show/1307 Description Binary-coded decimal (BCD) is a ...
- bzoj 1044 [HAOI2008]木棍分割——前缀和优化dp
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1044 前缀和优化. 但开成long long会T.(仔细一看不用开long long) #i ...
- 算法技巧讲解》关于对于递推形DP的前缀和优化
这是在2016在长沙集训的第三天,一位学长讲解了“前缀和优化”这一技巧,并且他这一方法用的很6,个人觉得很有学习的必要. 这一技巧能使线性递推形DP的速度有着飞跃性的提升,从O(N2)优化到O(N)也 ...
- bzoj 3398 [Usaco2009 Feb]Bullcow 牡牛和牝牛——前缀和优化dp / 排列组合
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=3398 好简单呀.而且是自己想出来的. dp[ i ]表示最后一个牡牛在 i 的方案数. 当前 ...
随机推荐
- MSXML insertBefore(IXMLDOMNode *newChild, VARIANT refChild) 传参
在xml操作中经常会用到在某一个节点后或前面插入一个节点,MSXML DOM 中使用的函数是insertBefore(IXMLDOMNode *newChild, VARIANT refChild): ...
- MySQL 的一个Bug?
在 my.ini 中的 [mysqld] 段设置 mysql basedir 和 datadir,如下: #Path to installation directory. All paths are ...
- iOS之文件解析
JSON JSON – OC 转换对照表 JSON OC {} NSDictonary [] NSArray "" NSString 数字 10,10.5 NSNumber NSJ ...
- 洛谷-烤鸡-BOSS战-入门综合练习1
题目背景 Background 猪猪hanke得到了一只鸡 题目描述 Description 猪猪Hanke特别喜欢吃烤鸡(本是同畜牲,相煎何太急!)Hanke吃鸡很特别,为什么特别呢?因为他有10 ...
- javascript动画效果之缓冲动画
这里的html和css的代码是复制之前的随便匀速运动的,所以不需要改变 <!DOCTYPE html> <html> <head> <meta charset ...
- 证明:一个整数a若不能被6整除,则a2+24必能被24整除。(整除理论,1.1.4)
证明:一个整数a若不能被6整除,则a2+24必能被24整除. 证明: 因为,a不能被6整除 所以,a不可以同时被2和3整除 所以,a一定是一个奇数, 所以,令a=2k+1,k是整数: 又因为,a2+2 ...
- 5.编写Java应用程序。首先,定义描述学生的类——Student,包括学号(int)、 姓名(String)、年龄(int)等属性;二个方法:Student(int stuNo,String name,int age) 用于对对象的初始化,outPut()用于输出学生信息。其次,再定义一个主类—— TestClass,在主类的main方法中创建多个Student类的对象,使用这些对象来测 试St
Student类: package com.bao; public class Student { int stuNo;String name,sex;int age,weight; Student( ...
- CSS问题:怎么样让鼠标经过按钮的时候发生的状态一直停留在当页呢?
$('p').mouseenter(function(){ $('p').css('background-color','yellow'); }); 只写一个mouseenter的动态效果的话是不能达 ...
- iOS拨打电话
1,这种方法,拨打完电话回不到原来的应用,会停留在通讯录里,而且是直接拨打,不弹出提示NSMutableString * str=[[NSMutableString alloc] initWithFo ...
- iosOC不可变数组遍历
NSArray * array = @[@"1",@"2",@"3"]; NSLog(@"%@",array); //1 ...