// gerry.cpp : Defines the entry point for the console application. // #include #include #include using namespace std; int main(int argc, char* argv[]) { int i, j, k; ifstream input; input.open("gerry.in"); int numRuns, numPrects; int ** precVotes; int * diffs; bool majors; int totalMe, totalOpp; input >> numRuns; for (int run = 0; run < numRuns; run++) { totalMe = 0; totalOpp = 0; majors = false; int smallDiffPrec = 0; cout << "Data Set " << run+1 << ":\n"; input >> numPrects; precVotes = new int * [2]; precVotes[0] = new int[numPrects]; precVotes[1] = new int[numPrects]; diffs = new int[numPrects]; for (i = 0; i < numPrects; i++) { input >> precVotes[0][i] >> precVotes[1][i]; if (precVotes[0][i] > precVotes[1][i]) { majors = true; /* if ((precVotes[0][i] - precVotes[1][i]) < (precVotes[0][smallDiffPrec] - precVotes[1][smallDiffPrec])) { smallDiffPrec = i; cerr << "smalldiff is " << i << endl; }*/ } totalMe += precVotes[0][i]; totalOpp += precVotes[1][i]; diffs[i] = precVotes[0][i] - precVotes[1][i]; } //cout << "me " << precVotes[0][smallDiffPrec] << " " << totalMe - precVotes[0][smallDiffPrec] << endl; //cout << "opp " << precVotes[1][smallDiffPrec] << " " << totalOpp - precVotes[1][smallDiffPrec]<< endl; if (!majors) { cout << "0" << endl; continue; } bool sorted = false; while(!sorted) { sorted = true; int temp; for (i = 0; i < numPrects-1; i++) { if (diffs[i] < diffs[i+1]) { temp = diffs[i]; diffs[i] = diffs[i+1]; diffs[i+1] = temp; temp = precVotes[0][i]; precVotes[0][i] = precVotes[0][i+1]; precVotes[0][i+1] = temp; temp = precVotes[1][i]; precVotes[1][i] = precVotes[1][i+1]; precVotes[1][i+1] = temp; sorted = false; } } } int * smallDiffArr; smallDiffArr = new int [numPrects]; smallDiffArr[0] = (precVotes[0][0] > precVotes[1][0]) ? precVotes[0][0] : 0; //cout << smallDiffArr[0] << endl; for (i = 1; i < numPrects; i++) { int curDiff = precVotes[0][i] - precVotes[1][i]; //int prevDiff = precVotes[0][i - 1] - precVotes[1][i - 1]; /*if (curDiff < smallestDiff && curDiff > 0) { smallestDiff = curDiff; } else if (curDiff + smallestDiff < smallestDiff && (curDiff + smallestDiff*/ if (curDiff < smallDiffArr[i-1] && curDiff > 0) smallDiffArr[i] = curDiff; else if (abs(curDiff + smallDiffArr[i - 1]) < abs (smallDiffArr[i - 1])) //else if (smallDiffArr[i-1] + curDiff < smallDiffArr[i-1] && smallDiffArr[i-1] + curDiff > 0) { smallDiffArr[i] = smallDiffArr[i - 1] + curDiff; } else smallDiffArr[i] = smallDiffArr[i - 1]; //cerr << smallDiffArr[i] << endl; } //if ((totalMe - precVotes[0][smallDiffPrec]) > (totalOpp - precVotes[1][smallDiffPrec])) if (smallDiffArr[numPrects-1] < totalMe - totalOpp && smallDiffArr[numPrects-1] > 0) cout << "2" << endl; else cout << "1" << endl; } return 0; }