QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#672487 | #7904. Rainbow Subarray | wsy_jim# | WA | 1017ms | 10824kb | C++14 | 4.2kb | 2024-10-24 17:00:48 | 2024-10-24 17:00:48 |
Judging History
answer
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<map>
#include<set>
#include<queue>
#include<stack>
#include<cstdlib>
#include<ctime>
#include<bitset>
#include<vector>
#include<climits>
#include<iomanip>
#include<unordered_map>
using namespace std;
#define N 500010
template<typename T>
inline void read(T &x){
x=0;bool flg=0;char c=getchar();
for(;!isdigit(c);c=getchar()) if(c=='-') flg=1;
for(;isdigit(c);c=getchar()) x=x*10+(c^48);
if(flg) x=-x;
}
#define int long long
int a[N];
int n, k;
unordered_map<int, int> mp1, mp2;
priority_queue<int> low;
priority_queue<int, vector<int>, greater<int>> high;
bool check(int mid)
{
//cout << mid << endl;
int sumlow = 0, sumhigh = 0;
int sizelow = 0, sizehigh = 0;
mp1.clear(), mp2.clear();
while (!low.empty())
low.pop();
while (!high.empty())
high.pop();
for (int i = 1; i <= n; i++)
{
while (!high.empty() && mp1[high.top()] == 0)
{
//mp1[high.top()]--;
high.pop();
}
while (!low.empty() && mp2[low.top()] == 0)
{
//mp2[low.top()]--;
low.pop();
}
if (sizelow + sizehigh == mid)
{
//cout << sumhigh - sumlow << endl;
//cout << high.top() << "top" << endl;
//cout << endl;
if (mid % 2 == 0){
if (sumhigh - sumlow <= k){
//cout << "OOOooo" << endl;
return false;
}
}
else
if (sumhigh - sumlow - high.top() <= k)
{
//cout << "OOOooo" << endl;
return false;
}
int x = a[i - mid];
if (x >= high.top()) {
sizehigh--;
sumhigh -= x;
mp1[x]--;
while (!high.empty() && mp1[high.top()] == 0)
high.pop();
}
else {
sizelow --;
sumlow -= x;
mp2[x]--;
while (!low.empty() && mp2[low.top()] == 0)
low.pop();
}
}
//cout << sumlow << " " << sumhigh << endl;
//cout << endl;
if (sizelow < sizehigh) {
sizelow ++;
high.push(a[i]);
sumhigh += a[i];
mp1[a[i]]++;
mp1[high.top()]--;
mp2[high.top()]++;
sumhigh -= high.top();
sumlow += high.top();
low.push(high.top());
high.pop();
}
else {
sizehigh++;
low.push(a[i]);
sumlow += a[i];
mp2[a[i]]++;
mp2[low.top()]--;
mp1[low.top()]++;
sumlow -= low.top();
sumhigh += low.top();
high.push(low.top());
low.pop();
}
//cout << sumlow << " " << sumhigh << endl;
}
if (mid % 2 == 0){
if (sumhigh - sumlow <= k){
//cout << "OOOooo" << endl;
return false;
}
}
else
if (sumhigh - sumlow - high.top() <= k)
{
//cout << "OOOooo" << endl;
return false;
}
return true;
}
signed main()
{
int T;
cin >> T;
while (T--)
{
scanf("%lld %lld", &n, &k);
for (int i = 1; i <= n; i++)
{
scanf("%lld", &a[i]);
a[i] -= i;
}
int l = 0, r = n;
//if (check(4))
// cout << "OOO" << endl;
//cout << endl << endl;
while(l < r)
{
int mid = (l + r) / 2;
if (check(mid))
r = mid;
else
l = mid + 1;
}
//if (!check(l + 1))
// cout << l + 1 << endl;
if (!check(l))
cout << l << endl;
else
cout << l - 1 << endl;
}
}
详细
Test #1:
score: 100
Accepted
time: 0ms
memory: 3644kb
input:
5 7 5 7 2 5 5 4 11 7 6 0 100 3 4 5 99 100 5 6 1 1 1 1 1 5 50 100 200 300 400 500 1 100 3
output:
4 3 5 1 1
result:
ok 5 lines
Test #2:
score: -100
Wrong Answer
time: 1017ms
memory: 10824kb
input:
11102 2 167959139 336470888 134074578 5 642802746 273386884 79721198 396628655 3722503 471207868 6 202647942 268792718 46761498 443917727 16843338 125908043 191952768 2 717268783 150414369 193319712 6 519096230 356168102 262263554 174936674 407246545 274667941 279198849 9 527268921 421436316 3613460...
output:
1 4 3 2 6 5 7 2 4 1 4 1 1 3 2 2 7 8 7 7 1 7 6 2 4 3 1 6 7 7 3 4 3 9 3 8 6 6 3 1 6 3 1 2 4 6 4 6 4 1 4 7 1 6 3 5 6 6 1 7 5 3 1 6 4 5 3 2 2 6 2 3 10 1 4 3 2 4 5 1 7 5 5 5 8 5 3 6 3 5 5 8 5 4 5 2 1 5 2 3 3 4 8 1 3 1 2 2 8 3 1 6 8 1 8 4 5 6 6 8 4 8 3 2 8 4 5 6 2 6 2 4 1 5 4 5 3 2 4 1 2 1 4 5 8 3 7 3 3 3...
result:
wrong answer 3538th lines differ - expected: '5', found: '4'