MT3037 新月轩就餐

 

 思路:

此题每道菜的价钱相同,想最小化付的钱即求最小区间长度可以满足“品尝到所有名厨手艺”。

使用双端队列存储元素,队尾不断向后遍历:头->尾

如果队头=队尾,则队头往右移一格,直到区间不同元素数=m。

#include <bits/stdc++.h>
using namespace std;
#define ll long long
const int N = 1e6 + 10, M = 2005;
const int INF = 0X3f3f3f3f;
deque<int> q;
int ans = INF;
int n, m, cnt[M], a[N], l, r, type;
int main()
{
    cin >> n >> m;
    for (int i = 1; i <= n; i++)
    {
        cin >> a[i];
        if (!cnt[a[i]])
            type++; // 此区间内有多少种不同的数
        cnt[a[i]]++;
        q.push_back(i);
        while (!q.empty() && cnt[a[q.front()]] > 1) // 如果此时队头元素的个数大于1,就pop掉
        {
            cnt[a[q.front()]]--;
            q.pop_front();
        }
        if (type == m)
        {
            if (q.size() < ans) // 最小化付的钱
            {
                ans = q.size();
                l = q.front();
                r = q.back();
            }
        }
    }
    cout << l << " " << r << endl;
    return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值