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 |
又写了一个跳跃式编程的版本原来自己写了一个版本,虽然AC了但是过程很复杂,最后看了这位大神(http://blog.csdn.net/lyy289065406/article/details/6648671)的方法以后,又用跳跃式编程写了一个版本,果然简单多了。 //problem 1009 #include <iostream> #include <vector> #include <algorithm> #include <cmath> using namespace std; struct rle { int value; int length; int index; }; struct edge { int value; int pos; bool operator<(const edge& other) { return pos < other.pos; } }; int height, width, n; vector<rle> v_in; vector<edge> v_edge; int get_value(int k, int i, int j) { int index = i*width + j; while(index < v_in[k].index) --k; while(index >= v_in[k].index + v_in[k].length) ++k; return v_in[k].value; } int get_edge(int k, int row, int col) { int local_value = get_value(k, row, col); int res = 0; for(int i = row - 1; i <= row + 1; ++i) for(int j = col - 1; j <= col + 1; ++j) { if(i < 0 || i >= height || j < 0 || j >= width) continue; int value = get_value(k, i, j); res = max(res, abs(value - local_value)); } return res; } int main() { while(cin >> width && width) { v_in.clear(); v_edge.clear(); int value, length, index = 0; while(cin >> value >> length && value + length) { rle item; item.value = value; item.length = length; item.index = index; v_in.push_back(item); index += length; } n = v_in[v_in.size() - 1].index + v_in[v_in.size() - 1].length; height = n/width; for(int k = 0; k < v_in.size(); ++k) { int row = v_in[k].index/width; int col = v_in[k].index%width; for(int i = row - 1; i <= row + 1; ++i) for(int j = col - 1; j <= col + 1; ++j) { if(i < 0 || i >= height || j < 0 || j >= width) continue; int res = get_edge(k, i, j); int index = i*width + j; edge item; item.value = res; item.pos = index; v_edge.push_back(item); } } int row = height - 1; int col = 0; int k = v_in.size() - 1; for(int i = row - 1; i <= row + 1; ++i) for(int j = col - 1; j <= col + 1; ++j) { if(i < 0 || i >= height || j < 0 || j >= width) continue; int res = get_edge(k, i, j); int index = i*width + j; edge item; item.value = res; item.pos = index; v_edge.push_back(item); } sort(v_edge.begin(), v_edge.end()); cout << width << endl; edge temp = v_edge[0]; for(int i = 1; i < v_edge.size(); ++i) { if(v_edge[i].value == temp.value) continue; cout << temp.value << " " << v_edge[i].pos - temp.pos << endl; temp = v_edge[i]; } cout << temp.value << " " << n - temp.pos << endl; cout << "0 0 " << endl; } cout << "0" << endl; return 0; } Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator