HDU 4374
居然在初始化上出错了,害我WA了好多次。。。。
View Code
1 #include<iostream> 2 #include<cstdio> 3 #include<deque> 4 #include<algorithm> 5 #define MAXN 104 6 #define MAXM 10004 7 #define MIN (1<<31) 8 using namespace std; 9 struct part { 10 int prev; 11 int p,id; 12 void ini(int a, int b, int c) 13 { 14 p = a;id = b;prev = c; 15 } 16 }data[MAXN][MAXM],temp; 17 int dp[MAXN][MAXM]; 18 int sum[MAXM]; 19 20 void prepare(int n, int m, int x, int t) 21 { 22 sum[0] = 0; 23 for (int j(1); j<=m; ++j)sum[j] = sum[j-1] + data[0][j-1].p; 24 for (int i(0); i<MAXN; ++i) 25 for (int j(0); j<MAXM; ++j) 26 dp[i][j] = MIN; 27 for (int i(0); i<m; ++i) { 28 if (abs(i-x)<=t) { 29 dp[0][i] = sum[max(i,x)+1] - sum[min(i,x)]; 30 } 31 } 32 } 33 34 int main() 35 { 36 int n,m,x,t; 37 while (scanf("%d%d%d%d",&n,&m,&x,&t) != EOF) { 38 for (int i(0); i<n; ++i) { 39 for (int j(0); j<m; ++j) { 40 scanf("%d",&data[i][j].p); 41 data[i][j].id = j; 42 } 43 } 44 --x; 45 prepare(n,m,x,t); 46 for (int i(1); i<n; ++i) { 47 sum[0] = 0; 48 for (int j(1); j<=m; ++j)sum[j] = sum[j-1] + data[i][j-1].p; 49 int fi(0),se(0),k(0); 50 deque<part> Q; 51 for (int j(0); j<m; ++j) { 52 fi = j; 53 while (!Q.empty() && Q.front().id < j)Q.pop_front(); 54 for (; k<m && j+t>=k; ++k) { 55 if (dp[i-1][k] == MIN)continue; 56 temp.ini(dp[i-1][k]+sum[k+1]-sum[j],k,j); 57 if (!Q.empty())se = Q.back().prev; 58 while (!Q.empty() && Q.back().p - (sum[fi] - sum[se]) < temp.p) { 59 Q.pop_back(); 60 if (!Q.empty())se = Q.back().prev; 61 } 62 Q.push_back(temp); 63 } 64 if (!Q.empty())se = Q.front().prev; 65 if (!Q.empty())dp[i][j] = max(dp[i][j],Q.front().p - (sum[fi] - sum[se])); 66 } 67 k = 0;Q.clear(); 68 for (int j(0); j<m; ++j) { 69 fi = j + 1; 70 while (!Q.empty() && Q.front().id + t < j)Q.pop_front(); 71 for (; k<=j; ++k) { 72 if (dp[i-1][k] == MIN)continue; 73 temp.ini(dp[i-1][k]+sum[j+1]-sum[k],k,j); 74 if (!Q.empty())se = Q.back().prev + 1; 75 while (!Q.empty() && Q.back().p + sum[fi] - sum[se] < temp.p) { 76 Q.pop_back(); 77 if (!Q.empty())se = Q.back().prev + 1; 78 } 79 Q.push_back(temp); 80 } 81 if (!Q.empty())se = Q.front().prev + 1; 82 if (!Q.empty())dp[i][j] = max(dp[i][j],Q.front().p + sum[fi] - sum[se]); 83 } 84 } 85 int ans(MIN); 86 for (int i(0); i<m; ++i) { 87 ans = max(dp[n-1][i],ans); 88 } 89 cout<<ans<<endl; 90 } 91 return 0; 92 }