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思维题 线段合并