#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;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Incorrect |
1 ms |
364 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Incorrect |
1 ms |
364 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Incorrect |
1 ms |
364 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |