?
?思路:
此題每道菜的價錢相同,想最小化付的錢即求最小區間長度可以滿足“品嘗到所有名廚手藝”。
使用雙端隊列存儲元素,隊尾不斷向后遍歷:頭->尾
如果隊頭=隊尾,則隊頭往右移一格,直到區間不同元素數=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;
}