Online Judge | Problem Set | Authors | Online Contests | User | ||||||
---|---|---|---|---|---|---|---|---|---|---|
Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest |
Re:对于运行超时的两种情况做分析 附上java代码In Reply To:对于运行超时的两种情况做分析 附上java代码 Posted by:smiledrown at 2013-05-10 22:27:37 一般化要考虑数值全部相同到块到情形. 比如 AAAAAA XXXXXX BBBBBB X若超过一行,还需要A=B 同附上AC代码. #include <cstdio> #include <cstdlib> #include <vector> #include <algorithm> using namespace std; vector<pair<int, int> > res; static void add_res(int &n, int value, int c) { // printf("add %d %d\n", value, c); if (n == 0) res.push_back(make_pair(value, c)), n++; else if (res[n-1].first == value) res[n-1].second += c; else res.push_back(make_pair(value, c)), n++; } static bool judge(int row, int col, int i, int handled, vector<pair<int, int> > &vec, int c, int t_row) { if (col == 0 && vec[i].second >= c) { if ((row == 0 || (handled >= c || (handled == 0 && vec[i-1].second >= c))) //up is same && (row == t_row - 1 || (vec[i].second % c == 0 && (vec[i].second > c || vec[i+1].second >= c)))) // down is same return true; } return false; } static void solve(int c, int t_row, vector<pair<int, int> > &vec) { int row, col, n; row = col = n = 0; for (int i = 0; i < vec.size(); i++) { int handled = 0; pair<int, int> &p = vec[i]; int second = p.second; while (handled < second) { if (judge(row, col, i, handled, vec, c, t_row)) { int up = handled >= c ? p.first : i > 0 ? vec[i-1].first : p.first; int down = p.second >= 2 * c ? p.first : (i == vec.size() - 1 ? p.first : vec[i+1].first); int value = max(abs(p.first - up), abs(p.first - down)); add_res(n, value, c); handled += c; p.second -= handled; row++; if (p.second >= 2 * c) { int cl = (p.second - c) / c; add_res(n, 0, cl * c); handled += cl * c; p.second -= cl * c; row += cl; } } else { // oops, one by one; for (; col < c && handled < second; col++, handled++, vec[i].second--) { int value = 0; if (row > 0) { if (col < c -1 && handled < c - 1) { //up-right int j = i-1, k = handled; while ((k += vec[j].second) < c -1) j--; value = max(value, abs(p.first - vec[j].first)); } if (handled < c) { int j = i-1, k = handled; while ((k += vec[j].second) < c) j--; value = max(value, abs(p.first - vec[j].first)); } if (col > 0 && handled < c + 1) { int j = i-1, k = handled; while ((k += vec[j].second) < c + 1) j--; value = max(value, abs(p.first - vec[j].first)); } } if (handled == 0 && col > 0) value = max(value, abs(p.first - vec[i-1].first)); if (i < vec.size() - 1) { if (col < c - 1 && handled + 1 == second) value = max(value, abs(p.first - vec[i+1].first)); } if (row < t_row - 1) { int left = second - handled - 1; if (col > 0 && left < c - 1) { int j = i+1, k = left; while ((k += vec[j].second) < c - 1) j++; value = max(value, abs(p.first - vec[j].first)); } if (left < c ) { int j = i+1, k = left; while ((k += vec[j].second) < c) j++; value = max(value, abs(p.first - vec[j].first)); } if (col < c - 1 && left < c + 1) { int j = i+1, k = left; while ((k += vec[j].second) < c + 1) j++; value = max(value, abs(p.first - vec[j].first)); } } add_res(n, value, 1); } if (col == c) col = 0, row++; } } vec[i].second = second; } for (int i = 0; i < res.size(); i++) printf("%d %d\n", res[i].first, res[i].second); res.clear(); } int main() { while (true) { int c; scanf("%d", &c); if (!c) break; vector<pair<int, int> > vec; vec.reserve(1000 + 10); int t_row = 0; while (true) { int v, l; scanf("%d %d", &v, &l); if (!v && !l) break; vec.push_back(make_pair(v, l)); t_row += l; }; t_row /= c; printf("%d\n", c); solve(c, t_row, vec); printf("0 0\n"); } printf("0\n"); return 0; } Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator