Codeforces1102B 结构体排序位置

B
只有k种颜色 相同值不能用同一种颜色 k种颜色都得用
默认按照值 有小到大排序

#include <bits/stdc++.h>
#define rep(i,a,n) for (int i=a;i<=n;i++)
#define pb push_back
#define mp make_pair
#define IO ios::sync_with_stdio(false)
#define fi first
#define se second
using namespace std;
const int maxn=3e5+5;
const int INF=0x3f3f3f3f;
typedef long long ll;
typedef pair<int,int> pii;

string s,t;
int n,k,a[maxn],vis[maxn];
pii b[maxn];
//376EF
int main(){
	
	cin>>n>>k;
	for(int i=1;i<=n;i++){
		cin>>a[i];
		b[i]=mp(a[i],i);
		vis[a[i]]++;
	}
	sort(b+1,b+n+1);
	for(int i=1;i<=5000;i++){
		if(vis[i]>k)
			return cout<<"NO"<<endl,0;
	}
	int now=1;
	for(int i=1;i<=n;i++){
		if(now>k)	 //3 2 1 2 3 
			now=1;	// 1 2 2 3 3
					// 2 2 1 1 1
		if(b[i].fi!=b[i-1].fi){
			
			a[b[i].se]=now;
			now++;
		}
		else{
		
			a[b[i].se]=now;
			now++;
		}
	}
	cout<<"YES"<<endl;
	for(int i=1;i<=n;i++)
		cout<<a[i]<<" ";
	cout<<endl;
	return 0;
}

D字符串平衡

string s,t;
int n,k,a[maxn],vis[maxn];
pii b[maxn];
//376EF
int main(){
	
	cin>>n;
	cin>>s;
	int c0=0,c1=0,c2=0,m=n/3;
	for(int i=0;i<n;i++){
		if(s[i]=='0')
			c0++;
		else if(s[i]=='1')
			c1++;
		else
			c2++;
	}
	c0-=m;
	c1-=m;
	c2-=m;
	if(c2<0){
		for(int i=n-1;i>=0 && c2<0;i--){
			if(s[i]=='1' && c1>0){
				c1--;
				c2++;
				s[i]='2';
			}
			if(s[i]=='0' && c0>0){
				c0--;
				c2++;
				s[i]='2';
			}
		}
		
	}
	if(c0<0){
		for(int i=0;i<n && c0<0;i++){
			if(s[i]=='1' && c1>0){
				c1--;
				c0++;
				s[i]='0';
			}
			if(s[i]=='2' && c2>0){
				c2--;
				c0++;
				s[i]='0';
			}
		}
		
	}
	
	if(c1<0){
		for(int i=0;i<n&& c1<0;i++){
			if(s[i]=='2'&& c2>0){
				c2--;
				c1++;
				s[i]='1';
			}	
		}
		for(int i=n-1;i>=0 && c1<0;i--){
			if(s[i]=='0' && c0>0){
				s[i]='1';
				c0--;
				c1++;
			}
		}
	}
	
	cout<<s<<endl;
	return 0;
}

E思维题 线段合并

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值