One dynamic programming training

A dynamic programming topics

https://www.luogu.org/problem/P2577

analysis:

First, this question is not a greedy sort that dp

Then the data range <= 200, it may be the only dp of

Consider this question, thinking that I should try to make those easy Dafan fast eat the slow but the top surface

And because all of the total time Dafan is certain,

That no matter how everyone finished dinner arrangements would spend so much time

Therefore, we consider only the top surface of the slow food must be the best

But this time there are two windows, how to arrange the necessary dp

  • A [i, j] F prior to recording individual queue i, the first team with the case where j is the maximum use time.

  • The second team does not record the reason may be launched by the state is the first state to the team.

  • For ease of calculation, it is recommended to make use prefixes and maintain it, can be more simple to calculate when the second team.

  • Then we got this equations:

    In the case of adding the first queue: F [I, J] = min (F [I, J], max (F [-I. 1, JA [I] .x], J + A [I] .y)) ;

    The current minimum is the maximum time a person with the time and personal time

    Similarly adding a second team

    f [i, j] = max (f [i-1, j], a [i] .y + b [i] -j) where b [i] for the i individual line before the total time used

    However, 200 * 40,000 seems a bit big, dimension reduction chant!

    Similar backpack as dimensionality reduction on the line

    code by std:

#include<bits/stdc++.h>
using namespace std;
struct lsg{int x,y;}a[1000];
int n,f[400001],sum,ans,b[1000];
bool pd(lsg x,lsg y){return x.y>y.y;}
int main(){
    ios::sync_with_stdio(false);
    cin>>n;
    for (int i=1;i<=n;i++)cin>>a[i].x>>a[i].y;
    sort(a+1,a+1+n,pd);memset(f,10,sizeof(f));f[0]=0;
    for (int i=1;i<=n;i++)b[i]=a[i].x+b[i-1];
    for (int i=1;i<=n;i++){
            for (int j=sum;j>=0;j--){
                f[j+a[i].x]=min(f[j+a[i].x],max(f[j],a[i].y+j+a[i].x));//将i加入第一个队列
                    f[j]=max(f[j],a[i].y+b[i]-j);//将i加入第二个队列
                }
            sum+=a[i].x;
        }
    ans=1e9;
    for (int i=1;i<=sum;i++)ans=min(ans,f[i]);
    cout<<ans<<endl;
}

Guess you like

Origin www.cnblogs.com/wzxbeliever/p/11622442.html
Recommended