This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#define int long long
using namespace std;
const int INF = 1e18;
signed main(void)
{
ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
int nbPieces;
cin >> nbPieces;
nbPieces *= 2;
vector<pair<int, int>> pieces(nbPieces);
for (auto &[x, y] : pieces)
cin >> x >> y;
sort(pieces.begin(), pieces.end());
int sol(0);
vector<vector<int>> dp(nbPieces+1, vector<int>(2, INF));
dp[0][0] = dp[0][1] = 0;
for (int iPiece(0); iPiece < nbPieces; ++iPiece)
{
auto [xPiece, yPiece] = pieces[iPiece];
int x = iPiece / 2 + 1;
int dis1 = abs(x-xPiece) + abs(yPiece-1);
int dis2 = abs(x-xPiece) + abs(yPiece-2);
if (iPiece % 2 == 0)
{
int bstBefore = min(dp[iPiece][0], dp[iPiece][1]);
dp[iPiece+1][0] = bstBefore + dis1;
dp[iPiece+1][1] = bstBefore + dis2;
}
else
{
dp[iPiece+1][0] = dp[iPiece][1] + dis1;
dp[iPiece+1][1] = dp[iPiece][0] + dis2;
}
}
sol = min(dp[nbPieces][0], dp[nbPieces][1]);
cout << sol << endl;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |