Skip to content

Instantly share code, notes, and snippets.

@lessandro
Created April 20, 2012 05:00
Show Gist options
  • Select an option

  • Save lessandro/2426130 to your computer and use it in GitHub Desktop.

Select an option

Save lessandro/2426130 to your computer and use it in GitHub Desktop.
puzzle #3 (ugly code (also wrong))
#include <vector>
#include <iostream>
#include <algorithm>
#include <cstring>
#define foreach(x) for (typeof((x).begin()) it = (x).begin(); it != (x).end(); ++it)
using namespace std;
vector<pair<int, int> > ateam, bteam, *teams, *oldteams;
vector<int> result;
int cs[3000];
int find_best_guy()
{
memset(cs+1000, 0, 2000*sizeof(int));
foreach (*teams) {
cs[it->first]++;
cs[it->second]++;
}
int max_cs = *max_element(cs+1000, cs+3000);
if (cs[1009] == max_cs)
return 1009;
for (int i=1000; i<3000; i++) {
if (cs[i] == max_cs)
return i;
}
// should never get here
return -1;
}
void remove_his_teams(int guy)
{
oldteams->clear();
foreach (*teams) {
if (it->first != guy && it->second != guy) {
oldteams->push_back(*it);
}
}
swap(teams, oldteams);
}
int main()
{
teams = &ateam;
oldteams = &bteam;
// read teams
int n;
cin >> n;
for (int i=0; i<n; i++) {
int a, b;
cin >> a >> b;
teams->push_back(make_pair(a, b));
}
// main loop
while (!teams->empty()) {
int best_guy = find_best_guy();
result.push_back(best_guy);
remove_his_teams(best_guy);
}
// print result
cout << result.size() << endl;
foreach (result) {
cout << *it << endl;
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment