QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#316419 | #7904. Rainbow Subarray | gingoo | RE | 0ms | 3504kb | C++20 | 7.4kb | 2024-01-27 20:41:11 | 2024-01-27 20:41:12 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
typedef int ld;
const int mod=1e9+7;
// struct point
// {
// ld x, y;
// point operator + (const point &a) const
// {
// return {x + a.x, y + a.y};
// }
// point operator - (const point &a) const
// {
// return {x - a.x, y - a.y};
// }
// point operator * (const ld &a) const
// {
// return {x * a, y * a};
// }
// };
// struct line
// {
// point s, t;
// };
// ld sqr(ld x) { return x * x; }
// // // const ld eps = 1e-9;
// // int sgn(ld x) { return x > eps ? 1 : -eps ? -1
// // : 0; }
// ld dot(const point &a, const point &b)
// {
// return a.x * b.x + a.y * b.y;
// }
// ld det(const point &a, const point &b)
// {
// return a.x * b.y - a.y * b.x;
// }
// bool point_on_line(const point &a, const line &l){
// return (det(l.s - a, a - l.t)) == 0;
// }
// bool pol(const point &a,const point &b,const point &c){
// return (a.y-b.y)*(b.x-c.x)==(b.y-c.y)*(a.x-b.x);
// }
// vector<point> v;
// map<point,int> mp;
// int n;
// bool dfs(vector<point> &v1,int now){
// if(v1.size()<=now)return 1;
// for(int i=0;i<=now;i++){
// for(int j=i+1;j<=now;j++){
// vector<point> v2;
// for(auto p:v1){
// if(!point_on_line(p,(line){v1[i],v1[j]})){
// v2.push_back(p);
// }
// }
// if(dfs(v2,now-1))return 1;
// }
// }
// return 0;
// }
typedef long long ll;
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);cout.tie(0);
int t;
cin>>t;
while(t--){
ll n,k;
cin>>n>>k;
vector<ll> a;
for(int i=1;i<=n;i++){
ll x;
cin>>x;
a.push_back(x-i);
}
int l=0,r=0;
ll sum1=0,sum2=0;
set<ll> s1,s2;
int ans=0;
ll now=a[0];
while(l<=r){
if(l==a.size())break;
while(sum1-now*s1.size()+now*s2.size()-sum2<=k){
ll now1,now2;
if(r==a.size()-1)break;
// int flag1=0,,flag2=0;
r++;
if(s1.empty() and s2.empty()){
if(a[r]>=now)s1.insert(a[r]),sum1+=a[r];
else s2.insert(a[r]),sum2+=a[r];
continue;
}
else if(s1.empty()){
if(a[r]>=now)s1.insert(a[r]),sum1+=a[r];
else s1.insert(now),sum1+=now,now=a[r];
continue;
}
else if(s2.empty()){
if(a[r]<=now)s2.insert(a[r]),sum2+=a[r];
else s2.insert(now),sum2+=now,now=a[r];
continue;
}
now1=*s1.begin(),s1.erase(s1.find(now1)),sum1-=now1;
now2=*s2.rbegin(),s2.erase(s2.find(now2)),sum2-=now2;
int cnt=r-l+1;
if(a[r]>=now1){
s1.insert(a[r]),sum1+=a[r];
if(cnt%2){
//s1.insert(now1),sum1+=now1;
//s2.insert(now2),sum2+=now2;
if(s1.size()>s2.size()){
s2.insert(now2),sum2+=now2;
s2.insert(now),sum2+=now;
now=now1;
}
else{
s1.insert(now1),sum1+=now1;
s2.insert(now2),sum2+=now2;
}
}
else{
s1.insert(now1),sum1+=now1;
s2.insert(now2),sum2+=now2;
}
}
else if(a[r]<=now2){
s2.insert(a[r]),sum2+=a[r];
if(cnt%2){
//s1.insert(now1),sum1+=now1;
//s2.insert(now2),sum2+=now2;
if(s1.size()<s2.size()){
s1.insert(now1),sum1+=now1;
s1.insert(now),sum1+=now;
now=now2;
}
else{
s1.insert(now1),sum1+=now1;
s2.insert(now2),sum2+=now2;
}
}
else{
s1.insert(now1),sum1+=now1;
s2.insert(now2),sum2+=now2;
}
}
else if(a[r]<=now and a[r]>=now2){
s2.insert(now2),sum2+=now2;
if(cnt%2){
//s1.insert(now1),sum1+=now1;
//s2.insert(now2),sum2+=now2;
if(s1.size()<s2.size()){
s1.insert(now1),sum1+=now1;
s1.insert(now),sum1+=now;
now=a[r];
}
else{
s1.insert(now1),sum1+=now1;
s2.insert(a[r]),sum2+=a[r];
}
}
else{
s1.insert(now1),sum1+=now1;
s2.insert(a[r]),sum2+=a[r];
}
}
else if(a[r]>=now and a[r]<=now1){
s1.insert(now1),sum1+=now1;
if(cnt%2){
//s1.insert(now1),sum1+=now1;
//s2.insert(now2),sum2+=now2;
if(s1.size()>s2.size()){
s2.insert(now2),sum2+=now2;
s2.insert(now),sum2+=now;
now=a[r];
}
else{
s1.insert(now1),sum1+=now1;
s2.insert(a[r]),sum2+=a[r];
}
}
else{
s1.insert(now1),sum1+=now1;
s2.insert(a[r]),sum2+=a[r];
}
}
}
//r--;
//ans=max(r-l+1,ans)
if(sum1-now*s1.size()+now*s2.size()-sum2<=k)ans=max(r-l+1,ans);
else ans=max(r-l,ans);
int l1=l;
if(a[l]>now){
if(!s1.empty()){
auto p=s1.find(a[l]);
s1.erase(p);sum1-=a[l];
}
else now=a[l+1],l1++;
}
else if(a[l]<now){
if(!s2.empty()){
auto p=s2.find(a[l]);
s2.erase(p);sum2-=a[l];
}
else now=a[l+1],l1++;
}
else{
if(!s2.empty()){
now=*s2.rbegin();
s2.erase(s2.find(now));
sum2-=now;
}
else if(!s1.empty()){
now=*s1.begin();
s1.erase(s1.find(now));
sum1-=now;
}
else now=a[l+1];
}
l++;
}
cout<<ans<<'\n';
}
return 0;
}
详细
Test #1:
score: 100
Accepted
time: 0ms
memory: 3504kb
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
Runtime Error
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...