Like pressure dp - HDU 1074

Title meaning

Courses are given a bunch of cut-off time and the time required to complete

Each completed over the course of time the deadline day deducted one point

Ask how to arrange completion of points deducted least of all classes

Topic analysis

We can see a total of not more than 16 courses 

Suppose there are three courses, with 111 denotes completed, 000 means no complete, then this problem can be completed like pressure dp

With dp [1] ... dp [1 << n-1] n represents 2 ^ n types of course completion state corresponding to fractional subtraction

With J = 1 << (k-1), when J & x is not 0, x represents a k-th bit is not 0, i.e. the complete course of K

Thus dp [x] = min (dp [x], dp [xJ] + t [xJ] + fin [j] -dea [j])

Your current represented by [points] and [did not finish the course] k-points plus [completed] k courses deduction of points whichever is less

As for how to complete the count k [min] courses buckle (t [xJ] + fin [j] -dea [j])

t [i] at time i represents the binary state

t [i] plus the time to complete, if it exceeds deadline, yet not overtime, it is necessary points

But if you do not time out, you can not deduct negative points, it is set to 0

Topic Code

 #include<iostream>
#include<stdio.h>
#include<algorithm>
#include<string.h>
using namespace std;
typedef long long LL;
const int maxn=(1<<16)+7;
const int INF=0x3f3f3f3f;
int T,n;
int dea[20],fin[20],dp[maxn],pre[maxn],t[maxn];
char s[20][100];
void print(int x){
    if(x==0)return;
    print(x-(1<<pre[x]));
    printf("%s\n",s[pre[x]]);
}
int main(){
    scanf("%d",&T);
    while(T--){
        scanf("%d",&n);
        for(int i=0;i<n;i++)
            scanf("%s%d%d",&s[i],&dea[i],&fin[i]);
        int bit=1<<n;
        for(int i=1;i<bit;i++){
            dp[i]=INF;
            for(int j=n-1;j>=0;j--){
                int temp=1<<j;
                if(!(temp&i))continue;
                int score=t[i-temp]+fin[j]-dea[j];
                if(score<0)score=0;
                if(dp[i]>dp[i-temp]+score){
                    dp[i]=dp[i-temp]+score;
                    t[i]=t[i-temp]+fin[j];
                    pre[i]=j;
                }
            }
        }
        printf("%d\n",dp[bit-1]);
        print(bit-1);
    }
    return 0;
}

 

Guess you like

Origin www.cnblogs.com/helman/p/11240961.html