【斜率优化】【CF311B】 Cats Transport

题目

Zxr960115 is owner of a large farm. He feeds mm cute cats and employs pp feeders. There’s a straight road across the farm and nn hills along the road, numbered from 1 to nn from left to right. The distance between hill ii and (i-1)(i−1) is d_{i}d
i

meters. The feeders live in hill 1.

One day, the cats went out to play. Cat ii went on a trip to hill h_{i}h
i

, finished its trip at time t_{i}t
i

, and then waited at hill h_{i}h
i

for a feeder. The feeders must take all the cats. Each feeder goes straightly from hill 1 to nn without waiting at a hill and takes all the waiting cats at each hill away. Feeders walk at a speed of 1 meter per unit time and are strong enough to take as many cats as they want.

For example, suppose we have two hills (d_{2}=1)(d
2

=1) and one cat that finished its trip at time 3 at hill 2 (h_{1}=2)(h
1

=2) . Then if the feeder leaves hill 1 at time 2 or at time 3, he can take this cat, but if he leaves hill 1 at time 1 he can’t take it. If the feeder leaves hill 1 at time 2, the cat waits him for 0 time units, if the feeder leaves hill 1 at time 3, the cat waits him for 1 time units.

Your task is to schedule the time leaving from hill 1 for each feeder so that the sum of the waiting time of all cats is minimized.

思路

首先对于每一只猫可以计算出何时出发的人可以正好接走它。
如此得到一个长 m 的时间数组 t[],将它从小到大排序。
令S[]为t[]的前缀和,dp[i][j]为i个人搞定前j只猫的代价,则
在这里插入图片描述
显然,这就可以用斜率优化。

代码

#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int N=1e5+77;
ll d[N],t[N],S[N],dp[107][N];
int q[107][N],hd[N],tl[N];

double Slope(int i,int j1,int j2)
{
	return (1.0*S[j1]+dp[i][j1]-S[j2]-dp[i][j2])/(j1-j2);
}

int main(){
	int n,m,p;
	cin>>n>>m>>p;
	if(p>=m) return puts("0"),0;
	for(int i=2; i<=n; i++)
	{
		cin>>d[i];
		d[i]+=d[i-1];
	}
	for(int i=1,x,p; i<=m; i++)
	{
		cin>>p>>x;
		t[i]=-d[p]+x;
	}
	sort(t+1,t+1+m);
	for(int i=1; i<=m; i++)
		S[i]=S[i-1]+t[i];
	for(int i=1; i<=m; i++)
	{
		for(int j=1; j<=min(i,p); j++)
		{
			while(hd[j-1]<tl[j-1]&&Slope(j-1,q[j-1][hd[j-1]],q[j-1][hd[j-1]+1])<=t[i])
				++hd[j-1];
			int tr=q[j-1][hd[j-1]]; 
			dp[j][i]=(dp[j-1][tr]+t[i]*(i-tr)-S[i]+S[tr]);
			while(hd[j]<tl[j]&&Slope(j,q[j][tl[j]],i)<Slope(j,q[j][tl[j]-1],q[j][tl[j]]))
				--tl[j];
			q[j][++tl[j]]=i;
		}
	}
	cout<<dp[p][m];
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
map pfn expected mapping type uncached-minus for [mem 0x7c11f000-0x7c11ffff], got write-back 这个问题怎么解决,这个会导致系统死机吗?PID: 500 TASK: ffff8800740d6dd0 CPU: 4 COMMAND: "mate-settings-d" #0 [ffff88024a6e7988] machine_kexec at ffffffff81059cdb #1 [ffff88024a6e79e8] __crash_kexec at ffffffff81105182 #2 [ffff88024a6e7ab8] crash_kexec at ffffffff81105270 #3 [ffff88024a6e7ad0] oops_end at ffffffff8168ed88 #4 [ffff88024a6e7af8] no_context at ffffffff8167e993 #5 [ffff88024a6e7b48] __bad_area_nosemaphore at ffffffff8167ea29 #6 [ffff88024a6e7b90] bad_area_nosemaphore at ffffffff8167eb93 #7 [ffff88024a6e7ba0] __do_page_fault at ffffffff81691b1e #8 [ffff88024a6e7c00] do_page_fault at ffffffff81691cc5 #9 [ffff88024a6e7c30] page_fault at ffffffff8168df88 [exception RIP: dev_set_drvdata+26] RIP: ffffffff8142c60a RSP: ffff88024a6e7ce8 RFLAGS: 00010206 RAX: 0000000900000000 RBX: ffff880258686098 RCX: 0000000180040001 RDX: ffff8801849e4000 RSI: 0000000000000000 RDI: ffff880258686098 RBP: ffff88024a6e7cf8 R8: ffff8801849e4000 R9: 0000000180040001 R10: 00000000849e6001 R11: ffffea0006127800 R12: ffff880239383398 R13: ffff880239383300 R14: ffff880061c29d08 R15: 0000000000000246 ORIG_RAX: ffffffffffffffff CS: 0010 SS: 0018 #10 [ffff88024a6e7d00] snd_usb_audio_free at ffffffffa059a587 [snd_usb_audio] #11 [ffff88024a6e7d28] snd_usb_audio_dev_free at ffffffffa059a5b2 [snd_usb_audio] #12 [ffff88024a6e7d38] __snd_device_free at ffffffffa02e2dc9 [snd] #13 [ffff88024a6e7d50] snd_device_free_all at ffffffffa02e311b [snd] #14 [ffff88024a6e7d70] release_card_device at ffffffffa02dd7de [snd] #15 [ffff88024a6e7d90] device_release at ffffffff814273f2 #16 [ffff88024a6e7db8] kobject_release at ffffffff8131a29e #17 [ffff88024a6e7de8] kobject_put at ffffffff8131a158 #18 [ffff88024a6e7e00] put_device at ffffffff81427717 #19 [ffff88024a6e7e10] snd_card_file_remove at ffffffffa02de1b4 [snd] #20 [ffff88024a6e7e40] snd_ctl_release at ffffffffa02df421 [snd] #21 [ffff88024a6e7e78] snd_disconnect_release at ffffffffa02ddafd [snd] #22 [ffff88024a6e7ea8] __fput at ffffffff811fff09 #23 [ffff88024a6e7ef0] ____fput at ffffffff812001be #24 [ffff88024a6e7f00] task_work_run at ffffffff810accc7 #25 [ffff88024a6e7f30] do_notify_resume at ffffffff8102ab22 #26 [ffff88024a6e7f50] int_signal at ffffffff8169677d gdb调试的bt,这个怎么判断是什么造成的死机
最新发布
07-20

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值