求助:两道简单的ACM题,老是WA
http://acm.hdu.edu.cn/showproblem.php?pid=2571
简单的DP
#include <iostream>using namespace std;int main(){ const int MIN = -1000; int arr[21][1001], sum[21][1001]; int count, n, m, max, i, j, k; scanf("%d", &count); while(count--) { scanf("%d %d", &n, &m); for(i=1;i<=n;i++) for(j=1;j<=m;j++) scanf("%d", &arr[i][j]); for(i=1;i<=m;i++) sum[0][i] = MIN; for(i=1;i<=n;i++) sum[i][0] = MIN; for(i=1;i<=n;i++) { for(j=1;j<=m;j++) { if(1==i && 1==j) { sum[1][1] = arr[1][1]; continue; } max = MIN; if(max < sum[i-1][j]) max = sum[i-1][j]; if(max < sum[i][j-1]) max = sum[i][j-1]; for(k=1;k<=m/2;k++) if(0==m%k && max<sum[i][k]) max = sum[i][k]; sum[i][j] = max + arr[i][j]; } } printf("%d\n", sum[n][m]); } return 0;}
#include <iostream>using namespace std;const int SIZE = 1002;int tree[SIZE][SIZE], arr[SIZE][SIZE];int Lowbit(int x){ return x & (-x); }void Change(int i, int j, int value);int Sum(int i, int j);void Swap(int &a, int &b);int main(){ int count, i, j, k, n; int x, y, x2, y2, sum, value; char ch; scanf("%d", &count); for(k=1;k<=count;k++) { for(i=0;i<SIZE;i++) { for(j=0;j<SIZE;j++) { tree[i][j] = Lowbit(i) * Lowbit(j); arr[i][j] = 1; } } getchar(); scanf("%d", &n); printf("Case %d:\n", k); while(n--) { cin >> ch; if('S' == ch) { scanf("%d %d %d %d", &x, &y, &x2, &y2); if(x > x2) Swap(x, x2); if(y > y2) Swap(y, y2); sum = Sum(x2+1, y2+1) - Sum(x, y2+1) - Sum(x2+1, y) + Sum(x, y); printf("%d\n", sum); } else if('A' == ch) { scanf("%d %d %d", &x, &y, &value); arr[x+1][y+1] += value; Change(x+1, y+1, value); } else if('D' == ch) { scanf("%d %d %d", &x, &y, &value); if(arr[x+1][y+1] < value) value = arr[x+1][y+1]; arr[x+1][y+1] -= value; Change(x+1, y+1, -value); } else if('M' == ch) { scanf("%d %d %d %d %d", &x, &y, &x2, &y2, &value); if(value < arr[x+1][y+1]) value = arr[x+1][y+1]; arr[x+1][y+1] -= value; arr[x2+1][y2+1] += value; Change(x+1, y+1, -value); Change(x2+1, y2+1, value); } } } return 0;}void Swap(int &a, int &b){ int temp = a; a = b; b = temp;}void Change(int i, int j, int value){ int temp; while(i < SIZE) { temp = j; while(temp < SIZE) { tree[i][temp] += value; temp += Lowbit(temp); } i += Lowbit(i); }}int Sum(int i, int j){ int sum, temp; sum = 0; while(i > 0) { temp = j; while(temp > 0) { sum += tree[i][temp]; temp -= Lowbit(temp); } i -= Lowbit(i); } return sum;}
#include <iostream>using namespace std;int n,m; //行 列int **luck; //幸运值int **max_luck;void get_max_luck(){ cin>>n>>m; luck=new int* [n+1]; max_luck=new int* [n+1]; for(int cnt=0;cnt<=n;++cnt) { luck[cnt]=new int[m+1]; max_luck[cnt]=new int[m+1]; } for(int x=1;x<=n;++x) { for(int y=1;y<=m;++y) { cin>>luck[x][y]; max_luck[x][y]=-100000; } } // dp max_luck[n][m]=luck[n][m]; for(int x=n;x>=1;--x) { for(int y=m;y>=1;--y) { if(x==n&&y==m) { continue; } if(x+1<=n) { max_luck[x][y]=max_luck[x+1][y]; } if(y+1<=m) { if(max_luck[x][y+1]>max_luck[x][y]) { max_luck[x][y]=max_luck[x][y+1]; } } for(int k=2;k*y<=m;++k) { if(max_luck[x][k*y]>max_luck[x][y]) { max_luck[x][y]=max_luck[x][k*y]; } } max_luck[x][y]+=luck[x][y]; } } cout<<max_luck[1][1];}int main(){ get_max_luck(); return 0;}
[解决办法]
1892题:
if(value < arr[x+1][y+1])
value = arr[x+1][y+1];
改为
if(value > arr[x+1][y+1])
value = arr[x+1][y+1];
[解决办法]
ACM伤透了我的心啊,还是顶一下楼主``