I need help!
查看原帖
I need help!
1189823
liborui0000楼主2024/7/26 21:08

10分求调

#include <bits/stdc++.h>

using namespace std;

int n, m;
int a[1000000];
queue<int> q;
queue<int> q1;
int ans;

int main(){
	cin >> n >> m;
	for (int i = 1; i <= m; i++){
		cin >> a[i];
	}
//	cout << q.size() << endl;
	for (int i = 1; i <= m; i++){		
		if (q.size() == 0){
			q.push(a[i]);
			ans++;
//			cout << a[i] << endl;
		}
		else{
			int sum = 0;
			for (int j = 1; j <= q.size(); j++){
				if (a[i] == q.front()) break;
				else{
					q1.push(q.front());
					q.pop();
					sum++;
				}
			}
			for (int j = 1;j <= q1.size(); j++){
				q.push(q1.front());
				q1.pop();
			}
			if (sum == q.size()){
				if (q.size() == n){
					q.pop();
					q.push(a[i]);
					ans++;
				}
				else if (q.size() < n){
					q.push(a[i]);
					ans++;
				}
			}
		}	
	}
	cout << ans << endl;
	return 0;
}
2024/7/26 21:08
加载中...