QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#322423 | #6767. Hourly Coding Problem | PetroTarnavskyi# | WA | 88ms | 7700kb | C++20 | 4.0kb | 2024-02-07 00:04:34 | 2024-02-07 00:04:35 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
#define FOR(i, a, b) for(int i = (a); i < (b); i++)
#define RFOR(i, b, a) for(int i = (b) - 1; i >= (a); i--)
#define SZ(a) (int)a.size()
#define ALL(a) a.begin(), a.end()
#define PB push_back
#define MP make_pair
#define F first
#define S second
typedef long long LL;
typedef vector<int> VI;
typedef vector<LL> VL;
typedef pair<int, int> PII;
typedef double db;
const int INF = 1e9;
const LL LINF = 1e18;
struct FewikMin
{
int n;
VL t;
void init(int _n)
{
n = _n;
t.resize(n, LINF);
}
void upd(int pos, LL val)
{
for(;pos < n; pos |= pos + 1)
t[pos] = min(t[pos], val);
}
LL query(int pos)
{
LL res = LINF;
for(;pos >= 0; pos = (pos & (pos + 1)) - 1)
res = min(res, t[pos]);
return res;
}
};
struct FewikMax
{
int n;
VL t;
void init(int _n)
{
n = _n;
t.resize(n, -LINF);
}
void upd(int pos, LL val)
{
for(;pos < n; pos |= pos + 1)
t[pos] = max(t[pos], val);
}
LL query(int pos)
{
LL res = -LINF;
for(;pos >= 0; pos = (pos & (pos + 1)) - 1)
res = max(res, t[pos]);
return res;
}
};
vector<pair<LL, LL>> recalc(const vector<LL>& pref, const vector<LL>& spref, int n, LL M)
{
int sz = n + 7;
FewikMax Tmax;
Tmax.init(sz);
FewikMin Tmin;
Tmin.init(sz);
vector<pair<LL, LL>> ans(n + 1);
FOR(i, 0, n + 1)
{
if(i != 0)
{
//pref[i] - pref[j] <= M -> pref[j] >= pref[i] - M
int pos = lower_bound(ALL(spref), pref[i] - M) - spref.begin();
//max[pos, n + 1] = Tmax[0, n + 1 - pos]
//cout << i << " " << pos << endl;
ans[i].F = Tmin.query(sz - 1 - pos) + 1;
ans[i].S = Tmax.query(sz - 1 - pos) + 1;
}
int pos = lower_bound(ALL(spref), pref[i]) - spref.begin();
//cout << i << " " << pos << " " << ans[i].F << " " << ans[i].S << endl;
Tmin.upd(sz - 1 - pos, ans[i].F);
Tmax.upd(sz - 1 - pos, ans[i].S);
}
return ans;
}
namespace BRUTE
{
const int N = 2000;
LL dp[N][N];
int from[N][N];
void solve(int n, int k, VI a)
{
assert(n < N - 1);
vector<LL> pref(n + 1);
FOR(i, 0, n)
{
pref[i + 1] = pref[i] + a[i];
}
FOR(i, 1, n + 1)
fill(dp[i], dp[i] + n + 1, LINF);
fill(dp[0], dp[0] + n + 1, LINF);
dp[0][0] = -LINF;
FOR(i, 1, n + 1)
{
FOR(cnt, 1, k + 1)
{
FOR(j, 0, i)
{
LL val = max(pref[i] - pref[j], dp[j][cnt - 1]);
if(dp[i][cnt] >= val)
{
dp[i][cnt] = val;
from[i][cnt] = j;
}
}
}
}
VI ans;
int v = n;
while(v != 0)
{
ans.PB(v - from[v][k]);
v = from[v][k];
k--;
}
assert(k == 0);
reverse(ALL(ans));
FOR(i, 0, SZ(ans))
{
if(i)
cout << " ";
cout << ans[i];
}
cout << "\n";
}
}
void solve()
{
int n, k;
cin >> n >> k;
VI a(n);
vector<LL> pref(n + 1, 0);
FOR(i, 0, n)
{
cin >> a[i];
pref[i + 1] = pref[i] + a[i];
}
if(1 || n < 200)
{
BRUTE::solve(n, k, a);
return;
}
vector<LL> spref = pref;
sort(ALL(spref));
spref.resize(unique(ALL(spref)) - spref.begin());
LL L = -LINF, R = LINF;
while(R - L > 1)
{
LL M = (L + R) / 2;
auto res = recalc(pref, spref, n, M);
if(res.back().F <= k && k <= res.back().S)
R = M;
else
L = M;
}
auto res = recalc(pref, spref, n, R);
int last = n;
VI ans;
RFOR(i, n, 0)
{
if(pref[last] - pref[i] > R)
continue;
if(res[i].F <= k - 1 && k - 1 <= res[i].S)
{
k--;
ans.PB(last - i);
last = i;
}
}
reverse(ALL(ans));
assert(k == 0);
assert(last == 0);
int pos = 0;
FOR(i, 0, SZ(ans))
{
//cout << pos << " " << pos + ans[i] << " " << pref[pos + ans[i]] << " " << pref[pos] << endl;
assert(pref[pos + ans[i]] - pref[pos] <= R);
pos += ans[i];
}
FOR(i, 0, SZ(ans))
{
if(i)
cout << " ";
cout << ans[i];
}
cout << "\n";
}
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout << fixed << setprecision(15);
int t;
cin >> t;
while(t--)
solve();
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 1ms
memory: 5688kb
input:
3 7 3 5 1 0 2 7 -3 4 6 4 -1 3 -2 4 -3 5 6 2 0 -2 0 -1 -1 0
output:
3 3 1 1 1 2 2 3 3
result:
ok 3 lines
Test #2:
score: -100
Wrong Answer
time: 88ms
memory: 7700kb
input:
200178 2 1 5 -1 4 3 4 -7 -4 10 2 1 -6 -2 4 1 8 10 -9 10 4 2 10 -4 10 1 4 1 7 -1 4 3 4 1 7 -5 10 10 2 1 -7 9 3 2 3 3 4 4 4 -1 4 -9 -8 3 1 10 6 7 2 2 3 8 4 4 0 -9 -2 -9 2 1 9 -3 1 1 -3 3 3 -8 -7 -3 4 3 -2 -3 3 1 4 4 7 -5 4 0 3 2 2 6 3 5 1 7 -3 2 3 0 4 1 10 -8 -8 -4 1 1 -2 1 1 -2 3 2 1 -9 -5 2 1 -6 4 1...
output:
2 1 1 2 2 4 1 3 4 4 2 2 1 1 1 1 1 3 1 1 1 1 1 1 2 1 1 1 1 1 2 1 1 1 1 1 2 1 5 4 1 1 2 1 2 1 1 2 2 1 1 1 1 1 1 2 1 1 2 3 1 1 2 1 1 1 1 1 2 1 4 5 1 1 1 1 1 1 1 2 2 1 1 5 1 2 1 3 1 5 1 1 2 1 4 4 1 3 1 1 1 1 1 1 3 1 1 1 1 1 1 1 3 2 1 1 1 1 1 1 4 1 1 1 2 2 1 2 1 1 1 1 1 3 1 3 1 1 1 1 1 1 2 1 2 2 2 3 2 3 ...
result:
wrong answer 28th lines differ - expected: '3 1 1', found: '2 2 1'