Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
branch: master
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 84 lines (69 sloc) 3.192 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <list>
#include <queue>
#include <string>

using namespace std;

struct tStateData {
int nItem;
double fVal;
int nRepresentatives;

tStateData(int nItem = 0, double fVal = 0, int nRepresentatives = 0)
: nItem(nItem), fVal(fVal), nRepresentatives(nRepresentatives) {
;
}

friend bool operator <(const tStateData &lhs, const tStateData &rhs) {
return lhs.fVal < rhs.fVal;
}
};
int main() {
const pair<string, int> aInitialData[] = {
{"Alabama" , 4779736} , {"Alaska" , 710231}
, {"Arizona" , 6392017} , {"Arkansas" , 2915918} , {"California" , 37253956}
, {"Colorado" , 5029196} , {"Connecticut" , 3574097} , {"Delaware" , 897934}
, {"Florida" , 18801310} , {"Georgia" , 9687653} , {"Hawaii" , 1360301}
, {"Idaho" , 1567582} , {"Illinois" , 12830632} , {"Indiana" , 6483802}
, {"Iowa" , 3046355} , {"Kansas" , 2853118} , {"Kentucky" , 4339367}
, {"Louisiana" , 4533372} , {"Maine" , 1328361} , {"Maryland" , 5773552}
, {"Massachusetts" , 6547629} , {"Michigan" , 9883640} , {"Minnesota" , 5303925}
, {"Mississippi" , 2967297} , {"Missouri" , 5988927} , {"Montana" , 989415}
, {"Nebraska" , 1826341} , {"Nevada" , 2700551} , {"New Hampshire" , 1316470}
, {"New Jersey" , 8791894} , {"New Mexico" , 2059179} , {"New York" , 19378102}
, {"North Carolina" , 9535483} , {"North Dakota" , 672591} , {"Ohio" , 11536504}
, {"Oklahoma" , 3751351} , {"Oregon" , 3831074} , {"Pennsylvania" , 12702379}
, {"Rhode Island" , 1052567} , {"South Carolina" , 4625364} , {"South Dakota" , 814180}
, {"Tennessee" , 6346105} , {"Texas" , 25145561} , {"Utah" , 2763885}
, {"Vermont" , 625741} , {"Virginia" , 8001024} , {"Washington" , 6724540}
, {"West Virginia" , 1852994} , {"Wisconsin" , 5686986} , {"Wyoming" , 563626}
};

priority_queue<tStateData> aQueue;
tStateData tmpStateData;

for (int i = 0; i != sizeof(aInitialData) / sizeof(pair<string, int>); ++i) {
aQueue.push({i, aInitialData[i].second / sqrt(3), 1});
}

for (int i = 0; i < 385; ++i) {
tmpStateData = aQueue.top();
aQueue.pop();

tmpStateData.nRepresentatives += 1;
tmpStateData.fVal = aInitialData[tmpStateData.nItem].second;
tmpStateData.fVal /= sqrt(tmpStateData.nRepresentatives * (tmpStateData.nRepresentatives + 1));

aQueue.push(tmpStateData);
}

list<pair<string, int>> aResults;

while (!aQueue.empty()) {
tmpStateData = aQueue.top();
aQueue.pop();
aResults.push_back({aInitialData[tmpStateData.nItem].first, tmpStateData.nRepresentatives});
}

aResults.sort(
[ ](const pair<string, int> &lhs, const pair<string, int> &rhs) {
return lhs.second > rhs.second;
}
);

for (auto it = aResults.begin(); it != aResults.end(); ++it) {
cout << setw(20) << left << it->first << it->second << endl;
}

return 0;
}
Something went wrong with that request. Please try again.