int main() { int n, m; scanf("%d%d", &n, &m); vector<int> a(n); int max_val = 0; int max_idx = -1; int max_value = 0; unordered_map<int, int> map; for(int i=0; i<n; i++) { scanf("%d", &a[i]); map[a[i]] = 0; if(a[i]>max_value) { max_value = a[i]; max_idx = i; } } //cout<<max_idx<<endl; if(max_idx<m) { cout<<max_idx-1+m<<endl; return 0; } int cur = 0; while(max_val!=m) { cur++; int t1 = a[0], t2 = a[1]; if(a[0]<a[1]) { a.erase(a.begin()); a.push_back(t1); map[t2]++; max_val = max(max_val, map[t2]); } else { a.erase(a.begin()+1); a.push_back(t2); map[t1]++; max_val = max(max_val, map[t1]); } } printf("%d\n", cur); return 0; }
点赞 评论
牛客网
牛客企业服务