码迷,mamicode.com
首页 > 其他好文 > 详细

luogu P2365 任务安排

时间:2018-12-16 11:10:35      阅读:122      评论:0      收藏:0      [点我收藏+]

标签:def   gis   ++i   复杂   ons   spl   https   机器   include   

嘟嘟嘟


如果常规dp,\(dp[i][j]\)表示前\(i\)个任务分\(j\)组,得到
\[dp[i][j] = min _ {k = 0} ^ {i - 1} (dp[k][j - 1] + (s * j + sumt[i]) * (sumc[i] - sumc[k]))\]
复杂度是\(O(n ^ 3)\)的。
因此我们要换一个思路。
在执行一批任务时,我们虽然不知道之前机器启动过多少次,但是可以确定机器因执行这批人武而花费的启动时间为\(s\),会累加到后面的任务上。
因此,令\(dp[i]\)表示把前\(i\)个任务分成若干批的最小费用,则
\[dp[i] = min_{j = 0} ^ {i - 1} (dp[j] + sumt[i] * (sumc[i] - sumc[j]) + s * (sumc[n] - sumc[j]))\]
\(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;
}

luogu P2365 任务安排

标签:def   gis   ++i   复杂   ons   spl   https   机器   include   

原文地址:https://www.cnblogs.com/mrclr/p/10125687.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!