349 - Transferable Voting (II)
Posted: Tue Oct 22, 2002 6:56 pm
I see nothing special with this problem, but I got wrong answer.
I tested with the test sets given and they are correct.
Are there any test cases which will not work for me?
Thank You.
Here is my source (if you want to take a look):
[cpp]// ACM Problem 349
// Transferable Voting (II)
// Done by Teh Ming Han
#include <stdio.h>
int main(){
int c,n,i,a,elec=0,b;
while(scanf("%d %d",&c,&n)==2){
if (c==0 && n==0) break;
elec++;
int nbad=0,ngood=0,good[100][5]={0};
for (i=1;i<=n;i++){
int check[5]={0};
int wrong=0;
int temp[5]={0};
for (a=1;a<=c;a++){ //read voters choices
scanf("%d",&temp[a]);
//check that vote is distinct and within candidates limit
if (check[temp[a]]==0 && temp[a]<=c) check[temp[a]]++;
else wrong = 1;
}
if (wrong==1) nbad++;
else{ //valid votes
ngood++;
for (a=1;a<=c;a++) good[ngood][a] = temp[a];
good[ngood][0] = c;
}
}
int towin,tie,can[5]={0};
towin = int(ngood/2)+1;
for (i=1;i<=c;i++){ //rounds
int max=0,votes[5]={0},nout=0,out[5]={0};
tie=0;
for (a=0;a<=5;a++) can[a]=0;
for (a=1;a<=ngood;a++){
votes[good[a][1]]++;
if (votes[good[a][1]]>max) max = votes[good[a][1]];
}
for (a=1;a<=c;a++){
if (votes[a]==max || votes[a]>=towin){
tie++;
can[tie] = a;
}else{
nout++;
out[nout] = a;
}
}
for (a=1;a<=ngood;a++)
for (b=1;b<=nout;b++)
if (good[a][1]==out){
good[a][0]--;
for (b=1;b<=good[a][0];b++) good[a] = good[a][b+1]; //advance votes
}
if (nout==0 || tie==1) break;
}
//output
printf("Election #%d\n",elec);
printf(" %d bad ballot(s)\n",nbad);
if (tie==1) printf(" Candidate %d is elected.\n",can[1]);
else{
printf(" The following candidates are tied:");
for (i=1;i<=tie;i++) printf(" %d",can);
printf("\n");
}
}
return 0;
}[/cpp]
Once again, thank you.
I tested with the test sets given and they are correct.
Are there any test cases which will not work for me?
Thank You.
Here is my source (if you want to take a look):
[cpp]// ACM Problem 349
// Transferable Voting (II)
// Done by Teh Ming Han
#include <stdio.h>
int main(){
int c,n,i,a,elec=0,b;
while(scanf("%d %d",&c,&n)==2){
if (c==0 && n==0) break;
elec++;
int nbad=0,ngood=0,good[100][5]={0};
for (i=1;i<=n;i++){
int check[5]={0};
int wrong=0;
int temp[5]={0};
for (a=1;a<=c;a++){ //read voters choices
scanf("%d",&temp[a]);
//check that vote is distinct and within candidates limit
if (check[temp[a]]==0 && temp[a]<=c) check[temp[a]]++;
else wrong = 1;
}
if (wrong==1) nbad++;
else{ //valid votes
ngood++;
for (a=1;a<=c;a++) good[ngood][a] = temp[a];
good[ngood][0] = c;
}
}
int towin,tie,can[5]={0};
towin = int(ngood/2)+1;
for (i=1;i<=c;i++){ //rounds
int max=0,votes[5]={0},nout=0,out[5]={0};
tie=0;
for (a=0;a<=5;a++) can[a]=0;
for (a=1;a<=ngood;a++){
votes[good[a][1]]++;
if (votes[good[a][1]]>max) max = votes[good[a][1]];
}
for (a=1;a<=c;a++){
if (votes[a]==max || votes[a]>=towin){
tie++;
can[tie] = a;
}else{
nout++;
out[nout] = a;
}
}
for (a=1;a<=ngood;a++)
for (b=1;b<=nout;b++)
if (good[a][1]==out){
good[a][0]--;
for (b=1;b<=good[a][0];b++) good[a] = good[a][b+1]; //advance votes
}
if (nout==0 || tie==1) break;
}
//output
printf("Election #%d\n",elec);
printf(" %d bad ballot(s)\n",nbad);
if (tie==1) printf(" Candidate %d is elected.\n",can[1]);
else{
printf(" The following candidates are tied:");
for (i=1;i<=tie;i++) printf(" %d",can);
printf("\n");
}
}
return 0;
}[/cpp]
Once again, thank you.