没想出什么好的算法,用的是最普通的方法
程序代码:
#include <iostream>
#include <algorithm>
#include <iterator>
#include <utility>
using namespace std;
struct foo
{
size_t x, y;
friend std::istream& operator>> (std::istream& is, foo& obj )
{
return is >> obj.x >> obj.y;
}
friend bool operator<( const foo& lhs, const foo& rhs ) noexcept
{
return std::tie(lhs.x,lhs.y) < std::tie(rhs.x,rhs.y);
}
friend size_t bulk( const foo& lhs, const foo& rhs, size_t m ) noexcept
{
auto foo = [](size_t delta) { return delta<2 ? 0 : delta-2; };
return lhs.x==rhs.x ? foo(rhs.y-lhs.y) : foo(m+1-lhs.y) + (rhs.x-lhs.x-1)*(m-1) + foo(rhs.y-0);
}
};
int main( void )
{
// 输入
size_t n,m,k;
foo s[1+47+1]; // 题目中交代过 1<=K<=47
cin >> n >> m >> k;
copy_n( istream_iterator<foo>(cin), k, s+1 );
// 开始干活
sort( s+1, s+1+k ); // 题目中没说过一定按照顺序输入
s[0].x=1, s[0].y=0;
s[k+1].x=n, s[k+1].y=m+1;
size_t sum = 0;
for( size_t i=0; i!=k+1; ++i )
sum += bulk( s[i], s[i+1], m );
// 输出
cout << sum << endl;
}