diff --git a/NFLSOJ/13244/13244.cpp b/NFLSOJ/13244/13244.cpp new file mode 100644 index 00000000..f65df0c8 --- /dev/null +++ b/NFLSOJ/13244/13244.cpp @@ -0,0 +1,64 @@ +// #include +#include +#include +#include +#include +#include + +// using std::cin; +// using std::cout; +std::ifstream cin("rewrite.in"); +std::ofstream cout("rewrite.out"); +const char endl = '\n'; + +const int N = 5005; + +int n; +long long f[N][N][2]; +bool flag = false; +std::pair a[N]; + +int main() { + std::ios::sync_with_stdio(false); + cin.tie(nullptr); + + cin >> n; + + for (int i = 1, l, r; i <= n; i++) { + cin >> a[i].first >> a[i].second; + } + + if (n % 2) { + flag = true; + } else { + a[++n] = std::make_pair(0, 0); + } + + memset(f, 0x3f, sizeof(f)); + std::sort(a + 1, a + n + 1, [&](const std::pair &lhs, const std::pair &rhs) -> bool { return lhs.second - lhs.first > rhs.second - rhs.first; }); + + f[0][0][0] = 0; + for (int i = 1; i <= n; i++) { + if (i == n || flag) { + for (int j = 0; j <= i; j++) { + f[i][j][1] = f[i - 1][j][0] + static_cast(n >> 1) * (a[i].second - a[i].first); + } + } + + for (int j = 0; j <= i; j++) { + if (j) { + f[i][j][0] = std::min(f[i][j][0], f[i - 1][j - 1][0] + static_cast(j - 1) * (a[i].second - a[i].first) + a[i].second); + f[i][j][1] = std::min(f[i][j][1], f[i - 1][j - 1][1] + static_cast(j - 1) * (a[i].second - a[i].first) + a[i].second); + } + + if (j != i) { + f[i][j][0] = std::min(f[i][j][0], f[i - 1][j][0] + static_cast(i - j - 1) * (a[i].second - a[i].first) - a[i].first); + f[i][j][1] = std::min(f[i][j][1], f[i - 1][j][1] + static_cast(i - j - 2) * (a[i].second - a[i].first) - a[i].first); + } + } + } + + cout << f[n][n >> 1][1] << endl; + + return 0; +} diff --git a/NFLSOJ/13244/samples/ex_rewrite1.in b/NFLSOJ/13244/samples/ex_rewrite1.in new file mode 100644 index 00000000..43487eb3 --- /dev/null +++ b/NFLSOJ/13244/samples/ex_rewrite1.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:0880b9669fa6d82f7223ec67bc09b247ed598249020bd967e01c7f5dd42818aa +size 6 diff --git a/NFLSOJ/13244/samples/ex_rewrite1.out b/NFLSOJ/13244/samples/ex_rewrite1.out new file mode 100644 index 00000000..804fcc90 --- /dev/null +++ b/NFLSOJ/13244/samples/ex_rewrite1.out @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:9a271f2a916b0b6ee6cecb2426f0b3206ef074578be55d9bc94f6f3fe3ab86aa +size 2 diff --git a/NFLSOJ/13244/samples/ex_rewrite2.in b/NFLSOJ/13244/samples/ex_rewrite2.in new file mode 100644 index 00000000..e2eaab6d --- /dev/null +++ b/NFLSOJ/13244/samples/ex_rewrite2.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:7430fad08805d1f25cc58a2979d818637281cef19f9e84d67de1b91fcc45262b +size 18 diff --git a/NFLSOJ/13244/samples/ex_rewrite2.out b/NFLSOJ/13244/samples/ex_rewrite2.out new file mode 100644 index 00000000..06d04c2c --- /dev/null +++ b/NFLSOJ/13244/samples/ex_rewrite2.out @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:aa67a169b0bba217aa0aa88a65346920c84c42447c36ba5f7ea65f422c1fe5d8 +size 2 diff --git a/NFLSOJ/13244/samples/ex_rewrite3.in b/NFLSOJ/13244/samples/ex_rewrite3.in new file mode 100644 index 00000000..45a5082c --- /dev/null +++ b/NFLSOJ/13244/samples/ex_rewrite3.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:b2cb1aaddda03e95c4a1c58edaf9ceacd794c05c87903b8c366f57d84960b9a9 +size 385 diff --git a/NFLSOJ/13244/samples/ex_rewrite3.out b/NFLSOJ/13244/samples/ex_rewrite3.out new file mode 100644 index 00000000..3d278ce1 --- /dev/null +++ b/NFLSOJ/13244/samples/ex_rewrite3.out @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:7f063ddc9465871b106b29b9358227628ec34ddf99a2d73f523decd70a2792e5 +size 13 diff --git a/NFLSOJ/13244/samples/ex_rewrite4.in b/NFLSOJ/13244/samples/ex_rewrite4.in new file mode 100644 index 00000000..74e53ce5 --- /dev/null +++ b/NFLSOJ/13244/samples/ex_rewrite4.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:596a6c2fb8ac5663ef5710a1c8a00c28c3da129fcd24c638eaae1b2e72f05f2a +size 42463 diff --git a/NFLSOJ/13244/samples/ex_rewrite4.out b/NFLSOJ/13244/samples/ex_rewrite4.out new file mode 100644 index 00000000..4f87ad45 --- /dev/null +++ b/NFLSOJ/13244/samples/ex_rewrite4.out @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:88427cbe4816023bd282c3f34855aaab4ec25068619363018af8a5da35d72d24 +size 17