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-思维+数据结构的更多相关文章

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

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

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

  4. Manthan, Codefest 19 (open for everyone, rated, Div. 1 + Div. 2) G. Polygons 数论

    G. Polygons Description You are given two integers

  5. 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$ ...

  6. 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 列的价值定义为 ...

  7. 线段树维护最后一个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序列. 思路: 首 ...

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

  9. 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]; ...

随机推荐

  1. CSS 常用效果--持续更新

    单行超出省略: white-space: nowrap; text-overflow:ellipsis; overflow:hidden; 多行超出省略: text-overflow: -o-elli ...

  2. 使用jmeter HTTP代理服务器录制APP脚本

    使用jmeter HTTP代理服务器录制APP脚本 步骤一.jemter设置 1.启动JMeter,双击运行jmeter.bat,启动jmeter jmeter运行主界面 2.添加线程组:右键测试计划 ...

  3. PHP实现单点登录最简单的方法

    PHP实现单点登录最简单的方法 用户在A登录 存入登录状态 登录B站(A的识别要传入B) 获取A的登录状态

  4. Qt qml的软件架构设计

    google: qt qml application architecture 有很多资源. 1 https://www.ics.com/blog/multilayered-architecture- ...

  5. 数据库的ACID和CAP

    传统数据库的ACID分别是:         A(Atomicty)原子性         B(Consistency)一致性         I(Isolation)独立性         D(Du ...

  6. SpringBoot中service注入失败(A component required a bean of type 'XXService' that could not found)

    先写了JUnit,发现启动不了,注释掉有问题的service也不可以.可能是因为spring开始时会加载所有service吧. 按照网友们的说法,一般需要检查: 1.入口类有没有写MapperScan ...

  7. Python yield 使用浅析【转】

    Python yield 使用浅析 IBM developerWorks 中国 : Open source IBM 开源 - IBM Developer 中国 (原 developerWorks 中国 ...

  8. fatfs系统的移植

    integer.h   FATFS的数据类型定义(一般不需要更改,其他的文件都需要引用这个文件的内容) ffcon.h FATFS的配置文件,配置项的各个参数都需要在这里修改 一个细致的讲解fatfs ...

  9. 记录一次kafka解决相同userId顺序消费的问题

    基本思路:在kafka生产者生产消息时,把相同userId的消息落在同一个分区/partition public void sendTopic1(String tpoic, String userId ...

  10. 【SQL Server高可用性】数据库复制:SQL Server 2008R2中通过数据库复制,把A表的数据复制到B表

    原文:[SQL Server高可用性]数据库复制:SQL Server 2008R2中通过数据库复制,把A表的数据复制到B表 经常在论坛中看到有人问数据同步的技术,如果只是同步少量的表,那么可以考虑使 ...