APIO2009——oil

简直就是一个暴力动归,太无语了……比noip2010tortoise还暴力。

以前我一直以为,for最多的一个程序是以前我写过的一个tarjan缩点+dp的题,但是,这个题改变了我对for的看法,太多了……

话说这个题,我一开始暴力实现用了10.xx秒,然后看了前面的编译,用了-O2开关,速度达到了7.74s,然后优化读入,没开编译开关,总速度达到了5.xx秒,最后两个一起上,总速度3.24s,秒掉一切Pascal!

开始他们还说要出一个题,用巨型读入让C/C++TLE掉,如今看起来,有点困难……

要怪也只能怪scanf/cin,cin在编译时确定类型,运行时速度自然快,可是却被嵌入stl中,实现地极其低效,十分无语,至于scanf,要在运行时确定类型。这样有好处,就是对于不确定性输入,当然,坏处就是拖累了速度。有人说,ios::sync_with_stdio(0)可以让cin/cout速度快一点,可事实上可没多少优化,还是没有scanf快,我觉得stl太注重特性,各种特性都有,反而拖累了速度。其实,我觉得C++对我而言比较有吸引力的就是引用传参……很无语吧,那些用pascal/C++的同志,C里面没用变量传参,一切只能用指针来代替,很无语……

算了,以后还是多注意各个输入吧……

附:APIO2009OIL的AC代码,看还是好看:

#include #include #define maxn 2000 int map[maxn + 3][maxn + 3]; int sum[maxn + 3][maxn + 3]; int LL[maxn + 3][maxn + 3]; int LR[maxn + 3][maxn + 3]; int RL[maxn + 3][maxn + 3]; int RR[maxn + 3][maxn + 3]; int X[maxn + 3]; int Y[maxn + 3]; char cmd[maxn * 5], *token; inline int max(int i, int j) { if (i > j) return i; return j; } int main(int argc, char *argv[]) { FILE *fin = fopen("oil.in", "r"); FILE *fout = fopen("oil.out", "w"); int m, n, k, i, j, ans = 0, L, U, M; fgets(cmd, sizeof(cmd), fin), sscanf(cmd, "%d%d%d", &m, &n, &k); for (i = 1; i <= m; ++i) { fgets(cmd, sizeof(cmd), fin); for (token = strtok(cmd, " "), j = 0; token; token = strtok(0, " ")) { map[i][++j] = atoi(token); sum[i][j] = sum[i - 1][j] + sum[i][j - 1] - sum[i - 1][j - 1] + map[i][j]; } } --k, n -= k, m -= k; for (i = 1; i <= m; ++i) for (j = 1; j <= n; ++j) map[i][j] = sum[i + k][j + k] + sum[i - 1][j - 1] - sum[i + k][j - 1] - sum[i - 1][j + k]; for (i = 1; i <= m; ++i) for (j = 1; j <= n; ++j) X[i] = max(X[i], map[i][j]); for (j = 1; j <= n; ++j) for (i = 1; i <= m; ++i) Y[j] = max(Y[j], map[i][j]); for (i = 1; i <= m; ++i) for (j = 1; j <= n; ++j) LL[i][j] = max(map[i][j], max(LL[i - 1][j], LL[i][j - 1])); for (i = 1; i <= m; ++i) for (j = n; j; --j) LR[i][j] = max(map[i][j], max(LR[i - 1][j], LR[i][j + 1])); for (i = m; i; --i) for (j = 1; j <= n; ++j) RL[i][j] = max(map[i][j], max(RL[i + 1][j], RL[i][j - 1])); for (i = m; i; --i) for (j = n; j; --j) RR[i][j] = max(map[i][j], max(RR[i + 1][j], RR[i][j + 1])); for (i = m - k; i; --i) for (j = n - k; j; --j) { ans = max(ans, LL[i][j] + LR[i][j + k + 1] + RR[i + k + 1][1]); ans = max(ans, LL[i][j] + RL[i + k + 1][j] + RR[1][j + k + 1]); } for (i = k + 1; i <= m; ++i) for (j = k + 1; j <= n; ++j) { ans = max(ans, RR[i][j] + LR[i - k - 1][j] + LL[m][j - k - 1]); ans = max(ans, RR[i][j] + RL[i][j - k - 1] + LL[i - k - 1][n]); } for (i = 1, U = X[i]; i <= m - k; U = max(U, X[++i])) for (j = i + k + 1, M = X[j]; j <= m - k; M = max(M, X[++j])) ans = max(ans, U + M + RR[j + k + 1][1]); for (j = 1, L = Y[j]; j <= n - k; L = max(L, Y[++j])) for (i = j + k + 1, M = Y[i]; i <= n - k; M = max(M, Y[++i])) ans = max(ans, L + M + RR[1][i + k + 1]); fprintf(fout, "%d/n", ans); fclose(fin); fclose(fout); return 0; }  

你可能感兴趣的:(APIO2009——oil)