Last active
February 9, 2017 04:48
-
-
Save rendon/aeceeb7f405cbd2c43b4091d07dee94d to your computer and use it in GitHub Desktop.
Tanques Omegaup DP
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* Copyright 2017 Rafael Rendón Pablo <[email protected]> */ | |
// region Template | |
#include <iostream> | |
#include <string> | |
#include <vector> | |
#include <algorithm> | |
#include <set> | |
#include <queue> | |
#include <map> | |
#include <cstring> | |
#include <cctype> | |
using namespace std; | |
typedef long long int64; | |
typedef unsigned long long uint64; | |
const int kOffset = 1000; | |
const int kMax = 5001; | |
// endregion | |
struct Tank { | |
int x, y; | |
bool operator<(const Tank& that) const { | |
if (this->x != that.x) { | |
return this->x < that.x; | |
} | |
return this->y < that.y; | |
} | |
}; | |
map<Tank, int> dp[kMax]; | |
Tank tanks[kMax]; | |
int f(int p, int x, int y) { | |
Tank tank = tanks[p]; | |
if (p == 0) { | |
return min(abs(tank.x - x), abs(tank.y - y)); | |
} | |
Tank prev{x, y}; | |
if (dp[p].count(prev) == 1) { | |
return dp[p][prev]; | |
} | |
int gox = f(p - 1, tank.x, y) + abs(tank.x - x); | |
int goy = f(p - 1, x, tank.y) + abs(tank.y - y); | |
dp[p][prev] = min(gox, goy); | |
return dp[p][prev]; | |
} | |
int main() { | |
ios::sync_with_stdio(false); | |
cin.tie(nullptr); | |
int T; | |
cin >> T; | |
for (int i = 0; i < T; i++) { | |
cin >> tanks[i].x >> tanks[i].y; | |
tanks[i].x += kOffset; | |
tanks[i].y += kOffset; | |
} | |
reverse(tanks, tanks + T); | |
cout << f(T - 1, kOffset, kOffset) << endl; | |
return EXIT_SUCCESS; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment