Solution to a problem -Cats Transport

Solution to a problem -Cats Transport

Cats Transport

There \ (n-\) a hill, \ (m \) cat promoter, \ (P \) only official shovel feces. The first \ (i-1 \) th section to the hill \ (I \) from a hill is \ (D_i \) . The first \ (i \) cats child in the hills \ (h_i \) play \ (t_i \) time. Each shovel feces officer can choose the departure time, and then from \ (1 \) a unit number per hill, continuing to go \ (n \) No. hills, already comes to an end collar on the cat walk. Each cat have kittens seeking child being led away most of the time and wait.

数据范围: \ (2 \ n \ 10 ^ 5 \) , \ (1 \ m \ 10 ^ 5 \) , \ (1 \ p \ 100 \) , \ (1 \ the d_i \ 10 ^ 4 \) , \ (1 \ the h_i \ n \) , \ (0 \ the t_i \ 10 ^ 9 \) .


Pretreatment can first look to simplify the problem:

\(D_i=\sum_{j=2}^i d_j\)

So \ (1 \) number to the hill \ (I \) No. hill distance \ (D_i \) .

\(T_i=t_i-D_{h_i}\)

It can be regarded as all cats are \ (1 \) No. hills, finish off time is \ (T_i \) .

The press each kitten (T_i \) \ sorting the \ (T_ {}. 1-I \ Le T_i \) (each cat requires only sub \ (T_i \) a message to).


Therefore, it becomes subject to sequence \ (T_i \) into \ (P \) subsequences, such that each difference number sequence to its maximum and minimum number of.

Sequences herein do not require continuous, it will be evident \ (T_i \) under orderly circumstances, more preferably continuous.

The first may assume \ (A \) a shovel feces took the first official \ (j + 1 \ sim i \) cat promoter, i.e., the maximum number of the sub-sequence is \ (T_i \) , may be as follows \ (\ texttt DP} {\) :

Set \ (f_ {a, i} \) represents the section \ (A \) th received official feces shovel section \ (I \) minimum latency and sub cats.

\[f_{a,i}=\min\{f_{a-1,j}+\sum_{h=j+1}^i(T_i-T_h)\} \]

About optimization, so \ (S_I = \ sum_. 1} = {J ^ I T_j \) :

\[\begin{split} f_{a,i}=&\min\{f_{a-1,j}+T_i(i-j)-(s_i-s_j)\}\\ f_{a,i}=&f_{a-1,j}+T_i(i-j)-(s_i-s_j)\\ f_{a,i}=&f_{a-1,j}+iT_i-jT_i-s_i+s_j\\ f_{a-1,j}+s_j=&T_i\cdot j+f_{a,i}+s_i-iT_i\\ \end{split} \\ \begin{cases} y=f_{a-1,j}+s_j\\ k=T_i\\ x=j\\ b=f_{a,i}+s_i-iT_i\\ \end{cases} \]

Sets a slope maintenance optimization \ (\ texttt {dp} \ ) convex hull under the template to the minimum.


Time complexity \ (\ Theta (n-m + \ log m + MP) \) , the spatial complexity \ (\ Theta (n-+ MP) \) .


Code

#include <bits/stdc++.h>
using namespace std;

//Start
#define re register
#define il inline
#define mk make_pair
#define pb push_back
#define db double
#define lng long long
#define fi first
#define se second
const int inf=0x3f3f3f3f;
const lng INF=0x3f3f3f3f3f3f3f3f;

//Data
const int N=100000,P=100;
int n,m,p;
lng d[N+7],t[N+7],s[N+7];

//DP
lng f[P+7][N+7];
pair<int,int> lor[P+7];
int q[P+7][N+7];
#define l(x) lor[x].fi
#define r(x) lor[x].se
il db X(re int a,re int j){
	return j;
}
il db Y(re int a,re int j){
	return f[a][j]+s[j];
}
il db slope(re int a,re int k,re int t){
	return (Y(a,k)-Y(a,t))/(X(a,k)-X(a,t));
}
il lng F(re int a,re int i,re int j){
	return f[a-1][j]+t[i]*(i-j)-(s[i]-s[j]);
}
il lng DP(){
	for(re int a=0;a<=p;a++){ //必须初始化
		lor[a]=mk(1,0);
		q[a][++r(a)]=0;
	}
	for(re int a=1;a<=p;a++){
		for(re int i=1;i<=m;i++){
			// printf("(%d,%d)\n",a,i);
			while(l(a-1)<r(a-1)&&slope(a-1,q[a-1][l(a-1)],q[a-1][l(a-1)+1])<=t[i]) l(a-1)++;
			//维护a-1队列递推
			f[a][i]=F(a,i,q[a-1][l(a-1)]);
			while(l(a)<r(a)&&slope(a,q[a][r(a)-1],q[a][r(a)])>=slope(a,q[a][r(a)],i)) r(a)--;
			//维护a队列为递推a+1做准备
			q[a][++r(a)]=i;
		}
	}
	return f[p][m];
}

//Main
int main(){
	scanf("%d%d%d",&n,&m,&p);
	if(p>=m) return puts("0"),0;
	for(re int i=2,x;i<=n;i++){
		scanf("%d",&x);
		d[i]=d[i-1]+x;
	}
	for(re int i=1,x,y;i<=m;i++){
		scanf("%d%d",&x,&y);
		t[i]=-d[x]+y;
	}
	sort(t+1,t+m+1);
	for(re int i=1;i<=m;i++) s[i]=s[i-1]+t[i];
	printf("%lld\n",DP());
	return 0;
}

I wish you happy learning!

Guess you like

Origin www.cnblogs.com/Wendigo/p/12616203.html