北大ACM(POJ1009-Edge Detection)

时间:2023-03-09 05:06:47
北大ACM(POJ1009-Edge Detection)
Question:http://poj.org/problem?id=1009
问题点:RLE编码。
 Memory: 648K        Time: 547MS
Language: C++ Result: Accepted #include <iostream>
#include <cstdlib>
#include <map>
#include <vector>
using namespace std; map<int,int> mp;
map<int,int> omp;
map<int, int>::iterator mp_Iter;
map<int, int>::iterator omp_Iter;
vector<int> st;
static int around[][]={{-,-},{-,},{-,},{,-},{,},{,-},{,},{,}};
int getValue(int index)
{
int value=;
for(mp_Iter = mp.begin(); mp_Iter != mp.end(); mp_Iter++)
{
if(index<mp_Iter->first) break;
value=mp_Iter->second;
}
return value;
}
int getMax(int index,int width,int count)
{
int center=getValue(index);
int h=index/width;
int max=;
for(int i=;i<;i++)
{
int sub=index+around[i][]*width+around[i][];
if(h+around[i][]==sub/width && sub>= && sub<count)
{
int a=getValue(sub);
max=abs(center-a)>max?abs(center-a):max;
}
}
return max;
}
void process(int width,int count)
{
for(int i=; i < st.size(); i++)
{
int index=st.at(i);
omp.insert(pair<int,int>(index,getMax(index,width,count)));
}
}
int main()
{
int width;
while(cin>>width && cout<<width<<endl && width)
{
mp.clear();
omp.clear();
st.clear();
int v,l,count=;
while(cin>>v>>l && l){//v有可能为0
mp.insert(pair<int,int>(count,v));
count+=l;
}
for(mp_Iter = mp.begin(); mp_Iter != mp.end(); mp_Iter++)
{
for(int i=-;i<;i++)
{
for(int j=-;j<;j++)
{
int index=mp_Iter->first+i*width+j;
if(index>= && index <count)
st.push_back(index);
}
}
}
st.push_back(width);
st.push_back(count-width);
process(width,count); omp_Iter = omp.begin();
int st=omp_Iter->first;
int val=omp_Iter->second;
omp_Iter++;
for(;omp_Iter != omp.end(); omp_Iter++)
{
if(omp_Iter->second != val)
{
cout<<val<<" "<<omp_Iter->first-st<<endl;
st=omp_Iter->first;
val=omp_Iter->second;
}
}
cout<<val<<" "<<count-st<<endl;
cout<<"0 0"<<endl;
}
return ;
}