Manthan, Codefest 19 (open for everyone, rated, Div. 1 + Div. 2)-E. Let Them Slide-思维+数据结构
Manthan, Codefest 19 (open for everyone, rated, Div. 1 + Div. 2)-E. Let Them Slide-思维+数据结构

【Problem Description】
\(n\times w\)的方格中,每一行有\(cnt_i\)个数字,每一行的数字都连续的放在一起,但是可以任意的平移。问每一列的最大和为多少。
【Solution】
最直观的想法是对于每一列\(j\),计算出每一行中的一个区间最大值,此区间长度为\(len=w-cnt+1\)。即能对第\(j\)列产生贡献的区间范围为\([j-len+1,j]\)。区间最大值可以很容易的用单调栈实现,但是\(n,w\le10^6\),而此方法的复杂度为\(O(n\cdot w)\)。所以不可行。
根据题目条件所有数组总长度不超过\(10^6\)。所以可以换个角度,考虑每一行的第\(j\)个数能对哪些列产生贡献。可以发现对于第\(j\)个数来说,能产生贡献的列的范围为\([j,w-cnt+j]\)。所以可以将所有数,按列分类,对于每一行维护一个\(set\),存储所有合法范围内的值,每次转移列时,将不合法的值从\(set\)中移除即可。此时对第\(j\)列的贡献就是\(set\)中的最大值。
【Code】
/*
* @Author: Simon
* @Date: 2019-08-27 13:32:39
* @Last Modified by: Simon
* @Last Modified time: 2019-08-27 13:57:34
*/
#include<bits/stdc++.h>
using namespace std;
typedef int Int;
#define int long long
#define INF 0x3f3f3f3f
#define maxn 1000005
int ans[maxn];
vector<pair<int,int> >add[maxn],remv[maxn];
multiset<int>s[maxn];
Int main(){
#ifndef ONLINE_JUDGE
//freopen("input.in","r",stdin);
//freopen("output.out","w",stdout);
#endif
ios::sync_with_stdio(false);
cin.tie(0);
int n,w;cin>>n>>w;
for(int i=1;i<=n;i++){
int cnt;cin>>cnt;
for(int j=1;j<=cnt;j++){
int x;cin>>x;
add[j].push_back({x,i}); //按列分类,起点
remv[w-cnt+j].push_back({x,i}); //终点
}
if(cnt<w){
add[1].push_back({0,i});
remv[w-cnt].push_back({0,i});
add[cnt+1].push_back({0,i});
remv[w].push_back({0,i});
}
}
for(int j=1;j<=w;j++){
for(auto v:add[j]){
int idx=v.second,val=v.first;
ans[j]-=s[idx].empty()?0:*s[idx].rbegin();
s[idx].insert(val);
ans[j]+=*s[idx].rbegin();
}
if(j<w){
ans[j + 1] = ans[j];
for(auto v:remv[j]){ //remv[j]表示这些数对第j列有贡献,对j+1列就没有贡献了,所以统计第j+1列的最大值时,需要将这些数移除。
int idx=v.second,val=v.first;
ans[j+1]-=*s[idx].rbegin();
s[idx].erase(s[idx].find(val));
ans[j+1]+=s[idx].empty()?0:*s[idx].rbegin();
}
}
}
for(int i=1;i<=w;i++) cout<<ans[i]<<" \n"[i==w];
#ifndef ONLINE_JUDGE
cout<<endl;system("pause");
#endif
return 0;
}
Manthan, Codefest 19 (open for everyone, rated, Div. 1 + Div. 2)-E. Let Them Slide-思维+数据结构的更多相关文章
- Manthan, Codefest 19 (open for everyone, rated, Div. 1 + Div. 2)-D. Restore Permutation-构造+树状数组
Manthan, Codefest 19 (open for everyone, rated, Div. 1 + Div. 2)-D. Restore Permutation-构造+树状数组 [Pro ...
- Manthan, Codefest 19 (open for everyone, rated, Div. 1 + Div. 2)-C. Magic Grid-构造
Manthan, Codefest 19 (open for everyone, rated, Div. 1 + Div. 2)-C. Magic Grid-构造 [Problem Descripti ...
- Manthan, Codefest 19 (open for everyone, rated, Div. 1 + Div. 2) F. Bits And Pieces sosdp
F. Bits And Pieces 题面 You are given an array
- Manthan, Codefest 19 (open for everyone, rated, Div. 1 + Div. 2) G. Polygons 数论
G. Polygons Description You are given two integers
- Manthan, Codefest 19 (open for everyone, rated, Div. 1 + Div. 2) (1208F,1208G,1208H)
1208 F 大意: 给定序列$a$, 求$\text{$a_i$|$a_j$&$a_k$}(i<j<k)$的最大值 枚举$i$, 从高位到低位贪心, 那么问题就转化为给定$x$ ...
- RMQ+差分处理(Let Them Slide)Manthan, Codefest 19 (open for everyone, rated, Div. 1 + Div. 2)
题意:https://codeforc.es/contest/1208/problem/E 现有n行w列的墙,每行有一排连续方块,一排方块可以左右连续滑动,且每个方块都有一个价值,第i 列的价值定义为 ...
- 线段树维护最后一个0的位置(Restore Permutation)Manthan, Codefest 19 (open for everyone, rated, Div. 1 + Div. 2)
题意:https://codeforc.es/contest/1208/problem/D 给你长度为n的序列,s[i]的值为p[1]到p[i-1]中比p[i]小的数的和,让你求出p序列. 思路: 首 ...
- Codeforces Manthan, Codefest 19 (open for everyone, rated, Div. 1 + Div. 2)
传送门 A. XORinacci 手玩三四项发现序列就是 $a,b,a\ xor\ b,a,b,...$,直接输出即可 #include<iostream> #include<cst ...
- Manthan, Codefest 19 (open for everyone, rated, Div. 1 + Div. 2)E(多重集维护)
#define HAVE_STRUCT_TIMESPEC#include<bits/stdc++.h>using namespace std;long long ans[1000007]; ...
随机推荐
- 【Leetcode_easy】840. Magic Squares In Grid
problem 840. Magic Squares In Grid solution: class Solution { public: int numMagicSquaresInside(vect ...
- 机器学习之径向基神经网络(RBF NN)
本文基于台大机器学习技法系列课程进行的笔记总结. 主要内容如下图所示: 首先介绍一下径向基函数网络的Hypothesis和网络的结构,然后介绍径向基神经网络学习算法,以及利用K-means进行的学习, ...
- Linux下安装redis以及常用命令
https://blog.csdn.net/zgf19930504/article/details/51850594 安装: 1.获取redis资源 wget http://download.redi ...
- STAR软件的学习
下载地址与参考文档 https://github.com/alexdobin/STAR/archive/2.5.3a.tar.gz wget https://github.com/alexdobin/ ...
- ROS安装,配置
ROS最好安装在Ubuntu系统,因为ROS目前在其他的系统中都是试验性的 ! <Learning ROS for Robotics Programming-- second Edition&g ...
- [ClickOnce] - Win10 管理员模式下无法安装 ClickOnce 之解决
Issue Windows 10 管理员模式下,点击 ClickOnce 安装程序无反应. 解决 1. 按 WIN+R 键打开“运行”窗口,输入 “gpedit.msc" 打开组策略.2. ...
- 虚拟机VMware中安装Ubuntu18.04
准备工作 Ubuntu 获取地址: 官网 清华镜像站 VMware 获取地址链接 安装过程 Vmware的安装过程此处不在赘述,不清楚如何安装的请自行百度,参见VMware14安装教程 然后就是Vmw ...
- go语言实现单链表
线性表包含两种存储方法:顺序存储结构和链式存储结构,其中顺序表的缺点是不便插入与删除数据. 单链表:每个结点包含两部分:数据域+指针域,上一个结点的指针指向下一结点,依次相连,形成链表.特别注意的是每 ...
- 使用Dockerfile构建镜像并push到私有仓库
环境:OS X 10.10.5 maven 3.3.9 Docker version 1.12.2 docker-machine version 0.8.2 程序示例为http://www.cnblo ...
- python练习:面向对象1
面向对象习题: 一:定义一个学生类.有下面的类属性: 1 姓名 2 年龄 3 成绩(语文,数学,英语)[每课成绩的类型为整数] 类方法: 1 获取学生的姓名:get_name() 返回类型:str 2 ...