QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#322401 | #6767. Hourly Coding Problem | PetroTarnavskyi# | WA | 1075ms | 3888kb | C++20 | 2.8kb | 2024-02-06 23:34:06 | 2024-02-06 23:34:07 |
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 pair<int, int> PII;
typedef double db;
const int INF = 1e9;
const LL LINF = 1e18;
struct FewikMin
{
int n;
VI t;
void init(int _n)
{
n = _n;
t.resize(n, INF);
}
void upd(int pos, int val)
{
for(;pos < n; pos |= pos + 1)
t[pos] = min(t[pos], val);
}
int query(int pos)
{
int res = INF;
for(;pos >= 0; pos = (pos & (pos + 1)) - 1)
res = min(res, t[pos]);
return res;
}
};
struct FewikMax
{
int n;
VI t;
void init(int _n)
{
n = _n;
t.resize(n, -INF);
}
void upd(int pos, int val)
{
for(;pos < n; pos |= pos + 1)
t[pos] = max(t[pos], val);
}
int query(int pos)
{
int res = -INF;
for(;pos >= 0; pos = (pos & (pos + 1)) - 1)
res = max(res, t[pos]);
return res;
}
};
vector<PII> recalc(const vector<LL>& pref, const vector<LL>& spref, int n, LL M)
{
FewikMax Tmax;
Tmax.init(n + 2);
FewikMin Tmin;
Tmin.init(n + 2);
vector<PII> ans(n + 1);
FOR(i, 0, n + 1)
{
//pref[i] - pref[j] <= M -> pref[j] >= pref[i] - M
if(i != 0)
{
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(n + 1 - pos) + 1;
ans[i].S = Tmax.query(n + 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(n + 1 - pos, ans[i].F);
Tmax.upd(n + 1 - pos, ans[i].S);
}
return ans;
}
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];
}
vector<LL> spref = pref;
sort(ALL(spref));
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;
}
}
assert(k == 0);
reverse(ALL(ans));
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;
}
详细
Test #1:
score: 100
Accepted
time: 1ms
memory: 3652kb
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: 1075ms
memory: 3888kb
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 3 1 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 3476th lines differ - expected: '2 1 2', found: '1 3 1'