Task scheduling (dp)

Insert picture description here

Idea: dp[i] represents the minimum value after the task is divided from 1 to i, dp[i] must be the last division from the previous division. The previous division is divided into 2 parts. Once again, 1—j is divided into dp. [j], the remaining part is from j to i, then we need to ask for the cost from j to i, because from j, we must first add the machine startup time. This startup time will affect everything later. We directly calculate The cost is s*(sumc[n]-sumc[j]) and then the cost of each task from j to i is sum[i]*(sumc[i]-sumc[j]), the state transition equation can be obtained Is dp [i] = min (dp [i], dp [j] + sumt [i] ∗ (sumc [i] − sumc [j]) + m ∗ (sumc [n] − sumc [j])) dp [i]=min(dp[i],dp[j]+sumt[i]*(sumc[i]-sumc[j])+m*(sumc[n]-sumc[j]))dp[i]=min(dp[i],dp[j]+s u m t [ i ](sumc[i]sumc[j])+m(sumc[n]sumc[j]))

#pragma GCC optimize(2)
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <set>
#include<iostream>
#include<vector>
#include<queue>
#include<map>
#include<stack>
#include<iomanip>
#include<cstring>
#include<time.h>
 
using namespace std;
typedef long long ll;
#define SIS std::ios::sync_with_stdio(false)
#define space putchar(' ')
#define enter putchar('\n')
#define lson root<<1
#define rson root<<1|1
typedef pair<int,int> PII;
const int mod=1e9+7;
const int N=2e6+10;
const int M=1e5+10;
const int inf=0x3f3f3f3f;
const int maxx=2e5+7;
const double eps=1e-6;
int gcd(int a,int b)
{
    
    
    return b==0?a:gcd(b,a%b);
}
 
ll lcm(ll a,ll b)
{
    
    
    return a*(b/gcd(a,b));
}
 
template <class T>
void read(T &x)
{
    
    
    char c;
    bool op = 0;
    while(c = getchar(), c < '0' || c > '9')
        if(c == '-')
            op = 1;
    x = c - '0';
    while(c = getchar(), c >= '0' && c <= '9')
        x = x * 10 + c - '0';
    if(op)
        x = -x;
}
template <class T>
void write(T x)
{
    
    
    if(x < 0)
        x = -x, putchar('-');
    if(x >= 10)
         write(x / 10);
    putchar('0' + x % 10);
}
ll qsm(int a,int b,int p)
{
    
    
    ll res=1%p;
    while(b)
    {
    
    
        if(b&1)
            res=res*a%p;
        a=1ll*a*a%p;
        b>>=1;
    }
    return res;
}
int n, m,k;
int dp[5005];
int sumt[5005],sumc[5005];
int main()
{
    
    
    scanf("%d%d",&n,&m);
    for(int i=1;i<=n;i++){
    
    
      scanf("%d%d",&sumt[i],&sumc[i]);
      sumt[i]+=sumt[i-1];
      sumc[i]+=sumc[i-1];
    }
    memset(dp,inf,sizeof dp);
    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])+m*(sumc[n]-sumc[j]));
        }
    }
    printf("%d",dp[n]);


    return 0;

}


Guess you like

Origin blog.csdn.net/qq_43619680/article/details/109404114