Add (forgotten) material from 2017 and 2018

parent 8a4cf9ed
File mode changed from 100755 to 100644
#include <cstdio>
#include <cinttypes>
#include <limits>
#include <vector>
using namespace std;
int64_t solve(int L, int G, const vector<int> &c) {
// Precompute sums.
vector<int64_t> s(L+1);
s[0] = 0;
for (int i = 1; i <= L; ++i) s[i] = s[i-1] + c[i-1];
// DP[g][l] : solution for the problem of g guards and the first l lunatics.
vector<vector<int64_t>> DP(G+1);
for (int i = 0; i <= G; ++i) DP[i].resize(L+1);
/* if g > 1:
* DP[g][l] = min_{k=0}^{k<=l} DP[g-1][k] + (s[l]-s[k]) * (l-k)
* otherwise:
* DP[1][l] = (s[l]-s[0]) * l
*/
for (int l = 0; l <= L; ++l)
DP[1][l] = (s[l] - s[0]) * l;
for (int g = 2; g <= G; ++g)
for (int l = 0; l <= L; ++l) {
DP[g][l] = numeric_limits<int64_t>::max();
for (int k = 0; k <= l; ++k) {
int64_t alt = DP[g-1][k] + (s[l] - s[k]) * (l - k);
if (alt < DP[g][l]) DP[g][l] = alt;
}
}
return DP[G][L];
}
int main() {
int L, G;
scanf("%d%d", &L, &G);
vector<int> c(L);
for (int i = 0; i < L; ++i)
scanf("%d", &(c[i]));
printf("%" PRIi64 "\n", solve(L, G, c));
}
#include <cstdio>
#include <cinttypes>
#include <limits>
#include <vector>
using namespace std;
int64_t solve(int L, int G, const vector<int> &c) {
// Precompute sums.
vector<int64_t> s(L+1);
s[0] = 0;
for (int i = 1; i <= L; ++i) s[i] = s[i-1] + c[i-1];
// DP[g][l] : solution for the problem of g guards
// and the first l lunatics.
vector<vector<int64_t>> DP(2);
for (int i = 0; i <= 1; ++i) DP[i].resize(L+1);
/* if g > 1:
* DP[g][l] = min_{k=0}^{k<=l} DP[g-1][k] + (s[l]-s[k]) * (l-k)
* otherwise:
* DP[1][l] = (s[l]-s[0]) * l
*/
for (int l = 0; l <= L; ++l)
DP[1][l] = (s[l] - s[0]) * l;
for (int g = 2; g <= G; ++g)
for (int l = 0; l <= L; ++l) {
DP[g%2][l] = numeric_limits<int64_t>::max();
for (int k = 0; k <= l; ++k) {
int64_t alt = DP[(g-1)%2][k] + (s[l] - s[k]) * (l - k);
if (alt < DP[g%2][l]) DP[g%2][l] = alt;
}
}
return DP[G%2][L];
}
int main() {
int L, G;
scanf("%d%d", &L, &G);
vector<int> c(L);
for (int i = 0; i < L; ++i)
scanf("%d", &(c[i]));
printf("%" PRIi64 "\n", solve(L, G, c));
}
#include <cstdio>
#include <cinttypes>
#include <limits>
#include <vector>
using namespace std;
int64_t solve(int L, int G, const vector<int> &c) {
// Precompute sums.
vector<int64_t> s(L+1);
s[0] = 0;
for (int i = 1; i <= L; ++i) s[i] = s[i-1] + c[i-1];
// DP[g][l] : solution for the problem of g guards
// and the first l lunatics.
vector<vector<int64_t>> DP(G+1);
for (int i = 0; i <= G; ++i) DP[i].resize(L+1);
/* if g > 1:
* DP[g][l] = min_{k=0}^{k<=l} DP[g-1][k] + (s[l]-s[k]) * (l-k)
* otherwise:
* DP[1][l] = (s[l]-s[0]) * l
*/
for (int l = 0; l <= L; ++l)
DP[1][l] = (s[l] - s[0]) * l;
for (int g = 2; g <= G; ++g)
for (int l = 0; l <= L; ++l) {
DP[g][l] = numeric_limits<int64_t>::max();
for (int k = 0; k <= l; ++k) {
int64_t alt = DP[g-1][k] + (s[l] - s[k]) * (l - k);
if (alt < DP[g][l]) DP[g][l] = alt;
}
}
return DP[G][L];
}
int main() {
int L, G;
scanf("%d%d", &L, &G);
vector<int> c(L);
for (int i = 0; i < L; ++i)
scanf("%d", &(c[i]));
printf("%" PRIi64 "\n", solve(L, G, c));
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment