当前位置:网站首页>Codeworks round 783 (Div. 2) d problem solution
Codeworks round 783 (Div. 2) d problem solution
2022-04-23 19:16:00 【ZZXzzx0_ 0】
D. Optimal Partition
The question :
Here you are. n n n Array of Numbers , You can divide this array into Several consecutive subarrays , Can't be empty .
hypothesis s [ i ] s[i] s[i] by a a a Array of Prefixes and arrays
that Continuous subarray a l a_l al a l + 1 a_{l+1} al+1 … a r a_r ar Of value by
- r − l + 1 r - l + 1 r−l+1 Premise yes : s [ r ] − s [ l − 1 ] > 0 s[r] - s[l-1] > 0 s[r]−s[l−1]>0
- 0 0 0 Premise yes : s [ r ] − s [ l − 1 ] = 0 s[r] - s[l-1] = 0 s[r]−s[l−1]=0
- − ( r − l + 1 ) -(r - l +1) −(r−l+1) Premise yes : s [ r ] − s [ l − 1 ] < 0 s[r] - s[l-1] < 0 s[r]−s[l−1]<0
Find the of several continuous subarrays The maximum value of the sum of values
Ideas :
hypothesis f [ i ] f[i] f[i] Express Only the front of the array is considered i The sum of the maximum value of the number .
From the meaning of the title have to
- f [ i ] = f [ j ] + ( i − j ) , [ s [ i ] − s [ j ] > 0 , j < i ] f[i] = f[j] + (i - j) ,[s[i]-s[j]>0,j<i] f[i]=f[j]+(i−j),[s[i]−s[j]>0,j<i]
- f [ i ] = f [ j ] , [ s [ i ] − s [ j ] = 0 , j < i ] f[i] = f[j],[s[i]-s[j]=0,j<i] f[i]=f[j],[s[i]−s[j]=0,j<i]
- f [ i ] = f [ j ] − ( i − j ) , [ s [ i ] − s [ j ] < 0 , j < i ] f[i] = f[j] - (i - j) ,[s[i]-s[j]<0,j<i] f[i]=f[j]−(i−j),[s[i]−s[j]<0,j<i]
Time complexity O n 2 On^2 On2
Consider optimizing , The above three formulas are further Simplification by
- f [ i ] − i = f [ j ] − j , [ s [ i ] > s [ j ] , j < i ] f[i] -i = f[j] - j ,[s[i]>s[j],j<i] f[i]−i=f[j]−j,[s[i]>s[j],j<i]
- f [ i ] = f [ j ] , [ s [ i ] = s [ j ] , j < i ] f[i] = f[j],[s[i]=s[j],j<i] f[i]=f[j],[s[i]=s[j],j<i]
- f [ i ] + i = f [ j ] + j , [ s [ i ] < s [ j ] , j < i ] f[i] + i = f[j] + j ,[s[i]<s[j],j<i] f[i]+i=f[j]+j,[s[i]<s[j],j<i]
consider The first formula ,
f [ i ] − i = f [ j ] − j , [ s [ i ] > s [ j ] , j < i ] f[i] -i = f[j] - j ,[s[i]>s[j],j<i] f[i]−i=f[j]−j,[s[i]>s[j],j<i]
Equivalent to
f [ i ] = m a x ( f [ j ] − j ) + i , [ s [ j ] < s [ i ] , j < i ] f[i] = max(f[j] - j)+i,[s[j]<s[i],j<i] f[i]=max(f[j]−j)+i,[s[j]<s[i],j<i]
We can take all of them s [ i ] s[i] s[i] As the subscript of the array ,
Equivalent to For each i i i, Query all Less than s [ i ] s[i] s[i] This Subscript f [ j ] − j f[j]-j f[j]−j The maximum of
consider s [ i ] s[i] s[i] Too much scope , Cannot be used as an array subscript , We can Will all s [ i ] s[i] s[i] discretization
Then query all Less than s [ i ] s[i] s[i] This Subscript f [ j ] − j f[j]-j f[j]−j The maximum of To update f [ i ] f[i] f[i]
Then on this line segment tree Update subscript to s [ i ] s[i] s[i] The value of is f [ i ] − i f[i] - i f[i]−i
The second and third expressions are the same
We can Build three segment trees ,
The maximum value of the query interval , Single point modification , that will do
Time complexity : O n l o g n Onlogn Onlogn
#include <bits/stdc++.h>
#define fer(i,a,b) for(re i = a ; i <= b ; ++ i)
#define der(i,a,b) for(re i = a ; i >= b ; -- i)
#define cf int _; cin>> _; while(_--)
#define all(x) (x).begin(),(x).end()
#define sf(x) scanf("%lld",&x)
#define re register int
#define lb lower_bound
#define int long long
#define pb push_back
using namespace std;
const int N = 5e5 + 10 , M = 1e9 , mod = 1e9 + 7 ; // mod = 998244353 ;
const double eps = 1e-7 , pi = acos(-1.0) ;
int n ;
int a[N] , s[N] ;
int f[N] ;
vector<int> q ;
struct Node
{
int l, r;
int v ; // Section [l, r] Maximum of
}tr[3][N * 4];
int d ;
int get(int x)
{
return lb(all(q) , x) - q.begin() + 1 ;
}
void pushup(int u) // Information from child nodes , To calculate the information of the parent node
{
tr[d][u].v = max(tr[d][u<<1].v,tr[d][u<<1|1].v);
}
void build(int u, int l, int r)
{
if(l == r)
{
tr[d][u] = {
l,r,-M};
}
else
{
tr[d][u] = {
l,r};
int mid = r + l >> 1 ;
build(u << 1 , l , mid );
build(u << 1 | 1 , mid + 1 , r);
pushup(u) ;
}
}
int query(int u, int l, int r)
{
if(tr[d][u].l >= l && tr[d][u].r <= r)
return tr[d][u].v ;
int v = -1e9 , minv = 1e9 ;
int mid = tr[d][u].l + tr[d][u].r >> 1 ;
if(l <= mid)
{
v = max(v,query(u << 1 , l ,r)) ;
}
if(r > mid)
{
v = max(v,query(u << 1 | 1 , l , r)) ;
}
return v ;
}
void modify(int u, int x, int v)
{
if(tr[d][u].l == x && tr[d][u].r == x) tr[d][u].v = max(v , tr[d][u].v) ;
else
{
int mid = tr[d][u].l + tr[d][u].r >> 1 ;
if(x <= mid) modify(u << 1 , x , v);
else modify(u << 1 | 1 , x ,v);
pushup(u);
}
}
signed main()
{
cf
{
cin >> n ;
fer(i,1,n) sf(a[i]) , s[i] = s[i - 1] + a[i] ;
fer(i,0,n) f[i] = -1e9 ;
f[0] = 0 ;
q.clear() ;
fer(i,0,n) q.pb(s[i]) ;
sort(all(q)) ;
q.erase( unique(all(q)) , q.end() ) ;
// d=0/1/2 Represent three segment trees respectively
fer(i,0,2)
{
d = i ;
build(1 , 1 , n + 1) ;
}
fer(i,0,2)
{
d = i ;
modify(1 , get(s[0]) , 0) ;
}
fer(i,1,n)
{
int w = get(s[i]) ;
d = 0 ;
f[i] = query(1 , 1 , w - 1) + i ;
d = 1 ;
f[i] = max(f[i] , query( 1 , w , w ) ) ;
d = 2 ;
f[i] = max(f[i] , query( 1 , w + 1 , n + 1) - i ) ;
d = 0 ;
modify(1 , w , f[i] - i) ;
d = 1 ;
modify(1 , w , f[i]) ;
d = 2 ;
modify(1 , w , f[i] + i) ;
}
cout << f[n] << "\n" ;
}
return 0 ;
}
版权声明
本文为[ZZXzzx0_ 0]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204231912032594.html
边栏推荐
- Esp01s with Arduino development environment
- Gossip: on greed
- Solve the problem of invalid listview Click
- Pit encountered using camera x_ When onpause, the camera is not released, resulting in a black screen when it comes back
- OpenHarmony开源开发者成长计划,寻找改变世界的开源新生力!
- Codeforces Round #783 (Div. 2) D题解
- Oracle configuration st_ geometry
- Raspberry pie uses root operation, and the graphical interface uses its own file manager
- 电路在线模拟
- Sword finger offer II 116 Number of provinces - spatial complexity O (n), time complexity O (n)
猜你喜欢

Simplified path (force buckle 71)

Audio signal processing and coding - 2.5.3 the discrete cosine transform
![[记录]TypeError: this.getOptions is not a function](/img/c9/0d92891b6beec3d6085bd3da516f00.png)
[记录]TypeError: this.getOptions is not a function

Raspberry pie 18b20 temperature

浅谈c语言指针的强制转换

Switching power supply design sharing and power supply design skills diagram

RuntimeError: Providing a bool or integral fill value without setting the optional `dtype` or `out`

12个例子夯实promise基础

White screen processing method of fulter startup page

Android Development: the client obtains the latest value in the database in real time and displays it on the interface
随机推荐
SSDB Foundation
C: generic reflection
The difference between ordinary inner class and static inner class
FTP, SSH Remote Access and control
Modify the font size of hint in editext
OpenHarmony开源开发者成长计划,寻找改变世界的开源新生力!
openlayers 5.0 两种居中方式
C1000k TCP connection upper limit test
Wechat applet part of the mobile phone Preview PDF did not respond
openlayers 5.0 离散聚合点
12个例子夯实promise基础
[report] Microsoft: application of deep learning methods in speech enhancement
c#:泛型反射
openlayers 5.0 热力图
Esp01s with Arduino development environment
2022.04.23 (the best time for lc_714_to buy and sell stocks, including handling charges)
The type initializer for ‘Gdip‘ threw an exception
Tencent cloud GPU best practices - remote development training using jupyter pycharm
openlayers 5.0 当地图容器大小改变时,重新加载地图
Using oes texture + glsurfaceview + JNI to realize player picture processing based on OpenGL es