This is my first post here so hello !
My code is accepted on programming challenges but not on uva online judge.
I've passed all the test cases I found here in this topic successfully. I've also tried uvatoolkit succesfully too.
Here is my code if any one could find the problem I'll be grateful
Code: Select all
// Australian Voting
#include <iostream>
#include <map>
#include <vector>
#include <sstream>
#include <algorithm>
using namespace std;
void readCandidates(map<int, string> &cand, int n){
int c=0;
while(c<n){
string str;
getline(cin, str);
if(str.empty())
continue;
++c;
cand[c] = str;
}
}
void printCandidates(map<int, string> &cand){
for(map<int, string>::iterator it=cand.begin() ; it!=cand.end() ; it++)
//cout << it->second << " : " << it->first << endl;
cout << it->second << endl;
}
void readBallots(vector<vector<int> > &ball, int n){
while(true){
string str;
getline(cin, str);
if(str.empty())
break;
vector<int> b;
b.push_back(1);
stringstream ss(str);
int x;
for(int i=0 ; i<n ; i++){
ss >> x;
b.push_back(x);
}
ball.push_back(b);
}
}
void printBallots(vector<vector<int> > &ball){
cout << "Ballots:" << endl;
for(int i=0 ; i<ball.size() ; i++){
cout << "(" << ball[i][0] << ") ";
for(int j=1 ; j<ball[i].size() ; j++)
cout << ball[i][j] << " ";
cout << endl;
}
cout << endl;
}
void initTable(vector<pair<int, int> > &r, int n){
for(int i=1 ; i<=n ; i++)
r.push_back(make_pair(0, i));
}
void printTable(vector<pair<int, int> > &r){
cout << "Results:" << endl;
for(int i=0 ; i<r.size() ; i++)
cout << r[i].second << " : " << r[i].first << endl;
cout << endl;
}
int total(vector<pair<int, int> > &r){
int sum = 0;
for(int i=0 ; i<r.size() ; i++)
sum += r[i].first;
//if(!sum) cerr << "sum can not equal zero" << endl;
return sum;
}
int rfind(vector<pair<int, int> > &r, int val){
for(int i=0 ; i<r.size() ; i++)
if(r[i].second == val)
return i;
return -1;
}
void initResults(vector<pair<int, int> > &r, vector<vector<int> > &b){
for(int i=0 ; i<b.size() ; i++){
int idx = rfind(r, b[i][1]);
//if(idx==-1) cerr << "index can not equal -1" << endl;
if(idx!=-1)
r[idx].first++;
}
}
double ratio(vector<pair<int, int> > &r, int k){
int sum = total(r);
return ((r[k].first * 1.0) / (double)sum) * 100.0;
}
vector<string> getWinners(vector<pair<int, int> > &r, map<int, string> &c){
vector<int> above;
vector<string> names;
for(int i=0 ; i<r.size() ; i++){
if(ratio(r, i) >= 50)
above.push_back(r[i].second);
}
int max = -1;
for(int i=0 ; i<above.size() ; i++)
if(r[rfind(r, above[i])].first > max)
max = r[rfind(r, above[i])].first;
for(int i=0 ; i<above.size() ; i++){
if(r[rfind(r, above[i])].first == max)
names.push_back(c[r[rfind(r, above[i])].second]);
}
return names;
}
int minResults(vector<pair<int, int> > &r){
int min = r[0].first;
for(int i=1 ; i<r.size() ; i++)
if(r[i].first < min)
min = r[i].first;
return min;
}
bool isTie(vector<pair<int, int> > &r){
int v = r[0].first;
for(int i=0 ; i<r.size() ; i++)
if(r[i].first != v)
return false;
return true;
}
void prune(vector<pair<int, int> > &r, vector<vector<int> > &b){
int min = minResults(r);
for(int i=0 ; i<r.size() ; i++){
if(r[i].first == min){
int del = r[i].second;
for(int j=0; j<b.size() ; j++){
if(b[j][b[j][0]] == del){
bool found = false;
while(!found){
++b[j][0];
int next = b[j][b[j][0]];
int idx = rfind(r, next);
if(idx != -1){
if(r[idx].first == min)
continue;
r[idx].first++;
found = true;
}
}
}
}
int idx = rfind(r, del);
r.erase(r.begin()+idx, r.begin()+idx+1);
}
}
}
int main(){
int t;
cin >> t;
while(t--){
int n;
cin >> n;
map<int, string> cand;
readCandidates(cand, n);
vector<vector<int> > ball;
readBallots(ball, n);
vector<pair<int, int> > results;
initTable(results, n);
initResults(results, ball);
vector<string> w = getWinners(results, cand);
if(!w.empty()){
for(int i=0 ; i<w.size() ; i++)
cout << w[i] << endl;
if(t) cout << endl;
continue;
}
if(isTie(results)){
printCandidates(cand);
if(t) cout << endl;
continue;
}
while(true){
prune(results, ball);
vector<string> w = getWinners(results, cand);
if(!w.empty()){
for(int i=0 ; i<w.size() ; i++)
cout << w[i] << endl;
break;
}
if(isTie(results)){
printCandidates(cand);
break;
}
}
if(t) cout << endl;
}
return 0;
}