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]; ...
随机推荐
- List的 Select()使用方法 Demo
List的 Select()使用方法 用List存储对象,代码如下: IList<Student> studentList = new List<Student>(); ;i& ...
- beego框架(golang)学习验证码
beego框架(golang)学习验证码 登录页面使用验证码 路由设置 /beego_admin_template/routers/router.go get请求页面, post验证用户名密码和验证码 ...
- 由于我最近搞了个wordpress搭建博客,这里我为大家分享一哈,使用wordpress过程中遇到的坑
Windows server下搭建mysql+php+apache环境参考教程: https://blog.csdn.net/qq_38125058/article/details/81157865 ...
- css 修改placeholder样式
input::-webkit-input-placeholder{ color:red; } input::-moz-placeholder{ /* Mozilla Firefox 19+ */ co ...
- laravel 运用.env部署多环境配置
切换到laravel 入口文件bootstrap\app.php 在 return $app; 之上添加: $environmentPath = $app->environmentPath( ...
- [DFS]排队(间隔排列)-C++
Description 小Q是班长.在校运动会上,小Q班要进行队列表演.小Q要选出2*N名同学编队,每人都被编上一个号,每一个从1到N的自然数都被某2名同学佩戴,现在要求将他们排成一列,使两个编号为1 ...
- miniconda3 安装tensorflow
使用miniconda3进行安装 conda create -n tensorflow conda install tensorflow 输入下面的代码进行测试 import tensorflow a ...
- JSP的部分知识(一)
通过Servlet进行整个网站的开发是可以的. 不过在Servlet中输出html代码,特别是稍微复杂一点的html代码,就会给人一种很酸爽的感觉. 如果能够直接使用Html代码,然后在html中写j ...
- 题解 CF1216C 【White Sheet】
虽然也很水,但这道还是比前两道难多了... 题目大意:给你三个位于同一平面直角坐标系的矩形,询问你后两个是否完全覆盖了前一个 首先,最直观的想法应该是,把第一个矩形内部每个整数点检查一下,看看是否位于 ...
- Python调用API接口的几种方式
Python调用API接口的几种方式 相信做过自动化运维的同学都用过API接口来完成某些动作.API是一套成熟系统所必需的接口,可以被其他系统或脚本来调用,这也是自动化运维的必修课. 本文主要介绍py ...