题意
简单的讲,题目的意思就是计算一张图片的边界。
分析
首先,不可能每个点挨个计算,所以要找出哪些需要计算的点。
使用了STL容器,另外加上情况考虑得比较复杂,所以效率不高。
代码如下:
#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
void ComputePoint(vector<int>& Result, const int& Point, const int& Width, const int& EndPoint, const int Key)
{
if (Point - Width >= 0)
{
Result.push_back(Point - Width);
if ((Point - Width) % Width > 0) Result.push_back(Point - Width - 1);
if ((Point - Width) % Width < Width - 1) Result.push_back(Point - Width + 1);
if (Key == 1 && (Point - Width) % Width == Width - 1) Result.push_back(Point - Width + 1);
}
if (Point + Width <= EndPoint)
{
Result.push_back(Point + Width);
if ((Point + Width) % Width > 0) Result.push_back(Point + Width - 1);
if ((Point + Width) % Width < Width - 1) Result.push_back(Point + Width + 1);
if (Key == 1 && (Point + Width) % Width == Width - 1 && Point + Width + 1 <= EndPoint) Result.push_back(Point + Width + 1);
}
Result.push_back(Point);
if (Point % Width > 0) Result.push_back(Point - 1);
if (Point % Width < Width - 1) Result.push_back(Point + 1);
};
int Find(const int* ValArray, const int* End, const int Point)
{
for (int i = 0;; ++i)
if (Point <= End[i])
return ValArray[i];
}
int Compare(const vector<int>& Vec, const int Value, const int*ValArray, const int* End)
{
int temp = 0;
for (vector<int>::const_iterator iter = Vec.begin(); iter != Vec.end(); ++iter)
if (Value != *iter&&temp < abs(Find(ValArray, End, *iter) - Find(ValArray, End, Value))) temp = abs(Find(ValArray, End, *iter) - Find(ValArray, End, Value));
return temp;
};
void Maximize(const vector<int>& NeedCompute, const int& Width, const int& EndPoint, const int* ValArray, const int* End)
{
int Count = (int)NeedCompute.size();
int* List = new int[Count];
int* Position = new int[Count];
vector<int>::const_iterator iter = NeedCompute.begin();
for (int i = 0; iter != NeedCompute.end(); ++iter, ++i)
{
Position[i] = *iter;
vector<int> temp;
ComputePoint(temp, *iter, Width, EndPoint, 0);
List[i] = Compare(temp, *iter, ValArray, End);
}
cout << Width << endl;
for (int i = 0; i < Count;)
{
int j = i + 1;
for (; j < Count; ++j)
if (List[j] != List[i])
{
cout << List[i] << " " << Position[j] - Position[i] << endl;
i = j;
break;
}
if (j == Count)
{
cout << List[i] << " " << EndPoint - Position[i] + 1 << endl;
break;
}
}
cout << 0 << " " << 0 << endl;
delete[]List;
delete[]Position;
}
void Calculate(const int& Width, const int& Length, const int* ValArray, const int* Start, const int* End)
{
vector<int> NeedCompute;
for (int i = 0; i < Length; i++)
{
ComputePoint(NeedCompute, Start[i], Width, End[Length - 1], 1);
NeedCompute.push_back(Start[i] - (Start[i] % Width));
}
sort(NeedCompute.begin(), NeedCompute.end());
NeedCompute.erase(unique(NeedCompute.begin(), NeedCompute.end()), NeedCompute.end());
Maximize(NeedCompute, Width, End[Length - 1], ValArray, End);
}
int main()
{
int Width;
while (cin >> Width&&Width != 0)
{
int Value = 0;
int Count = 0;
int ValArray[1000];
int Start[1000];
int End[1000];
int Length = 0;
while (cin >> Value >> Count&&Count != 0)
{
if (Length == 0)
{
Start[Length] = 0;
End[Length] = Count - 1;
}
else
{
Start[Length] = End[Length - 1] + 1;
End[Length] = End[Length - 1] + Count;
}
ValArray[Length] = Value;
++Length;
}
Calculate(Width, Length, ValArray, Start, End);
}
cout << 0 << endl;
return 0;
}