一、任务安排1
#include<iostream>
#include<algorithm>
#include<cstring>
using namespace std;
const int N = 5010;
typedef long long LL;
int n,s;
LL t[N],c[N];
LL f[N];
int main()
{
cin >> n >> s;
for(int i = 1;i <= n;i ++)
{
cin >> t[i] >> c[i];
t[i] += t[i - 1];
c[i] += c[i - 1];
}
memset(f, 0x3f, sizeof f);
f[0] = 0;
for(int i = 1;i <= n;i ++)
{
for(int j = 0;j < i;j ++)
f[i] = min(f[i], f[j] + t[i] * (c[i] - c[j]) + s * (c[n] - c[j]));
}
cout << f[n];
return 0;
}
二、任务安排2
#include<iostream>
#include<algorithm>
#include<cstring>
using namespace std;
const int N = 300010;
typedef long long LL;
LL t[N],c[N];
LL f[N];
int q[N];//队列
int n,s;
int main()
{
scanf("%d%d", &n, &s);
for(int i = 1;i <= n;i ++)
{
scanf("%lld%lld", &t[i],&c[i]);
t[i] += t[i - 1];
c[i] += c[i - 1];
}
int hh = 0, tt = 0;//默认队列已经进入了一个元素所以tt不用置为-1
q[0] = 0;
for(int i = 1;i <= n;i ++)//根据凸包函数,我们要保证f[i] - t[i] - c[i]最小//在斜率固定的情况下经过的点一定是一个外围的凸包,斜率为s + t[i]
{
while(hh < tt && f[q[hh + 1]] - f[q[hh]] <= (t[i] + s) * (c[q[hh + 1]] - c[q[hh]]) ) hh ++;
int j = q[hh];
f[i] = f[j] - (t[i] + s) * c[j] + t[i] * c[i] + s * c[n];
while (hh < tt && (__int128)(f[q[tt]] - f[q[tt - 1]]) * (c[i] - c[q[tt - 1]]) >= (__int128)(f[i] - f[q[tt - 1]]) * (c[q[tt]] - c[q[tt - 1]])) tt -- ;
q[ ++ tt] = i;
}
printf("%lld\n", f[n]);
return 0;
}
三、任务安排3
这里就加了一个二分优化,其他没什么变化
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
typedef long long LL;
const int N = 300010;
int n, s;
LL t[N], c[N];
LL f[N];
int q[N];
int main()
{
scanf("%d%d", &n, &s);
for (int i = 1; i <= n; i ++ )
{
scanf("%lld%lld", &t[i], &c[i]);
t[i] += t[i - 1];
c[i] += c[i - 1];
}
int hh = 0, tt = 0;
q[0] = 0;
for (int i = 1; i <= n; i ++ )
{
int l = hh, r = tt;
while (l < r)
{
int mid = l + r >> 1;
if (f[q[mid + 1]] - f[q[mid]] > (t[i] + s) * (c[q[mid + 1]] - c[q[mid]])) r = mid;
else l = mid + 1;
}
int j = q[r];
f[i] = f[j] - (t[i] + s) * c[j] + t[i] * c[i] + s * c[n];
while (hh < tt && (double)(f[q[tt]] - f[q[tt - 1]]) * (c[i] - c[q[tt - 1]]) >= (double)(f[i] - f[q[tt - 1]]) * (c[q[tt]] - c[q[tt - 1]])) tt -- ;
q[ ++ tt] = i;
}
printf("%lld\n", f[n]);
return 0;
}
四、运输小猫
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
typedef long long LL;
const int N = 100010, M = 100010, P = 110;
int n, m, p; // n: 物品数量, m: 任务数量, p: 机器数量
LL d[N], t[N], a[N], s[N]; // d: 物品位置前缀和, t: 任务完成时间, a: 任务开始时间与物品位置差, s: a的前缀和
LL f[P][M]; // f[j][i] 表示前j个机器完成前i个任务的最小代价
int q[M]; // 斜率优化中的单调队列
// 计算决策点k在状态转移方程中的y值
LL get_y(int k, int j)
{
return f[j - 1][k] + s[k];
}
int main()
{
scanf("%d%d%d", &n, &m, &p);
// 输入物品位置并计算前缀和
for (int i = 2; i <= n; i ++ )
{
scanf("%lld", &d[i]);
d[i] += d[i - 1];
}
// 输入任务并计算任务开始时间与物品位置差
for (int i = 1; i <= m; i ++ )
{
int h;
scanf("%d%lld", &h, &t[i]);
a[i] = t[i] - d[h];
}
// 对任务开始时间与物品位置差进行排序
sort(a + 1, a + m + 1);
// 计算a的前缀和,用于后续斜率计算
for (int i = 1; i <= m; i ++ ) s[i] = s[i - 1] + a[i];
// 初始化dp数组,没有机器时,完成任何任务都需要无穷大代价
memset(f, 0x3f, sizeof f);
for (int i = 0; i <= p; i ++ ) f[i][0] = 0;
// 动态规划求解
for (int j = 1; j <= p; j ++ ) // 枚举机器数量
{
int hh = 0, tt = 0; // 单调队列的头尾指针
q[0] = 0; // 队列初始化为0,表示不使用任何任务
for (int i = 1; i <= m; i ++ ) // 枚举任务数量
{
// 维护单调队列,找到最优决策点k
while (hh < tt && (get_y(q[hh + 1], j) - get_y(q[hh], j)) <= a[i] * (q[hh + 1] - q[hh])) hh ++ ;
int k = q[hh]; // 当前最优决策点
// 更新dp状态
f[j][i] = f[j - 1][k] - a[i] * k + s[k] + a[i] * i - s[i];
// 维护单调队列的单调性
while (hh < tt && (get_y(q[tt], j) - get_y(q[tt - 1], j)) * (i - q[tt]) >=
(get_y(i, j) - get_y(q[tt], j)) * (q[tt] - q[tt - 1])) tt -- ;
q[ ++ tt] = i;
}
}
// 输出结果,前p个机器完成前m个任务的最小代价
printf("%lld\n", f[p][m]);
return 0;
}