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 |
所有的能找到的测试数据都过了,但还是WA,请大家看看,谢谢#include <iostream> #include <vector> #define IO_MODE 0 //iostream //#define IO_MODE 1 //fstream #if 1==IO_MODE #include <fstream> #endif //#define DEBUG using namespace std; typedef struct { int value; int len; }RLE; typedef struct { int x; int y; }POS; typedef struct { size_t cur; int cur_elem; }OFFSET; /************************************************************************ 功能: A.生成、销毁二维数组; B.可以获得上中下层的指针; C.移动层(指针移动,填充high_lay层) 注意: 自动填充周边数据为-1,当遇到时认为该数和任意相遇的数相等 ************************************************************************/ class lay_manager { public: lay_manager(vector<RLE> *r, int line_len); ~lay_manager(); void mov_lay(int num=1); int get_value_by_pos(int x, int y, int value); int *low_lay; int *mid_lay; int *high_lay; #ifdef DEBUG void print_vec()const; #endif OFFSET rles_offset; private: vector<RLE> *rles; int get_next_value(); int lay_len; }; #ifdef DEBUG void lay_manager::print_vec() const { int *p=NULL; cout << "------------------------------------------" <<endl; for (int x=0; x<3; x++) { if (0==x) { p=low_lay; } else if (1==x) { p=mid_lay; } else { p=high_lay; } for (int y=0; y<lay_len; y++) { cout << p[y]<< "\t"; } cout << endl; } } #endif //DEBUG int lay_manager::get_value_by_pos(int x, int y, int value) { int tmp=0; if (-1==value) { return -1; } switch (x) { case 0: tmp = low_lay[y]; break; case 1: tmp = mid_lay[y]; break; case 2: tmp = high_lay[y]; break; default: ; } if (-1==tmp) { if (-2==value) { return -2; } return value; } else { return tmp; } } lay_manager::lay_manager(vector<RLE> *r, int line_len) { rles = r; rles_offset.cur=0; rles_offset.cur_elem=0; lay_len = line_len+2; low_lay = new int[lay_len]; memset(low_lay, 0, lay_len); this->low_lay[0] = -1; this->low_lay[lay_len-1] = -1; mid_lay = new int[lay_len]; memset(mid_lay, 0, lay_len); this->mid_lay[0] = -1; this->mid_lay[lay_len-1] = -1; high_lay = new int[lay_len]; memset(high_lay, 0, lay_len); this->high_lay[0] = -1; this->high_lay[lay_len-1] = -1; int i=0; //low for (i=1; i<lay_len-1; i++) { this->low_lay[i]=-1; } //mid for (i=1; i<lay_len-1; i++) { this->mid_lay[i]=get_next_value(); } //high for (i=1; i<lay_len-1; i++) { this->high_lay[i]=get_next_value(); } #ifdef DEBUG cout << "原始" << endl; this->print_vec(); #endif // DEBUG } int lay_manager::get_next_value() { if (rles_offset.cur>=rles->size()) { return -1; } if (rles_offset.cur_elem==rles->at(rles_offset.cur).len) { rles_offset.cur++; rles_offset.cur_elem=0; } if (rles_offset.cur>=rles->size()) { return -1; } rles_offset.cur_elem++; return rles->at(rles_offset.cur).value; } /************************************************************************/ //low->mid, mid->high , high->new */ /************************************************************************/ void lay_manager::mov_lay(int num) { int i=0; if (num == 1) { int *t=low_lay; low_lay=mid_lay; mid_lay = high_lay; high_lay = t; //填充新的一层 for (i=1; i<lay_len-1; i++) { this->high_lay[i]=get_next_value(); } } else { this->rles_offset.cur_elem+=(this->lay_len-2)*(num-1); //low for (i=1; i<lay_len-1; i++) { this->low_lay[i]=get_next_value(); } //mid for (i=1; i<lay_len-1; i++) { this->mid_lay[i]=get_next_value(); } //high for (i=1; i<lay_len-1; i++) { this->high_lay[i]=get_next_value(); } } #ifdef DEBUG cout<<"\nmov happened...." << endl; this->print_vec(); #endif // DEBUG } lay_manager::~lay_manager() { delete [] low_lay; delete [] mid_lay; delete [] high_lay; } #define ABS(x) (((x)>0)?(x):-(x)) #define MAX(a,b) (((a)>(b))?(a):(b)) /************************************************************************ ******************* (-1,-1) (-1,+0) (-1,+1) (+0,-1) (+0,+0) (+0,+1) (+1,-1) (+1,+0) (+1,+1) ******************* (x,y) x为行 y为列 ************************************************************************/ int calc(POS p, lay_manager &lays) { int value = lays.get_value_by_pos(p.x, p.y, -2); if (-2==value) { return -2; } int abs00 = ABS(lays.get_value_by_pos(p.x-1, p.y-1, value)-value); int abs01 = ABS(lays.get_value_by_pos(p.x-1, p.y+0, value)-value); int abs02 = ABS(lays.get_value_by_pos(p.x-1, p.y+1, value)-value); int abs10 = ABS(lays.get_value_by_pos(p.x+0, p.y-1, value)-value); int abs12 = ABS(lays.get_value_by_pos(p.x+0, p.y+1, value)-value); int abs20 = ABS(lays.get_value_by_pos(p.x+1, p.y-1, value)-value); int abs21 = ABS(lays.get_value_by_pos(p.x+1, p.y+0, value)-value); int abs22 = ABS(lays.get_value_by_pos(p.x+1, p.y+1, value)-value); int max=MAX(abs00, abs01); max = MAX(max, abs02); max = MAX(max, abs10); max = MAX(max, abs12); max = MAX(max, abs20); max = MAX(max, abs21); max = MAX(max, abs22); return max; } size_t calc_count(vector<RLE> *v, size_t line_len) { size_t size=0; for (size_t i=0; i<v->size(); i++) { size += v->at(i).len; } if (size%line_len==0) { return size/line_len; } else { return size/line_len+1; } } #define PRESS_DATA void process(vector<RLE> *rles, size_t line_len, vector<RLE> *result) { int tmp=-1; int last=-1; lay_manager lays(rles, line_len); //打印三层 //c长度为实际层数 size_t c=calc_count(rles, line_len); for(size_t lay=0; lay<c; lay++) { //计算第二层 POS pos; int x=1; for (size_t y=1; y<=line_len;y++) { pos.x = x; pos.y = y; tmp = calc(pos, lays); if (-2==tmp) { break; } #ifdef DEBUG cout << tmp << endl; #endif // DEBUG if (tmp!=last) { RLE rle={tmp, 1}; result->push_back(rle); last = rle.value; } else { result->at(result->size()-1).len++; } } #ifdef PRESS_DATA //压缩计算器 //功能:计算要跳过计算的层数,填充结果数据 if (lays.rles_offset.cur<rles->size()) { int cur = 0; //记录high层的开头元素的数组位置 size_t cur_elem =lays.rles_offset.cur_elem; cur_elem = cur_elem - line_len+1; if(cur_elem<0) { cur = lays.rles_offset.cur-1; cur_elem = rles->at(cur).len-ABS(cur_elem); } else { cur=lays.rles_offset.cur; cur_elem=cur_elem; } size_t head=1; size_t rear=rles->at(cur).len; if ((cur_elem>=head+line_len)&&(cur_elem<=rear-line_len)) { int m_lay=(rear-line_len-(cur_elem-1))/line_len; if (0!=m_lay) { #ifdef DEBUG cout << "计划跳过" << m_lay << "层" << endl; #endif // DEBUG lays.rles_offset.cur = cur; lays.rles_offset.cur_elem=cur_elem-1; #ifdef DEBUG cout << "移动offset到上一行:" << "\t"; cout << lays.rles_offset.cur << ' '<< lays.rles_offset.cur_elem << endl; #endif // DEBUG lay += (m_lay); lays.mov_lay(m_lay); #ifdef DEBUG cout << "移动后offset:" << "\t"; cout << lays.rles_offset.cur << ' '<< lays.rles_offset.cur_elem << endl; #endif // DEBUG if (0!=last) { RLE rle={0, m_lay*line_len}; result->push_back(rle); last = rle.value; } else { result->at(result->size()-1).len += m_lay*line_len; } continue; } } } #endif //PRESS_DATA lays.mov_lay(1); } } void print_vector(const vector<RLE> &rle) { size_t size=rle.size(); for(size_t i=0; i<size; i++) { cout << rle.at(i).value<<" "; cout << rle.at(i).len << endl; } } int main() { #if 1==IO_MODE fstream file("1009.txt",ios::in); #endif size_t line_len; vector<RLE> rles; while(1) { #if 1==IO_MODE file >> line_len; #else cin >> line_len; #endif if (0==line_len) { break; } rles.clear(); while (1) { RLE item; #if 1==IO_MODE file >> item.value >> item.len; #else cin >> item.value >> item.len; #endif if (0==item.value&&0==item.len) { break; } rles.push_back(item); } vector<RLE> result; process(&rles, line_len, &result); cout << line_len << endl; print_vector(result); 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