1893D - Colorful Constructive - CodeForces Solution


constructive algorithms data structures greedy *2600

Please click on ads to support us..

C++ Code:

#include<bits/stdc++.h>
using namespace std;
int n,m,a[200005],b[200005],c[200005],vis[200005],id[200005];
map<int,int> mp[200005];
struct node{
	int id,lim,cnt;
	bool operator <(const node &b) const
	{
		return min(lim,cnt)<min(b.lim,b.cnt);
	}
};
void output(int x)
{
	priority_queue<pair<int,int> > q;
	for(auto i:mp[x]) q.emplace(i.second,i.first)/*,cerr<<x<<" "<<i.first<<" "<<i.second<<"\n"*/;
	vector<pair<int,int> > f(b[x]+5);
	for(int i=1;i<=b[x];i++)
	{
		if(f[i].first!=0) q.emplace(f[i]);
		auto j=q.top();
		q.pop();
		cout<<j.second<<" ";
		j.first--;
		if(j.first!=0&&i+c[x]<=b[x]) f[i+c[x]]=j; 
	}
	cout<<"\n";
}
int main()
{
	int t;
	cin>>t;
	while(t--)
	{
		cin>>n>>m;
		for(int i=1;i<=n;i++)
		{
			cin>>a[i];
			vis[a[i]]++;
		}
		for(int i=1;i<=m;i++)
		{
			cin>>b[i];
		}
		for(int i=1;i<=m;i++)
		{
			cin>>c[i];
			// cerr<<"lim:"<<c[i]<<"\n";
		}
		set<pair<int,int> > s;
		for(int i=1;i<=n;i++)
		{
			if(vis[i]) s.insert({vis[i],i});
		}
		priority_queue<node> q;
		for(int i=1;i<=m;i++)
		{
			q.push({i,c[i],b[i]});
		}
		bool f=1;
		while(!q.empty())
		{
			auto [x,y,z]=q.top();//找出限制最大的架子
			q.pop();
			if(min(y,z)>s.size())
			{
				f=0;
				break ;
			}
			vector<pair<int,int> > _;
			for(int i=1;i<=min(y,z);i++)
			{
				auto it=prev(s.end());//优先把出现次数最多的分配给他
				auto t=*it;
				t.first--;
				_.emplace_back(t);
				mp[x][t.second]++;
				s.erase(it);
			}
			z-=min(y,z);
			if(z) q.push({x,y,z});
			for(auto i:_)
			{
				if(i.first) s.insert(i);
			}
		}
		if(!f) cout<<"-1\n";
		else
		{
			for(int i=1;i<=m;i++)
			{
				output(i);	
			}
		}
		for(int i=1;i<=m;i++)
		{
			mp[i].clear();
		}
		for(int i=1;i<=n;i++)
		{
			vis[i]=0;
		}
	}
}


Comments

Submit
0 Comments
More Questions

1475E - Advertising Agency
1345B - Card Constructions
1077B - Disturbed People
653A - Bear and Three Balls
794A - Bank Robbery
157A - Game Outcome
3B - Lorry
1392A - Omkar and Password
489A - SwapSort
932A - Palindromic Supersequence
433A - Kitahara Haruki's Gift
672A - Summer Camp
1277A - Happy Birthday Polycarp
577A - Multiplication Table
817C - Really Big Numbers
1355A - Sequence with Digits
977B - Two-gram
993A - Two Squares
1659D - Reverse Sort Sum
1659A - Red Versus Blue
1659B - Bit Flipping
1480B - The Great Hero
1519B - The Cake Is a Lie
1659C - Line Empire
515A - Drazil and Date
1084B - Kvass and the Fair Nut
1101A - Minimum Integer
985D - Sand Fortress
1279A - New Year Garland
1279B - Verse For Santa