bzoj1307: 玩具

6 篇文章 0 订阅

好像数据很弱……

但我还是写了个较正常的乱搞解法?

考虑到这联续k个必定为1……k排列才能更新答案

我们先找到所有1的位置,然后在一个1到左右两个1的整个区间里扫描判重统计,具体参照代码

大概就是O(nlogn)的时间复杂度了

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<cstring>
#include<string>
#include<map>
#include<algorithm>
#include<queue>
using namespace std;
int n;
int a[1000005],b[1000005];
int e[1000005];
queue<int>que;
priority_queue<int>Q;
int cnt=0,ans=0,tot=0;
int main()
{
    int i,j,k,t,l;
    scanf("%d",&n);
    for(i=1;i<=n;i++){
        scanf("%d",&a[i]);
        if(a[i]==1){cnt++;b[cnt]=i;}
        }
    b[cnt+1]=n+1;
    for(i=1;i<=cnt;i++){
        if(b[i+1]-1-b[i-1]>ans){
        for(j=b[i+1]-1;j>b[i-1];j--){
            if(e[a[j]]==0){
               Q.push(a[j]);
               que.push(a[j]);
               e[a[j]]=1;
               tot++;
               }
            else{
                 while(que.front()!=a[j]){
                      while(!e[Q.top()])Q.pop();
                         k=Q.top();
                      if(k==tot)ans=max(k,ans);
                      t=que.front();
                      e[t]=0;tot--;que.pop();
                      }
                 }
            }
         if(tot!=0){
            if(e[1]==1)if(Q.top()==tot)ans=max(ans,Q.top());
            while(!que.empty()){t=que.front();e[t]=0;tot--;que.pop();}
            while(!Q.empty())Q.pop();
            }
         }
        }
    printf("%d",ans);
    return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值