Skip to content

Instantly share code, notes, and snippets.

@yinyanghu
Last active December 12, 2015 10:49
Show Gist options
  • Save yinyanghu/4761484 to your computer and use it in GitHub Desktop.
Save yinyanghu/4761484 to your computer and use it in GitHub Desktop.
Topcoder SRM568 Div2
#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>
#define infinity 1000000000
using namespace std;
class BallsSeparating {
public:
int minOperations(vector <int>, vector <int>, vector <int>);
};
int BallsSeparating::minOperations(vector <int> red, vector <int> green, vector <int> blue) {
int n = red.size();
if (n < 3) return -1;
int ans = infinity;
vector<int> sum;
sum.clear();
int total = 0;
for (int i = 0; i < n; ++ i)
{
sum.push_back(red[i] + green[i] + blue[i] - max(max(red[i], green[i]), blue[i]));
total += sum[i];
}
for (int i = 0; i < n; ++ i)
for (int j = 0; j < n; ++ j)
{
if (i == j) continue;
for (int k = 0; k < n; ++ k)
{
if (k == i || k == j) continue;
int count = total;
count -= sum[i] + sum[j] + sum[k];
count += green[i] + blue[i];
count += red[j] + blue[j];
count += red[k] + green[k];
if (count < ans)
ans = count;
}
}
return ans;
}
#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>
using namespace std;
class ShuffleSort {
public:
double shuffle(vector <int>);
};
double ShuffleSort::shuffle(vector <int> cards) {
sort(cards.begin(), cards.end());
int n = cards.size();
double ret = 0;
for (int i = 0; i < n; ++ i)
{
int k = 0;
for (int j = i; j < n; ++ j)
if (cards[j] == cards[i])
++ k;
else
break;
ret += (double)(n - i) / k;
}
ret -= (n - 1);
return ret;
}
#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>
using namespace std;
class TheSimilarNumbers {
public:
int find(int, int);
};
int TheSimilarNumbers::find(int lower, int upper) {
int total = 0;
int x = lower;
while (x <= upper)
{
++ total;
x = x * 10 + 1;
}
return total;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment