You Are the One(利用栈的性质的区间DP)

本文介绍了一道经典的动态规划问题,即如何通过调整相亲顺序使参与者的总体不开心值最小。利用动态规划方法解决此问题,并提供了两种不同的实现方案。


Link:http://acm.hdu.edu.cn/showproblem.php?pid=4283



You Are the One

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2436    Accepted Submission(s): 1137


Problem Description
  The TV shows such as You Are the One has been very popular. In order to meet the need of boys who are still single, TJUT hold the show itself. The show is hold in the Small hall, so it attract a lot of boys and girls. Now there are n boys enrolling in. At the beginning, the n boys stand in a row and go to the stage one by one. However, the director suddenly knows that very boy has a value of diaosi D, if the boy is k-th one go to the stage, the unhappiness of him will be (k-1)*D, because he has to wait for (k-1) people. Luckily, there is a dark room in the Small hall, so the director can put the boy into the dark room temporarily and let the boys behind his go to stage before him. For the dark room is very narrow, the boy who first get into dark room has to leave last. The director wants to change the order of boys by the dark room, so the summary of unhappiness will be least. Can you help him?
 

Input
  The first line contains a single integer T, the number of test cases.  For each case, the first line is n (0 < n <= 100)
  The next n line are n integer D1-Dn means the value of diaosi of boys (0 <= Di <= 100)
 

Output
  For each test case, output the least summary of unhappiness .
 

Sample Input
  
2    5 1 2 3 4 5 5 5 4 3 2 2
 

Sample Output
  
Case #1: 20 Case #2: 24
 

Source
 



题目大意:

有n个男屌丝事先按1,2,3,,,,,,n的顺序排好,每个人都有一个不开心值unhappy[i],如果第i个人第k个上台找对象,那么该屌丝男的不开心值就会为(k-1)*unhappy[i],因为在他前面有k-1个人嘛,导演为了让所有男屌的总不开心值最小,搞了一个小黑屋,可以通过小黑屋来改变男屌的出场顺序

注意:这个小黑屋是个栈,男屌的顺序是排好了的,但是可以通过入栈出栈来改变男屌的出场顺序


解题思路:(操度娘所知~度娘你好腻害)

dp[i][j]表示区间[i,j]的最小总不开心值

把区间[i,j]单独来看,则第i个人可以是第一个出场,也可以是最后一个出场(j-i+1),也可以是在中间出场(1   ~  j-i+1)

不妨设他是第k个出场的(1<=k<=j-i+1),那么根据栈后进先出的特点,以及题目要求原先男的是排好序的,那么::

第  i+1  到 i+k-1  总共有k-1个人要比i先出栈,

第 i+k   到j 总共j-i-k+1个人在i后面出栈

举个例子吧:

有5个人事先排好顺序  1,2,3,4,5

入栈的时候,1入完2入,2入完3入,如果我要第1个人第3个出场,那么入栈出栈顺序是这样的:

1入,2入,3入,3出,2出,1出(到此第一个人就是第3个出场啦,很明显第2,3号人要在1先出,而4,5要在1后出)


这样子,动态转移方程就出来啦,根据第i个人是第k个出场的,将区间[i,j]分成3个部分

dp[i][j]=min(dp[i][j],dp[i+1,i+k-1]+dp[i+k,j]+(k-1)*a[i]+(sum[j]-sum[i+k-1])*k);   

(sum[j]-sum[i+k-1])*k 表示 后面的 j-i-k+1个人是在i后面才出场的,那么每个人的不开心值都会加个 unhappy,sum[i]用来记录前面i个人的总不开心值,根据题目,每个人的unhappy是个累加的过程,多等一个人,就多累加一次



AC code:

#include<iostream>
#include<algorithm>
#include<stdio.h>
#include<cstring>
#include<cmath>
#include<vector>
#include<string.h>
#define LL long long
using namespace std;
const int INF=0x3f3f3f3f;
int dp[111][111];
int sum[111];
int a[111];
int n;
int main()
{
    int T,cas,i,j,k,p;
    cas=0;
    scanf("%d",&T);
    while(T--)
    {
        cas++;
        sum[0]=0;
        scanf("%d",&n);
        for(i=1;i<=n;i++)
        {
            scanf("%d",&a[i]);
            sum[i]=sum[i-1]+a[i];
        }
        memset(dp,0,sizeof(dp));
        for(i=1;i<=n;i++)
        {
            for(j=i+1;j<=n;j++)
            {
                dp[i][j]=INF;
            }
        }
        for(int p=1;p<=n;p++)
        {
            for(i=1;i<=n-p+1;i++)
            {
                j=i+p-1;
                for(k=1;k<=p;k++)
                {
                    dp[i][j]=min(dp[i][j],dp[i+1][i+k-1]+dp[i+k][j]+(k-1)*a[i]+(sum[j]-sum[i+k-1])*k);

                }
            }
        }
        printf("Case #%d: %d\n",cas,dp[1][n]);

    }
    return 0;
}



记忆化搜索的区间DP比较好理解,附上:

#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
using namespace std;
int dp[110][110];
int a[110],sum[110];
int n;
int solve(int i,int j)
{
    int &ans=dp[i][j];
    if(ans!=-1) return ans;
    if(i>=j) return 0;
    ans=1<<30;
    for(int k=1;k<=j-i+1;k++){
        ans=min(ans,solve(i+1,i+k-1)+solve(i+k,j)+(k-1)*a[i]+(sum[j]-sum[i+k-1])*k);
    }
    return ans;
}
int main()
{
    int t,iCase=1;
    cin>>t;
    while(t--){
        cin>>n;
        sum[0]=0;
        for(int i=1;i<=n;i++){
            scanf("%d",&a[i]);
            sum[i]=sum[i-1]+a[i];
        }
        memset(dp,-1,sizeof dp);
        printf("Case #%d: %d\n",iCase++,solve(1,n));
    }
    return 0;
}



Batch Scheduling There is a sequence of N jobs to be processed on one machine. The jobs are numbered from 1 to N, so that the sequence is 1,2,..., N. The sequence of jobs must be partitioned into one or more batches, where each batch consists of consecutive jobs in the sequence. The processing starts at time 0. The batches are handled one by one starting from the first batch as follows. If a batch b contains jobs with smaller numbers than batch c, then batch b is handled before batch c. The jobs in a batch are processed successively on the machine. Immediately after all the jobs in a batch are processed, the machine outputs the results of all the jobs in that batch. The output time of a job j is the time when the batch containing j finishes. A setup time S is needed to set up the machine for each batch. For each job i, we know its cost factor Fi and the time Ti required to process it. If a batch contains the jobs x, x+1,... , x+k, and starts at time t, then the output time of every job in that batch is t + S + (T x + T x+1 + ... + T x+k). Note that the machine outputs the results of all jobs in a batch at the same time. If the output time of job i is Oi, its cost is Oi * Fi. For example, assume that there are 5 jobs, the setup time S = 1, (T1, T2, T3, T4, T5) = (1, 3, 4, 2, 1), and (F1, F2, F3, F4, F5) = (3, 2, 3, 3, 4). If the jobs are partitioned into three batches {1, 2}, {3}, {4, 5}, then the output times (O1, O2, O3, O4, O5) = (5, 5, 10, 14, 14) and the costs of the jobs are (15, 10, 30, 42, 56), respectively. The total cost for a partitioning is the sum of the costs of all jobs. The total cost for the example partitioning above is 153. You are to write a program which, given the batch setup time and a sequence of jobs with their processing times and cost factors, computes the minimum possible total cost. Input Your program reads from standard input. The first line contains the number of jobs N, 1 <= N <= 10000. The second line contains the batch setup time S which is an integer, 0 <= S <= 50. The following N lines contain information about the jobs 1, 2,..., N in that order as follows. First on each of these lines is an integer Ti, 1 <= Ti <= 100, the processing time of the job. Following that, there is an integer Fi, 1 <= Fi <= 100, the cost factor of the job. Output Your program writes to standard output. The output contains one line, which contains one integer: the minimum possible total cost. Sample Input 5 1 1 3 3 2 4 3 2 3 1 4 Sample Output 153 题意:n个工作,编号为1到n,现在要把工作分成若干个连续但是不相交的区间。从第一个数所在的区间开始,依次完成每个区间的工作。假设某个区间包含的工作为(i到j),那么完成这个区间的工作要花的时间为(s+Ti+...Tj),s为启动时间一个常量,Ti为完成第i个工作要花的时间。设T为完成前i-1个工作所花的时间,那么完成这个区间的花费为(T+s+Ti+...Tj)*(Fi+...Fj)。求完成n个工作的最小花费。 思路:最开始用dp[i]表示1到i的最小花费,然而方程式化简出来后发现无法套到斜率优化里面%>_<%,最后发现其实dp[i]表示i到n就解决问题了( ⊙ o ⊙ )dp[i]=min(dp[j]+(s+sumt[i]-sumt[j])*sumf[i])(在i后面的所有人都会因为i造成花费,所以*sumf[i],而表示1到i时需要*(sum[i]-sum[j])就造成了展开后无法化简到斜率的形式o(>﹏<)o)然后按普通套路乘开化简,转成函数。O(∩_∩)O~~ #include<iostream> #include<cstdio> #include<algorithm> #include<cstdlib> #include<queue> #include<vector> #include<cmath> #include<cstring> #include<deque> using namespace std; const int N=12000+10; int n,s,head,tail,sum[N],c[N],dp[N]; struct node{ int x,y,pos; node(){} node(int a,int b,int c){ x=a,y=b,pos=c; } node operator - (const node &a)const{ return node(x-a.x,y-a.y,0); } int operator * (const node &a)const{ return x*a.y-y*a.x; } }now,q[N<<2]; bool check(node a,node b,int c){ return (b.y-a.y)<c*(b.x-a.x); } int main(){ while(~scanf("%d%d",&n,&s)){ for(int i=1;i<=n;i++) scanf("%d%d",&sum[i],&c[i]); for(int i=n;i>=1;i--) sum[i]+=sum[i+1],c[i]+=c[i+1]; memset(dp,0,sizeof(dp)); head=tail=0; q[tail++]=node(0,0,n+1); for(int i=n;i>=1;i--){ while(head+1<tail&&check(q[head],q[head+1],c[i])) head++; dp[i]=dp[q[head].pos]+(s+sum[i]-sum[q[head].pos])*c[i]; now=node(sum[i],dp[i],i); while(head+1<tail&&(q[tail-1]-q[tail-2])*(now-q[tail-1])<=0) tail--; q[tail++]=now; } printf("%d\n",dp[1]); } }
最新发布
08-07
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

林下的码路

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值