洛谷P1638 逛画展(贪心)

32 篇文章 0 订阅
18 篇文章 0 订阅

P1638 逛画展

题解:维护一个涵盖所有画师的滑动窗口,尽量让窗口内每种画的数量保持在 1 1 1,记录长度最短的窗口端点。

代码

#include<bits/stdc++.h>

using namespace std;
int n,m,d[1000010],now[2001];

int main()
{
#ifndef ONLINE_JUDGE
    freopen("input.in","r",stdin);
#endif
	cin>>n>>m;
	for(int i = 1; i <= n; ++i){
		cin>>d[i];
	}

	int front = 1, rear = 1, ans_X = 1, ans_Y = 1e7, cnt = 0;
	while(front <= rear){
		if(now[d[rear]] == 0) cnt++;
		if(rear<=n)
		now[d[rear++]]++;
		for( ; now[d[front]] > 1; front++)
			now[d[front]]--;
		if(cnt >= m){
			if(rear - front < ans_Y - ans_X)
				ans_X = front, ans_Y = rear;
		}
		if(rear == n + 1) break;
	}
	cout<<ans_X<<' '<<ans_Y-1<<endl;
    return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值