牛客-卷王之王

原题链接

思路用优先队列+结构体
优先队列从小往大排,按顺序拿出来操作,一旦超过x就退出。
主要难点在优先队列的运用

#include<bits/stdc++.h>
using namespace std;
const int N = 1e5 + 10; 

struct node{
    
    
	int a, i;

	//重载运算符,这样写是从大到小排,可是不知道为什么实际效果是从小往大
	bool operator< (const node & x) const {
    
    
		return a > x.a;
	}
};

//优先队列
priority_queue<node> q;
int num[N];

int main()
{
    
    
	int n, m;
	cin >> n >> m;
	for (int i = 1; i <= n; i ++ )
	{
    
    
		scanf("%d", &num[i]);
		q.push({
    
    num[i], i});
	}
	
	while (m -- )
	{
    
    
		int cnt = 0; 
		vector<int> f; //此处要开vector,否则会T 
		int x; scanf("%d", &x);
		if (x == 0) continue;
		
		while (!q.empty())
		{
    
    
			node t = q.top();
			if (t.a > x) break;
			num[t.i] += x;
			q.pop();
			f.push_back(t.i);
			cnt ++;
		}
		for (int i = 0; i < cnt; i ++ )
		{
    
    
			q.push({
    
    num[f[i]], f[i]});
		}
	}
	
	for (int i = 1; i <= n; i ++ )
	{
    
    
		printf("%d ", num[i]);
	}
	return 0;
}

Supongo que te gusta

Origin blog.csdn.net/qq_34682765/article/details/121366186
Recomendado
Clasificación