头部背景图片
Decaku 's Blog |
Decaku 's Blog |

牛客练习赛59 E 石子搬运

$ n堆石头,最多搬m次,每次只能选一堆搬运一部分,搬运一次代价 \\
是石头数量的平方,询问搬完最小代价,还有一个操作是修改一堆 \\
石头数量。m,n数量级都是400。$


$贪心似乎可做,但我不太会。考虑dp[i][j]为把第i堆石头j次 \\
搬完的最小代价,搬完n堆就是把dp做区间合并。然后因为有修改,\\
所以用线段树记录区间的dp信息就可以了。$

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#include<bits/stdc++.h>
using namespace std;
#define IOS ios::sync_with_stdio(false);cin.tie(0);
#define pii pair<ll,ll>
#define F first
#define S second
#define pb push_back
#define mp make_pair
#define rep(i,a,b) for(ll i=a;i<=(ll)b;++i)
#define per(i,a,b) for(ll i=a;i>=(ll)b;--i)
#define ll long long
#define lowbit(x) ((x)&(-x))

const ll maxn=410;
ll dp[maxn<<2][maxn],a[maxn],n,m,q,sum[maxn<<2],len[maxn<<2];

void build(ll u,ll l,ll r)
{
if(l==r)
{
sum[u] = a[l]; len[u] = 1;
rep(i,1,min(m,a[l]))
{
ll x = i-a[l]%i, y = a[l]%i;
dp[u][i] = (a[l]/i)*(a[l]/i)*x + (a[l]/i+1)*(a[l]/i+1)*y;
}
return ;
}
ll mid=l+r>>1;
build(u<<1,l,mid);
build(u<<1|1,mid+1,r);
sum[u] = sum[u<<1]+sum[u<<1|1];
len[u] = len[u<<1]+len[u<<1|1];
rep(i,0,400) dp[u][i] = 1e18;
rep(i,len[u<<1],min(m,sum[u<<1])){
rep(j,len[u<<1|1],min(m,sum[u<<1|1])){
if(i+j<=m) dp[u][i+j] = min(dp[u][i+j],dp[u<<1][i]+dp[u<<1|1][j]);
}
}
}

void modify(ll u,ll l,ll r,ll x,ll v)
{
if(l==r)
{
a[x]=v; sum[u]=v;
rep(i,1,min(m,a[l]))
{
ll x = i-a[l]%i, y = a[l]%i;
dp[u][i] = (a[l]/i)*(a[l]/i)*x+(a[l]/i+1)*(a[l]/i+1)*y;
}
return ;
}
ll mid=l+r>>1;
if(x<=mid) modify(u<<1,l,mid,x,v);
if(x>mid) modify(u<<1|1,mid+1,r,x,v);
sum[u] = sum[u<<1]+sum[u<<1|1];
rep(i,0,400) dp[u][i] = 1e18;
rep(i,len[u<<1],min(m,sum[u<<1])){
rep(j,len[u<<1|1],min(m,sum[u<<1|1])){
if(i+j<=m) dp[u][i+j] = min(dp[u][i+j],dp[u<<1][i]+dp[u<<1|1][j]);
}
}
}
int main()
{
IOS
cin>>n>>m;
rep(i,1,n) cin>>a[i];
build(1,1,n);
ll q; cin>>q;
while(q--)
{
ll x,v; cin>>x>>v;
modify(1,1,n,x,v);
cout<< dp[1][min(m,sum[1])] <<"\n";
}
}
avatar Decaku 菜菜菜