10-分 剩下全wa
查看原帖
10-分 剩下全wa
947873
RedWen_shuo楼主2024/7/11 17:22

求明人指点

#include<bits/stdc++.h>
#define ll long long
#define pri priority_queue<ll>
#define prig priority_queue<ll, vector<ll>, greater<ll> >

using namespace std;

const int N = 5e5 + 1000;

struct node{
	ll x, next;
	
	bool operator < (const node &a) const{
		return a.next > next;
	}
};

ll n, m, k, ans;
ll a[N], vis[N], v[N];
priority_queue<node> q;

int main(){
//	ios::sync_with_stdio(0);
//	cin.tie(0); cout.tie(0);

	cin >> n >> k >> m;
	for (int i = 1; i <= m; i ++) cin >> a[i], v[i] = 1e8;
	for (int i = m; i >= 1; i --){
		q.push((node){a[i], v[a[i]]}), v[a[i]] = i;
//		cout << a[i] << " " << v[a[i]] << "\n";
	}
	
//	for (int i = 1; i <= m; i ++) cout << q.top().next << " ", q.pop(); 

	for (int i = 1, cnt = 0; i <= m; i ++){
		if (!vis[a[i]] && cnt < k){
//			cout << 1 << " ";
			vis[a[i]] = 1;
			cnt ++;
			ans ++;
//			cout << cnt << " ";
		}else if (!vis[a[i]] && cnt == k){
			vis[q.top().x] = 0, q.pop();
			vis[a[i]] = 1;
			ans ++;
		}
		
	}

	cout << ans;

	return 0;
}

采用 堆优化 + 贪心


后面全wa了

2024/7/11 17:22
加载中...