QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#304300 | #7897. Largest Digit | PetroTarnavskyi# | WA | 1ms | 3448kb | C++20 | 1.9kb | 2024-01-13 17:26:15 | 2024-01-13 17:26:15 |
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;
struct Fenwick
{
int n;
vector<LL> v;
void init(int _n)
{
n = _n;
v.clear();
v.assign(n, 0);
}
void upd(int i, int x)
{
for (; i < n; i |= (i + 1))
v[i] += x;
}
LL query(int i)
{
LL ans = 0;
for (; i >= 0; i = (i & (i + 1)) - 1)
ans += v[i];
return ans;
}
int lowerBound(LL x)
{
LL sum = 0;
int i = -1;
int lg = 31 - __builtin_clz(n);
while (lg >= 0)
{
int j = i + (1 << lg);
if (j < n && sum + v[j] < x)
{
sum += v[j];
i = j;
}
lg--;
}
return i + 1;
}
}cnt, sum;
int n;
LL k;
VI a, b;
void add(int i, bool add)
{
int j = lower_bound(ALL(b), a[i]) - b.begin();
cnt.upd(j, add ? 1 : -1);
sum.upd(j, add ? a[i] : -a[i]);
}
bool ok()
{
int c = cnt.query(n);
int j = cnt.lowerBound((c + 1) / 2);
LL x = b[j];
LL s1 = sum.query(j);
LL s2 = sum.query(n) - s1;
LL need = x * j - s1 + s2 - x * (c - j);
return need <= k;
}
void solve()
{
cin >> n >> k;
cnt.init(n);
sum.init(n);
a.clear();
a.resize(n);
b.clear();
b.resize(n);
FOR (i, 0, n)
{
cin >> a[i];
a[i] -= i;
b[i] = a[i];
}
sort(ALL(b));
int ans = 0;
int r = 0;
FOR (l, 0, n)
{
while (r < n && ok())
{
add(r, 1);
r++;
}
ans = max(ans, r - l);
add(l, 0);
}
cout << ans << '\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: 0
Wrong Answer
time: 1ms
memory: 3448kb
input:
2 178 182 83 85 2 5 3 6
output:
142 143
result:
wrong answer 1st lines differ - expected: '7', found: '142'