luogu P2365 任务安排
作者:互联网
嘟嘟嘟
如果常规dp,\(dp[i][j]\)表示前\(i\)个任务分\(j\)组,得到
复杂度是\(O(n ^ 3)\)的。
因此我们要换一个思路。
在执行一批任务时,我们虽然不知道之前机器启动过多少次,但是可以确定机器因执行这批人武而花费的启动时间为\(s\),会累加到后面的任务上。
因此,令\(dp[i]\)表示把前\(i\)个任务分成若干批的最小费用,则
\(sumt[i] * (sumc[i] - sumc[j])\)表示的是不考虑机器启动时前\(i\)批任务的费用。之所以可以这么写,是因为后面的\(s * (sumc[n] - sumc[j])\)已经把他们的时间算进去了,即包含在了\(dp[j]\)中。
时间复杂度\(O(n ^ 2)\)。
#include<cstdio>
#include<iostream>
#include<cmath>
#include<algorithm>
#include<cstring>
#include<cstdlib>
#include<cctype>
#include<vector>
#include<stack>
#include<queue>
using namespace std;
#define enter puts("")
#define space putchar(' ')
#define Mem(a, x) memset(a, x, sizeof(a))
#define rg register
typedef long long ll;
typedef double db;
const int INF = 0x3f3f3f3f;
const db eps = 1e-8;
const int maxn = 5e3 + 5;
inline ll read()
{
ll ans = 0;
char ch = getchar(), last = ' ';
while(!isdigit(ch)) last = ch, ch = getchar();
while(isdigit(ch)) ans = (ans << 1) + (ans << 3) + ch - '0', ch = getchar();
if(last == '-') ans = -ans;
return ans;
}
inline void write(ll x)
{
if(x < 0) x = -x, putchar('-');
if(x >= 10) write(x / 10);
putchar(x % 10 + '0');
}
int n, s;
int sumt[maxn], sumc[maxn];
int dp[maxn];
int main()
{
n = read(); s = read();
for(int i = 1, t, c; i <= n; ++i)
{
t = read(), sumt[i] = sumt[i - 1] + t;
c = read(), sumc[i] = sumc[i - 1] + c;
}
Mem(dp, 0x3f); dp[0] = 0;
for(int i = 1; i <= n; ++i)
for(int j = 0; j < i; ++j)
dp[i] = min(dp[i], dp[j] + sumt[i] * (sumc[i] - sumc[j]) + s * (sumc[n] - sumc[j]));
write(dp[n]), enter;
return 0;
}
上述算法已经能过此题,但还有一个$O(n)$的做——斜率优化。 简单来说就是对上述dp式进行变形,把常数、仅与$i$有关的项、仅与$j$有关的项以及$i, j$的乘积项分开。 具体维护下凸壳等想法不想讲了(懒),以后填坑吧 先上代码 ```c++ #include #include #include #include #include #include #include #include #include #include using namespace std; #define enter puts("") #define space putchar(' ') #define Mem(a, x) memset(a, x, sizeof(a)) #define rg register typedef long long ll; typedef double db; const int INF = 0x3f3f3f3f; const db eps = 1e-8; const int maxn = 1e4 + 5; inline ll read() { ll ans = 0; char ch = getchar(), last = ' '; while(!isdigit(ch)) last = ch, ch = getchar(); while(isdigit(ch)) ans = (ans << 1) + (ans << 3) + ch - '0', ch = getchar(); if(last == '-') ans = -ans; return ans; } inline void write(ll x) { if(x < 0) x = -x, putchar('-'); if(x >= 10) write(x / 10); putchar(x % 10 + '0'); }
int n, s;
ll sumt[maxn], sumc[maxn];
ll dp[maxn];
int q[maxn], l = 1, r = 1;
int main()
{
n = read(); s = read();
for(int i = 1, t, c; i <= n; ++i)
{
t = read(), sumt[i] = sumt[i - 1] + t;
c = read(), sumc[i] = sumc[i - 1] + c;
}
Mem(dp, 0x3f); dp[0] = 0;
for(int i = 1; i <= n; ++i)
{
while(l < r && (dp[q[l + 1]] - dp[q[l]]) <= (s + sumt[i]) * (sumc[q[l + 1]] - sumc[q[l]])) l++;
dp[i] = dp[q[l]] - (s + sumt[i]) * sumc[q[l]] + sumt[i] * sumc[i] + s * sumc[n];
while(l < r && (dp[q[r]] - dp[q[r - 1]]) * (sumc[i] - sumc[q[r]]) >= (dp[i] - dp[q[r]]) * (sumc[q[r]] - sumc[q[r - 1]])) r--;
q[++r] = i;
}
write(dp[n]), enter;
return 0;
}
标签:sumt,安排,int,luogu,P2365,ch,include,sumc,dp 来源: https://blog.51cto.com/u_15234622/2831196