zoj1483 dp

【题解】:

【代码】:

 1 #include <iostream>
 2 #include <string.h>
 3 #include <stdio.h>
 4 #define LL long long
 5 using namespace std;
 6 
 7 bool dp[505][505];//已经用了i个人,已分配出j个任务
 8 LL S[505];
 9 
10 int N,M;
11 LL K;
12 int main(){
13     while(~scanf("%d%d%lld",&N,&M,&K)){
14         S[0]=0;
15         for(int i=1;i<=M;i++){
16             LL x;
17             scanf("%lld",&x);
18             S[i]=S[i-1]+x;
19         }
20         LL L,R;
21         if (S[M] % N == 0){
22             L = S[M]/N-K;
23             if (L<0) L=0;
24         }else {
25             L = S[M]/N-K+1;
26             if (L<0) L=0;
27         }
28         R=S[M]/N+K;
29         //划分区间是[L,R]
30         memset(dp,0,sizeof(dp));
31         dp[0][0]=true;
32         for(int i=1;i<=N;i++){
33             for(int j=1;j<=M;j++){
34                 //保证 S[j]-S[k] = A[k+1]+A[k+2]....A[j] 在 [L,R]的范围内
35                 for(int k=j;S[j]-S[k]<=R && k>=0;k--){
36                     if(S[j]-S[k]<L) continue;//去除前面不可行的
37                     if (dp[i-1][k]) dp[i][j]=true;
38                 }
39 
40             }
41         }
42         if (dp[N][M]) printf("possible\n");else printf("impossible\n");
43     }
44     return 0;
45 }
View Code

 

posted @ 2014-06-10 02:30  little_w  阅读(155)  评论(0编辑  收藏  举报