Problem A. Candies Google Kickstart Round D 2018

这一题最开始naive地O(N^2)枚举子序列,然后跪了。题目看起来和sliding window有关, 因为枚举子序列的过程中,constraint和objective都是一样的,而且两个子序列会有很多重合部分。

对于small input,sweetness>=0。令sweet[l,r]是[l+1,...,r]区间的sweetness之和,odd[l,r]是[l+1,...,r]区间的odd candies个数。那么固定l,sweet,odd都是随r递增的。所以如果已经枚举得到了区间[l,r]的值,r为最远的满足constraint的index(Then it maximizes objective),对于l'>l,只需要将r'从r之后开始枚举即可。因为对于r'<=r,必定满足sweetness and odd constraints.

对于large input,sweet[l,r]不是递增的,但是odd[l,r]仍有递增性,仍然可以用sliding window technique.

如果sweet[l,r]超出了odd D, 将global l右移即可。对于符合odd constriant的区间[l,r],可以通过multiset的lower_bound在logN得到,l,r such that sweet[l,r]<=D and is maximized. lower_bound(x)返回的是把元素x插入set中最小位置: y|y>=x,upper_bound(x)返回的是把元素x插入set中最大位置:y|y>x。

对于每一个r,可以maintain一个global l,得到[l,r]满足odd constraint且l最小。因为odd[l,r]的单调性,l只需要从1至N移动一次即可。得到[l,r]之后,我们需要找到l使得sweetness[0,r]-sweetness[0,l]<=D,所以sweetness[0,l]>=sweetness[0,r]-D,是一个upper bound。因为我们只需要实现将前缀和存在set中即可。本来我想的是枚举l,针对每个l找到符合条件的r,但是发现upper bound不能直接用,然后想着怎么改cmp函数。。。o(╯□╰)o可能把cmp大于小于互换就行了?

另外要注意一下presum[r]-presum[l]对应的区间不包括l。

#include<iostream>
#include<stdio.h>
#include<cstdio>
#include<stdlib.h>
#include<vector>
#include<string>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<stack>
#include<queue>
#include<ctype.h>
#include<map>
#include<time.h>
#include<set>
#include<bitset>
#include<sstream>
using namespace std;
//2018 round D Problem A. Candies
const int maxn=500010;
int T;
int N;
long long O;
long long D;
long long A;
long long B;
long long C;
long long M;
long long L;
long long X[maxn];
long long S[maxn];
long long preodd[maxn]; //# of odd candies from 1 to i
long long presum[maxn]; //pre sum of sweetiness from 1 to i
long long odd[maxn];
long long X1;
long long X2;
int far_ro[maxn]; //max R for L such that SO[L,R]==O
multiset<long long>window=multiset<long long>(); //sliding window
int flg;
long long ans;
void erase_one(multiset<long long>&s, long long x)
{
    multiset<long long>::iterator it=s.find(x);
    s.erase(it);
}
void test_set()
{
    multiset<int>test;
    test.insert(1);
    test.insert(2);
    test.insert(3);
    test.insert(3);
    auto x=test.find(3);
    test.erase(x);
    for(auto i=test.begin();i!=test.end();i++)
    {
        cout<<*i<<endl;
    }
}
int main()
{

    cin>>T;
    for(int ca=1;ca<=T;ca++)
    {
        cin>>N>>O>>D;
//        cout<<N<<" "<<O<<" "<<D<<endl;
        cin>>X1>>X2>>A>>B>>C>>M>>L;
//        cout<<X1<<" "<<X2<<" "<<A<<" "<<B<<" "<<C<<" "<<M<<" "<<L<<endl;
        memset(X,0,sizeof(X));
        memset(S,0,sizeof(S));
        memset(preodd,0,sizeof(preodd));
        memset(presum,0,sizeof(presum));
        memset(odd,0,sizeof(odd));
        window.clear();
        flg=false;
        ans=0;
        X[1]=X1;
        X[2]=X2;
        for(int i=3;i<=N;i++)
        {
            X[i]=A*X[i-1]%M+B*X[i-2]%M+C%M;
            X[i]%=M;
            
        }
        for(int i=1;i<=N;i++)
        {
            S[i]=X[i]+L;
            presum[i]=presum[i-1]+S[i];
            if(S[i]%2!=0)
            {
                preodd[i]=preodd[i-1]+1;
                odd[i]=1;
            }
            else
            {
                preodd[i]=preodd[i-1];
            }
        }
//        for(int i=0;i<=N;i++)
//        {
//            cout<<S[i]<<" ";
//        }
//        cout<<endl;
//        for(int i=0;i<=N;i++)
//        {
//            cout<<presum[i]<<" ";
//        }
//        cout<<endl;
//        for(int i=0;i<=N;i++)
//        {
//            cout<<preodd[i]<<" ";
//        }
//        cout<<endl;
        
        // fix R, moving L forward
        int preL=1;
        long long curr_odd=0;
//        window.insert(presum[0]);
        for(int i=1;i<=N;i++)
        {
//            cout<<i<<" "<<preL<<endl;
            window.insert(presum[i-1]);
//            cout<<"insert"<<endl;
            curr_odd=preodd[i]-preodd[preL-1];
//            cout<<"curr odd "<<curr_odd<<endl;
            while(curr_odd>O)
            {
                erase_one(window,presum[preL-1]);
                curr_odd-=odd[preL];
                preL++;
            }
//            cout<<"curr odd "<<curr_odd<<" preL "<<preL<<endl;
            multiset<long long>::iterator it=window.lower_bound(presum[i]-D);
            if(it==window.end())
            {
                continue;
            }
//            cout<<"min S[L] "<<(*it)<<endl;
            if(flg==false)
            {
                flg=true;
                ans=presum[i]-(*it);
            }
            else
            {
                ans=max(ans,presum[i]-(*it));
            }
            
        }
        if(flg==false)
        {
            printf("Case #%d: IMPOSSIBLE\n",ca);
        }
        else
        {
            printf("Case #%d: %lld\n",ca,ans);
        }
        
    }
    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值