concatena appoggiandoti ad una variabile globale (se ti occorre tenere separate combinazioni diverse appoggiati ad un array) e stampa solo alla fine

x es.
var str='';
function a(){
str+='<p style="background:'+CN[k]+'"></p>';
}
function b(){
str+='<p style="background:'+CP[k]+'"></p>';
}
function scrivi(){
document.write(str);
}