이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <unordered_set>
#include <unordered_map>
#include <algorithm>
#include <iostream>
#include <numeric>
#include <cassert>
#include <vector>
#include <stack>
#include <queue>
#include <map>
#include <set>
typedef long long llong;
const int MAXN = 500000 + 10;
const int MAXAB = 30000 + 10;
const llong INF = 1e18;
const int INTINF = 1e9;
int abs(int num)
{
if (num < 0) return -num;
return num;
}
llong abs(llong num)
{
if (num < 0) return -num;
return num;
}
int n;
int sum;
int c[MAXN];
llong dp[2][2*MAXAB];
int sumA;
int sumB;
void solve()
{
assert(sumA < MAXAB);
assert(sumB < MAXAB);
for (int i = 0 ; i < 2 * MAXAB ; ++i)
{
dp[(n + 1) & 1][i] = INF;
}
dp[(n + 1) & 1][MAXAB] = 0;
for (int pos = n ; pos >= 1 ; --pos)
{
for (int bal = 2 * MAXAB - 1 ; bal >= 0 ; --bal)
{
dp[pos & 1][bal] = INF;
if (bal + 1 < 2 * MAXAB)
{
dp[pos & 1][bal] = dp[pos & 1][bal + 1];
}
if (bal - c[pos] >= 0)
{
dp[pos & 1][bal] = std::min(dp[pos & 1][bal], dp[(pos + 1) & 1][std::min(bal - c[pos], 2 * MAXAB)] + abs(bal - c[pos] - MAXAB));
}
}
}
std::cout << dp[1][MAXAB] << '\n';
}
void input()
{
std::cin >> n;
for (int i = 1 ; i <= n ; ++i)
{
std::cin >> c[i]; sumA += c[i];
int val; std::cin >> val; sumB += val;
c[i] -= val;
sum += c[i];
}
}
void fastIOI()
{
std::ios_base :: sync_with_stdio(0);
std::cout.tie(nullptr);
std::cin.tie(nullptr);
}
int main()
{
fastIOI();
input();
solve();
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |