当前位置:网站首页>20200810 T2 dispatch money
20200810 T2 dispatch money
2022-06-11 07:30:00 【Master. Yi】
Title Description
Give a length of n n n Permutation a i a_i ai, Divide it into several paragraphs , Each segment is divided into X X X The price of , After the division, the segment needs to be sorted , The cost is the number of inverse pairs of intervals . Find the minimum total cost .
n ≤ 300000 , 1 ≤ X ≤ 1 0 9 n\le 300000,1\le X\le 10^9 n≤300000,1≤X≤109
time limit 5s.
Topic analysis
violence O ( n 2 ) O(n^2) O(n2) DP: f i = f j + i n v e r s i o n _ p a i r ( j + 1 , i ) f_i=f_j+inversion\_pair(j+1,i) fi=fj+inversion_pair(j+1,i)
Experienced OI Players can immediately guess the monotony of the decision , The proof is simple , The larger the interval, the faster the cost function grows , When in i i i At the decision point k k k Than the decision point j j j Bad time ( k < j k<j k<j), stay i ′ > i i'>i i′>i It must be inferior to the latter .
However, the binary maintenance stack requires online calculation of the number of interval reverse pairs , This is a n n n\sqrt n nn The problem of , You need to take a ride log \log log,300000 Obviously I can't run .
The divide and conquer method can be similar to that of Mo team , Use a tree array to maintain how many numbers are less than x x x, Add... When the decision point moves / Delete the front end of the section / Back end point , According to the process of divide and conquer, we can see that the number of times such an interval moves is O ( n log n ) O(n\log n) O(nlogn) Grade .
But divide and conquer should be based on the interval of decision points f f f All known , So we need to put another one on the outer layer cdq Divide and conquer .
The sum O ( n log 3 n ) O(n\log^3n) O(nlog3n), But the constant is very small .
If you want to write strictly , It is a little troublesome to undo each time , After the actual test, it is not as fast as directly using the writing method similar to Mo's team , That is, if the current interval is not consistent with the query interval, move , Very easy to write .
Code:
#include<bits/stdc++.h>
#define maxn 300005
#define LL long long
using namespace std;
const LL inf = 0x3f3f3f3f3f3f3f3fll;
int n,X,a[maxn];
LL f[maxn];
int arr[maxn];
void upd(int i,int v){
for(;i<=n;i+=i&-i) arr[i]+=v;}
int qsum(int i){
int s=0;for(;i;i-=i&-i) s+=arr[i];return s;}
LL ask(int x,int y){
static int l=1,r=0; static LL inver=0;
while(l>x) l--,inver+=qsum(a[l]),upd(a[l],1);
while(r<y) r++,inver+=r-l-qsum(a[r]),upd(a[r],1);
while(l<x) upd(a[l],-1),inver-=qsum(a[l]),l++;
while(r>y) upd(a[r],-1),inver-=r-l-qsum(a[r]),r--;
return inver;
}
void solve2(int l,int r,int ql,int qr){
if(ql>qr) return;
int mid=(ql+qr)>>1,p=-1; LL mn=inf,tmp;
for(int i=l;i<=r&&i<mid;i++)
if((tmp=f[i]+ask(i+1,mid))<mn) mn=tmp,p=i;
f[mid]=min(f[mid],mn+X);
solve2(l,p,ql,mid-1),solve2(p,r,mid+1,qr);
}
void solve1(int l,int r){
if(l==r) return;
int mid=(l+r)>>1;
solve1(l,mid);
solve2(l,mid,mid+1,r);
solve1(mid+1,r);
}
int main()
{
freopen("dispatch.in","r",stdin);
freopen("dispatch.out","w",stdout);
scanf("%d%d",&n,&X);
for(int i=1;i<=n;i++) scanf("%d",&a[i]),f[i]=inf;
solve1(0,n);
printf("%lld\n",f[n]);
}
If you want to get stuck, you can try to upd The function is written as ++ and --, And in the cycle of finding the decision point, judge whether to follow the cycle or reverse the cycle according to the endpoint position .
There are also optimizations in solve1 The interval is smaller than the block size S S S Sometimes violent , Length of pretreatment interval ≤ S \le S ≤S The reverse logarithm of , This one can O ( n S ) O(nS) O(nS) Preprocessing , w i , j = w i , j − 1 + w i + 1 , j − w i + 1 , j − 1 + [ a i > a j ] w_{i,j}=w_{i,j-1}+w_{i+1,j}-w_{i+1,j-1}+[a_i>a_j] wi,j=wi,j−1+wi+1,j−wi+1,j−1+[ai>aj]
Expand
O ( n n ) O(n\sqrt n) O(nn) Find the number of inverse pairs of intervals .
Offline approach :
Mo team , Consider moving endpoints without using a tree array , Instead, change the query range to be less than x x x The number of . First run, Mo team , Get all this O ( n n ) O(n\sqrt n) O(nn) A asked .
The difference is to query a prefix less than x x x The number of , Block the range , It is equivalent to a block prefix and an intra block prefix , Scan from front to back , After adding one number at a time O ( n ) O(\sqrt n) O(n) modify , The query is O ( 1 ) O(1) O(1) Of .
Online practice :
See WerKeyTom_FTD The blog of
The general idea is to preprocess the intra block contribution , Inter block contribution , When it is necessary to calculate the contribution between two cells, find the sorted situation in the interval through the pre ordered array in the block , And then merge .
The complexity of time and space is O ( n n ) O(n\sqrt n) O(nn) Of .
边栏推荐
- 【软件测试】这样的简历已经刷掉了90%的面试者
- Rabin-Miller素数测试
- Create a form whose client area is 800 pixels by 600 pixels
- 2022.5.30-6.5 AI行业周刊(第100期):三年时光
- Gobang interface of mobile console (C language)
- 3年功能测试拿8K,被新来的反超,其实你在假装努力
- 2、 User login and registration
- 【AtCoder1980】Mysterious Light(数学模拟)
- Raspberry pie builds a full-featured NAS server (07): manage your library & read as you please
- P3811 [template] multiplicative inverse
猜你喜欢
![[analysis of STL source code] summary note (4): behind the scenes hero allocator](/img/b9/cf53fd8f933042ff65844d61eca55e.jpg)
[analysis of STL source code] summary note (4): behind the scenes hero allocator

