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;
namespace std {
template <typename T> ostream &operator<<(ostream &out, const vector<T> &vec) {
out << "[";
for (int i = 0; i < (int)vec.size(); ++i) {
out << vec[i];
if (i + 1 < (int)vec.size())
out << ", ";
}
return out << "]";
}
} // namespace std
void dbg_out() { cout << endl; }
template <typename Head, typename... Tail> void dbg_out(Head H, Tail... T) {
cout << ' ' << H;
dbg_out(T...);
}
#ifdef DEBUG
#define dbg(...) cout << "(" << #__VA_ARGS__ << "):", dbg_out(__VA_ARGS__)
#else
#define dbg(...)
#endif
const int INF = 1e18;
struct SegTree {
vector<int> seg;
int from;
SegTree(int sz) {
from = 1;
while (from < sz)
from *= 2;
seg.assign(2 * from, INF);
}
void upd(int i, int x) {
i += from;
seg[i] = x;
while (i > 1) {
i /= 2;
seg[i] = min(seg[2 * i], seg[2 * i + 1]);
}
}
int query(int deb, int fin) {
deb += from, fin += from;
int ret = INF;
while (deb < fin) {
if (deb % 2)
ret = min(ret, seg[deb++]);
if (fin % 2)
ret = min(ret, seg[--fin]);
deb /= 2;
fin /= 2;
}
return ret;
}
};
signed main(void) {
ios_base::sync_with_stdio(false);
cin.tie(0);
int nbEleves, nbTests, maxSubtasks;
cin >> nbEleves >> nbTests >> maxSubtasks;
vector<int> points(nbTests), prefSum(nbTests + 1);
for (int i = 0; i < nbTests; ++i) {
cin >> points[i];
prefSum[i + 1] = prefSum[i] + points[i];
}
vector<vector<int>> prvWA(nbEleves, vector<int>(nbTests + 1, -1));
for (int i = 0; i < nbEleves; ++i)
for (int j = 0; j < nbTests; ++j) {
char c;
cin >> c;
prvWA[i][j + 1] = prvWA[i][j];
if (c == '0')
prvWA[i][j + 1] = j;
}
vector<SegTree> segtrees(nbEleves + 1, nbTests + 1);
for (int i = 0; i <= nbEleves; ++i)
segtrees[i].upd(0, 0);
vector<vector<int>> posChange(nbTests + 1);
for (int testRestant = 1; testRestant <= nbTests; ++testRestant) {
for (int iEleve = 0; iEleve < nbEleves; ++iEleve)
if (prvWA[iEleve][testRestant] != -1)
posChange[testRestant].push_back(prvWA[iEleve][testRestant]);
sort(posChange[testRestant].rbegin(), posChange[testRestant].rend());
}
for (int nbTaches = 1; nbTaches <= maxSubtasks; ++nbTaches) {
vector<int> dp(nbTests + 1, INF);
for (int testRestant = 1; testRestant <= nbTests; ++testRestant) {
int prv = testRestant - 1;
for (int nbMauvais = 0; nbMauvais <= (int)posChange[testRestant].size();
++nbMauvais) {
int nxt = nbMauvais == (int)posChange[testRestant].size()
? -1
: posChange[testRestant][nbMauvais]; // can't take nxt
int nbBons = nbEleves - nbMauvais;
dp[testRestant] =
min(dp[testRestant], nbBons * prefSum[testRestant] +
segtrees[nbBons].query(nxt + 1, prv + 1));
prv = nxt;
}
}
for (int nbBons = 0; nbBons <= nbEleves; ++nbBons) {
int from = segtrees[nbBons].from;
for (int i = 0; i <= nbTests; ++i) {
segtrees[nbBons].seg[from + i] = dp[i] - nbBons * prefSum[i];
}
for (int i = from - 1; i >= 1; --i)
segtrees[nbBons].seg[i] =
min(segtrees[nbBons].seg[2 * i], segtrees[nbBons].seg[2 * i + 1]);
}
dbg(dp);
cout << dp.back() << '\n';
}
}
# | 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... |