Help-or-else
Time Limit: 10000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 56 Accepted Submission(s): 33
Problem Description
A penal colony for finance professionals will soon be holding its annual community service activity with some rules that are considered suitable for a penal colony. Every inmate is assigned a set P of N people to help with their finances and a limit of K minutes. In addition to the circumstances of the jth person, 1 <= j <= N, a time penalty of ej for choosing not to give advice and the time duration of dj minutes allotted to provide the advice are also made clear to the inmate. An inmate starts his community service at time T equal to zero. If the inmate started working with the jth person at time T, then he must terminate his work no later than T+dj. Regardless of the validity of the advice and time of completion, a value of Cj ( = T+ dj ) is deducted from the inmate's alloted minutes. Also the inmate is not permitted to work with another person until the time T+ dj. If S is the set of people helped by an inmate, then the total number of used minutes is calculated as
Your task is to write a program to calculate the maximum number of persons that can be helped by an inmate without exceeding his K minutes limit.
Your task is to write a program to calculate the maximum number of persons that can be helped by an inmate without exceeding his K minutes limit.
Input
Input consists of sets for many inmates. The description for each inmate begins with two integers N and K, separated by a single space on a line by themselves, that represent the number of people and the maximum allowed minutes. 0 < N <= 200 and 0 < K <= 6000. Each of the following N lines contains two integers, separated by a single space, which represent the penalty and time duration one person to be assisted. All integers have values between 0 and 10000, inclusive. Input terminates with two zeros on a line by themselves.
Output
For each inmate, the output consists of a single line that contains the maximum number of persons to be helped within the given time limit using the format shown. “Mission Impossible” is entered where not exceeding the given time limit is not possible.
Sample Input
1 1000 100 1000 2 100 1000 1000 20 10 1 1 0 10000 4 293 61 30 295 39 206 27 94 85 0 0
Sample Output
1: 1 2: Mission Impossible 3: 0 4: 3
Source
//以题意要求,我们可以按第二个时间从大到小排序 //然后就从人数最多开始枚举是不是符合条件 //在计算有 m 个人可以帮助的时候 // dp[i][j] 表示 计算到第 i 个人 一共 有 j 个人可以得到帮助所用的时间最小 // 转移方程是 dp[i][j] = min( dp[i-1][j]+qe[i).e , dp[i-][j-1]+j*qe[i].d ; // j*qe[i].d 表示第 i 个人作为第 j 个人被帮助 //最后判断 dp[n][m] 是否 <= k #include<iostream> #include<cstdio> #include<cstring> #include<algorithm> #include<queue> using namespace std ; #define maxn 201 #define INF 10000000 struct node { int e , d ; }qe[maxn] ; int dp[maxn][maxn] , k ; int cmp( node a , node b ) { return a.d > b.d ; } bool check( int n , int m ) { int i , j ; for( i = 0 ; i <= n ;i++ ) for( j = 0 ; j <= m ;j++ ) dp[i][j] = INF ; dp[0][0] = 0 ; for( i = 1 ; i <= n ;i++ ) { dp[i][0] = dp[i-1][0]+qe[i].e ; for( j = 1 ; j <= m && j <= i;j++ ) { dp[i][j] = dp[i-1][j]+qe[i].e ; dp[i][j] = min( dp[i][j],dp[i-1][j-1]+j*qe[i].d) ; } } return dp[n][m] <= k ; } int main() { int i , j , n , m ,ans ,case1 = 0 , tot ; // freopen("in.txt","r",stdin) ; while( scanf("%d%d",&n , &k ) != EOF) { if( n == k && k == 0 ) break ; tot = 0 ; for( i = 1 ; i <= n ;i++ ) { scanf("%d%d" , &qe[i].e,&qe[i].d) ; tot += qe[i].e ; } sort(qe+1,qe+1+n,cmp) ; ans = -1 ; if( tot <= k ) ans = 0 ; for( i = n ; i >= 0 ;i-- ) { if(check(n,i)) { ans = i ; break ; } } if( ans == -1 )printf("%d: Mission Impossible ",++case1) ; else printf("%d: %d ",++case1,ans) ; } }