JVM tuning

Directrix of ellipse

【Oracle 数据库】奶妈式教程day03 排序查询

C language Yanghui triangle code

群晖DS918创建m.2 固态硬盘SSD读写缓存

【Oracle 数据库】奶妈式教程day02 数据库管理工具SQLPLUS的使用

2022低压电工考题及在线模拟考试
![20200803 T3 my friends [divide and conquer NTT optimization recursion]](/img/35/01201e3136e3dd5cd562a0481f1ee9.jpg)
20200803 T3 my friends [divide and conquer NTT optimization recursion]

Nim product
随机推荐
Second remaining learning notes
Classification of MNIST datasets with keras
[STL source code analysis] summary notes (7): ingenious deque
Sdl-2 thread logic
【CF#388 (Div. 2)】A. Bachgold Problem
Console program for viewing BMP files
C language judging big end and small end [consortium or pointer] big end and small end conversion
【CF#697 (Div. 3)】 A - Odd Divisor
QT picture adaptive display control
Sqlzoo question brushing record-3
MS office level II wrong question record [10]
MFC debugger OutputDebugString override
Tetris preliminary
Sdl-3 YUV playback
QT interface nested movement based on qscrollarea
[STL source code analysis] summary notes (5): a good helper for understanding iterators --list
【CodeForces908H】New Year and Boolean Bridges (FWT)
2022 melting welding and thermal cutting exam exercises and answers
Uoj 553 [unr 4] caproic acid set [computational geometry (points in circle → points in half plane)]
二本毕业,银行外包测试工作 4 个月有余。聊聊一些真实感受 ...