题目链接
题解
简单的贪心题
优先与上一个不一样且数量最多的,如果有多个相同,则优先选择非结尾颜色
比较显然,但不知怎么证
#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
#include<queue>
#include<cmath>
#include<map>
#define Redge(u) for (int k = h[u],to; k; k = ed[k].nxt)
#define REP(i,n) for (int i = 1; i <= (n); i++)
#define mp(a,b) (cp){a,b}
#define cls(s) memset(s,0,sizeof(s))
#define LL long long int
using namespace std;
const int maxn = 1000005,maxm = 100005,INF = 1000000000;
inline int read(){
int out = 0,flag = 1; char c = getchar();
while (c < 48 || c > 57){if (c == '-') flag = -1; c = getchar();}
while (c >= 48 && c <= 57){out = (out << 3) + (out << 1) + c - 48; c = getchar();}
return out * flag;
}
int ans[maxn],n,s,t,m;
struct cp{
int cnt,id;
};
inline bool operator <(const cp& a,const cp& b){
return a.cnt == b.cnt ? a.id != t : a.cnt < b.cnt;
}
priority_queue<cp> q;
int main(){
n = read(),s = read(),t = read();
if (n == 1 && (s != t)){puts("0"); return 0;}
int tmp;
REP(i,n){
tmp = read(); m += tmp;
if (i == s) tmp--;
if (i == t) tmp--;
if (tmp == -1){puts("0"); return 0;}
if (tmp) q.push(mp(tmp,i));
}
cp u,v;
ans[1] = s; ans[m] = t;
for (int i = 2; i < m; i++){
u = q.top(); q.pop();
if (u.id == ans[i - 1]){
if (q.empty()){puts("0"); return 0;}
v = q.top(); q.pop();
ans[i] = v.id; v.cnt--;
q.push(u);
if (v.cnt) q.push(v);
}
else {
ans[i] = u.id; u.cnt--;
if (u.cnt) q.push(u);
}
}
if (ans[m - 1] == t){puts("0"); return 0;}
for (int i = 1; i <= m; i++)
printf("%d ",ans[i]);
return 0;
}