DIV1 250pt

题意:在平面直角坐标系中,只能走到整点,每次有两种移动方法,可以沿平行于坐标轴方向走,也可以沿45度方向走,前者走一步耗时wt,后者走一步耗时st。比如从(x, y)可以走到(x+1, y),(x-1, y),(x, y+1),(x, y-1)四个点,耗时均为wt,也可以走到(x-1, y+1),(x-1, y-1),(x+1, y+1),(x+1, y-1),耗时均为st。给定x, y, wt, st,求从(0, 0)到(x, y)最少耗时多少。

解法:水题,见代码。

tag: greedy

 // BEGIN CUT HERE
/* */
// END CUT HERE
#line 7 "StreetWalking.cpp"
#include <cstdlib>
#include <cctype>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <vector>
#include <iostream>
#include <sstream>
#include <set>
#include <queue>
#include <fstream>
#include <numeric>
#include <iomanip>
#include <bitset>
#include <list>
#include <stdexcept>
#include <functional>
#include <string>
#include <utility>
#include <map>
#include <ctime>
#include <stack> using namespace std; #define clr0(x) memset(x, 0, sizeof(x))
#define clr1(x) memset(x, -1, sizeof(x))
#define pb push_back
#define mp make_pair
#define sz(v) ((int)(v).size())
#define out(x) cout<<#x<<":"<<(x)<<endl
#define tst(a) cout<<#a<<endl
#define CINBEQUICKER std::ios::sync_with_stdio(false) typedef vector<int> VI;
typedef vector<string> VS;
typedef vector<double> VD;
typedef long long int64; const double eps = 1e-;
const double PI = atan(1.0)*;
const int inf = / ; inline int MyMod( int a , int b ) { return (a%b+b)%b;} class StreetWalking
{
public:
long long minTime(int x, int y, int wt, int st){
if (st >= wt*) return ((int64)x + y) * wt; if (x < y) swap(x, y);
int64 t = (int64)y * st;
int64 dif = x - y;
if (st < wt){
if (dif & ) return t + (dif-) * st + wt;
return t + dif * st;
}
return t + dif * wt;
} // BEGIN CUT HERE
public:
void run_test(int Case) { if ((Case == -) || (Case == )) test_case_0(); if ((Case == -) || (Case == )) test_case_1(); if ((Case == -) || (Case == )) test_case_2(); if ((Case == -) || (Case == )) test_case_3(); if ((Case == -) || (Case == )) test_case_4(); if ((Case == -) || (Case == )) test_case_5(); if ((Case == -) || (Case == )) test_case_6(); }
private:
template <typename T> string print_array(const vector<T> &V) { ostringstream os; os << "{ "; for (typename vector<T>::const_iterator iter = V.begin(); iter != V.end(); ++iter) os << '\"' << *iter << "\","; os << " }"; return os.str(); }
void verify_case(int Case, const long long &Expected, const long long &Received) { cerr << "Test Case #" << Case << "..."; if (Expected == Received) cerr << "PASSED" << endl; else { cerr << "FAILED" << endl; cerr << "\tExpected: \"" << Expected << '\"' << endl; cerr << "\tReceived: \"" << Received << '\"' << endl; } }
void test_case_0() { int Arg0 = ; int Arg1 = ; int Arg2 = ; int Arg3 = ; long long Arg4 = 18LL; verify_case(, Arg4, minTime(Arg0, Arg1, Arg2, Arg3)); }
void test_case_1() { int Arg0 = ; int Arg1 = ; int Arg2 = ; int Arg3 = ; long long Arg4 = 16LL; verify_case(, Arg4, minTime(Arg0, Arg1, Arg2, Arg3)); }
void test_case_2() { int Arg0 = ; int Arg1 = ; int Arg2 = ; int Arg3 = ; long long Arg4 = 20LL; verify_case(, Arg4, minTime(Arg0, Arg1, Arg2, Arg3)); }
void test_case_3() { int Arg0 = ; int Arg1 = ; int Arg2 = ; int Arg3 = ; long long Arg4 = 247LL; verify_case(, Arg4, minTime(Arg0, Arg1, Arg2, Arg3)); }
void test_case_4() { int Arg0 = ; int Arg1 = ; int Arg2 = ; int Arg3 = ; long long Arg4 = 240LL; verify_case(, Arg4, minTime(Arg0, Arg1, Arg2, Arg3)); }
void test_case_5() { int Arg0 = ; int Arg1 = ; int Arg2 = ; int Arg3 = ; long long Arg4 = 41010000000LL; verify_case(, Arg4, minTime(Arg0, Arg1, Arg2, Arg3)); }
void test_case_6() { int Arg0 = ; int Arg1 = ; int Arg2 = ; int Arg3 = ; long long Arg4 = 3929LL; verify_case(, Arg4, minTime(Arg0, Arg1, Arg2, Arg3)); } // END CUT HERE };
//by plum rain
// BEGIN CUT HERE
int main()
{
//freopen( "a.out" , "w" , stdout );
StreetWalking ___test;
___test.run_test(-);
return ;
}
// END CUT HERE
05-11 13:31