zoj 3790 离散化

离散化一下然后贪心一下就好了

AC代码如下:

#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <set>
#include <vector>
#include <map>
using namespace std;

const int MAXN = 110000;

vector<int> s[MAXN];
set<int> myset;
map<int,int> mp;
int N, K, a[MAXN];
set<int>::iterator p;

int calc( int g ){
    int ans, now;
    int ft, ls;
    ft = ls = 0;
    ans = 1;
    now = 0;
    while( 1 ){
        while( now <= K && ls < s[g].size() && ft < s[g].size() ){
            if( ls == s[g].size() - 1 ){
                break;
            }
            int temp = s[g][ls+1] - s[g][ls] - 1;
			now += temp;
			ls++;
            if( now <= K ){
				ans = max( ans, ls - ft + 1 );
            }
        }
		if( ls == s[g].size() - 1 ){
			while( now > K ){
				now -= s[g][ft+1] - s[g][ft] - 1;
				ft++;
			}
			ans = max( ans, ls - ft + 1 );
			break;
		}
		while( now > K ){
			now -= s[g][ft+1] - s[g][ft] - 1;
			ft++;
		}
    }
    return ans;
}

int main(){

    while( scanf( "%d%d", &N, &K ) != EOF ){
        myset.clear();
		mp.clear();
        for( int i = 1; i <= N; i++ ){
            scanf( "%d", &a[i] );
            myset.insert( a[i] );
        }
        int top;
        for( p = myset.begin(), top = 1; p != myset.end(); p++, top++ ){
            mp.insert( pair<int,int>( *p, top ) );
            s[top].clear();
        }
        for( int i = 1; i <= N; i++ ){
			a[i] = mp.find( a[i] )->second;
            s[a[i]].push_back( i );
        }
        int ans = 0;
        for( int i = 1; i < top; i++ ){
            ans = max( ans, calc( i ) );
        }
        printf( "%d\n", ans );
    }

    return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值