代码打印

2025-cpc-17 2025-05-18 15:58:42

#include <bits/stdc++.h>
#define int long long
using namespace std;
int n,m,ANS,top=0;
int b[1000],a[1000],x[1000],y[1000],z[1000];
int Next[1000];
bool nextend[1000];
struct Q
{
	int num;
	int next;
};
priority_queue<Q>q;
bool operator<(Q a,Q b)//?
{
	return a.next>b.next;
}
signed main()
{
	ios::sync_with_stdio(false);
	cin.tie(0);
	int T;
	cin>>T;
	while(T--)
	{
		cin>>n>>m;
		ANS=0;
		memset(nextend,0,sizeof(nextend));
		memset(Next,0,sizeof(Next));
		while(!q.empty()) q.pop();
		for(int i=1;i<=n;i++) cin>>a[i];
		for(int i=1;i<=n;i++) cin>>x[i];
		for(int i=1;i<=n;i++) cin>>y[i];
		for(int i=1;i<=n;i++) cin>>z[i];
		
		for(int i=1;i<=n;i++)
		{
			bool flag=0;
			for(int j=i+1;j<=n;j++)
			{
				if(a[j]==a[i]) 
				{
					flag=1;
					Next[i]=j;
					break;
				}
			}
			if(!flag)
			{
				nextend[i]=1;
			}
		}
		
		for(int i=1;i<=n;i++)
		{
			bool flag=0;
			Q temp[1005];
			int tempcnt=0;
			while(!q.empty())
			{
				temp[++tempcnt]=q.top();
				q.pop();
				if(temp[tempcnt].num==a[i])
				{
					flag=1;
					ANS+=x[i];
					if(nextend[i]) 
					{
						tempcnt--;
					}
					else temp[tempcnt].next=Next[i];
					break;
				}
			}
			for(int jj=1;jj<=tempcnt;jj++)
			{
				q.push(temp[jj]);
			}
			if(!flag)
			{
				if(Nextend[i]) continue;
				else if(q.size()<m)//?
				{
					q.push((Q){a[i],Next[i]});
				}
				else if(q.top().next>next[i])
				{
					q.pop();
					q.push((Q){a[i],Next[i]});
				}
			}
		}
		cout<<ANS<<endl;
	}
	return 0;
}
/*
1
8 2
3 2 1 1 2 3 3 3
1 1 1 1 1 1 1 1
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
*/

共 1 条回复

2025-cpc-root

1