【洛谷2577】[ZJOI2005] 午餐(较水DP)

点此看题面

大致题意:(N)个学生去食堂打饭,每个学生有两个属性:打饭时间(a_i)和吃饭时间(b_i)。现要求将这些学生分成两队分别打饭,求最早何时所有人吃完饭。

贪心

首先,依据贪心的思想,肯定是吃饭时间长的先打饭,因此可以将其按吃饭时间先排序预处理一遍。

如何(DP)

贪心完,就是(DP)了。

个人认为三维(DP)还是非常好想的:用(f_{i,j,k})表示当前轮到第(i)个学生、在两个窗口打饭分别用了(j)(k)个单位时间时最早吃完的时间

其实,确定了(i)(j),此时可以唯一确定(k=sum_i-j)(sum_i=sum_{x=1}^ia_x)),故可以将原(DP)删去一维,变成二维(DP)

关于状态转移

状态转移应该也是挺容易想到的,可以分将第(i)个学生放在(1)号窗口还是(2)号窗口讨论。

  • 放在(1)号窗口:此时的时间为(max(f_{i-1,j-a_i},j+b_i))
  • 放在(2)号窗口:此时的时间为(max(f_{i-1,j},sum_i-j+b_i))

大致思路就是如此。

代码

#include<bits/stdc++.h>
#define max(x,y) ((x)>(y)?(x):(y))
#define min(x,y) ((x)<(y)?(x):(y))
#define uint unsigned int
#define LL long long
#define ull unsigned long long
#define swap(x,y) (x^=y,y^=x,x^=y)
#define abs(x) ((x)<0?-(x):(x))
#define INF 1e9
#define Inc(x,y) ((x+=(y))>=MOD&&(x-=MOD))
#define ten(x) (((x)<<3)+((x)<<1)) 
#define N 200
using namespace std;
int n,sum[N+5];
struct student
{
    int x,y;
    inline bool operator < (const student Cmp) const
    {
        return y>Cmp.y;
    }
}s[N+5]; 
class FIO
{
    private:
        #define Fsize 100000
        #define tc() (FinNow==FinEnd&&(FinEnd=(FinNow=Fin)+fread(Fin,1,Fsize,stdin),FinNow==FinEnd)?EOF:*FinNow++)
        #define pc(ch) (FoutSize<Fsize?Fout[FoutSize++]=ch:(fwrite(Fout,1,FoutSize,stdout),Fout[(FoutSize=0)++]=ch))
        int f,FoutSize,OutputTop;char ch,Fin[Fsize],*FinNow,*FinEnd,Fout[Fsize],OutputStack[Fsize];
    public:
        FIO() {FinNow=FinEnd=Fin;}
        inline void read(int &x) {x=0,f=1;while(!isdigit(ch=tc())) f=ch^'-'?1:-1;while(x=ten(x)+(ch&15),isdigit(ch=tc()));x*=f;}
        inline void read_char(char &x) {while(isspace(x=tc()));}
        inline void read_string(string &x) {x="";while(isspace(ch=tc()));while(x+=ch,!isspace(ch=tc())) if(!~ch) return;}
        inline void write(int x) {if(!x) return (void)pc('0');if(x<0) pc('-'),x=-x;while(x) OutputStack[++OutputTop]=x%10+48,x/=10;while(OutputTop) pc(OutputStack[OutputTop]),--OutputTop;}
        inline void write_char(char x) {pc(x);}
        inline void write_string(string x) {register int i,len=x.length();for(i=0;i<len;++i) pc(x[i]);}
        inline void end() {fwrite(Fout,1,FoutSize,stdout);}
}F;
class Class_DP
{
    private:
        int f[N+5][N*N+5];
    public:
        inline int GetAns()
        {
            register int i,j,ans=INF;
            for(i=1;i<=n;++i)
            {
                for(j=0;j<=sum[i];++j) f[i][j]=min(sum[i]-j>=s[i].x?max(f[i-1][j],(sum[i]-j)+s[i].y):INF,j>=s[i].x?max(f[i-1][j-s[i].x],j+s[i].y):INF);//分两种情况讨论并转移
                for(;j<=sum[n];++j) f[i][j]=INF;//对不可能出现的情况直接赋值为INF    
            }
            for(i=0;i<=sum[n];++i) ans=min(ans,f[n][i]);//取min
            return ans;
        }
}DP;
int main()
{
    register int i;
    for(F.read(n),i=1;i<=n;++i) F.read(s[i].x),F.read(s[i].y);
    for(sort(s+1,s+n+1),i=1;i<=n;++i) sum[i]=sum[i-1]+s[i].x;//贪心的思想,将原数组排序,然后求出前缀和
    return F.write(DP.GetAns()),F.end(),0;
}
原文地址:https://www.cnblogs.com/chenxiaoran666/p/Luogu2577.html