Skip to content

Instantly share code, notes, and snippets.

@terry182
Created April 8, 2018 08:54
Show Gist options
  • Select an option

  • Save terry182/d4d9b360062f566225e3f8e4219b7154 to your computer and use it in GitHub Desktop.

Select an option

Save terry182/d4d9b360062f566225e3f8e4219b7154 to your computer and use it in GitHub Desktop.
Google Code Jam 2018 Qualification Round codes
#include <iostream>
using namespace std;
int main()
{
int T;
cin >> T;
for (int kase = 1; kase <= T; ++kase)
{ int d;
int limit = 0, cur = 0, cha = 1, ans = 0;
string s;
cin >> d >> s;
for (int i = 0; i < s.length(); ++i)
{ if (s[i] == 'S')
{ cur += cha;
limit += 1;
}
else
cha <<= 1;
}
if (d < limit) { cout << "Case #" << kase << ": IMPOSSIBLE" << endl; continue;}
if (s[s.length() -1] == 'C') cha >>= 1;
for (int i = s.length() - 2; i >= 0; i--)
{
if (cur <= d)
{
cout << "Case #" << kase << ": " << ans << endl;
break;
}
if (s[i] == 'C' && s[i+1] == 'S')
{
cur -= (cha >> 1);
s[i] = 'S';
s[i+1] = 'C';
i += 2;
ans++;
}
else if (s[i] == 'C')
cha >>= 1;
}
}
}
#include <iostream>
#include <algorithm>
using namespace std;
int main()
{
std::ios::sync_with_stdio(false);
int T;
cin >> T;
for (int kase = 1; kase <= T; kase++)
{
int n, ok = -1;
cin >> n;
int a[n], b[n];
for (int i = 0; i < n; ++i) cin >> a[i];
for (int i = 0; i < n; i += 2) b[i/2] = a[i];
sort(b, b+(n/2)+(n%2));
for (int i = 0; i < n; i += 2) a[i] = b[i/2];
for (int i = 1; i < n; i += 2) b[i/2] = a[i];
sort(b, b+(n/2));
for (int i = 1; i < n; i += 2) a[i] = b[i/2];
for (int i = 0; i < n-1; ++i)
if (a[i] > a[i+1])
{ ok = i;
break;
}
cout << "Case #" << kase << ": ";
if (ok == -1) cout << "OK" << endl;
else cout << ok << endl;
}
}
#include <iostream>
#include <cstring>
#include <cmath>
using namespace std;
int main()
{
int T;
cin >> T;
for (int kase = 1; kase <= T; ++kase)
{
int a;
cin >> a;
int x = (int)(sqrt(a));
int y = ceil( (float)a / x );
bool filled[x][y];
memset(filled, 0, sizeof(filled));
for (int tt = 0; tt < 1000; ++tt)
{
int bestx = 1, besty = 1;
int empty = 0;
for (int i = 1; i < x-1; i++)
for (int j = 1; j < y-1; j++)
{
int current = 0;
for (int k = -1; k <= 1; k++)
for (int l = -1; l <= 1; l++)
current += !filled[i+k][j+l];
if (current > empty)
{
bestx = i;
besty = j;
empty = current;
}
}
cout << bestx+1 << " " << besty+1 << endl;
int fillx, filly;
cin >> fillx >> filly;
if (fillx == filly && filly == 0)
break;
if (fillx == filly && filly == -1)
break;
filled[fillx-1][filly-1] = true;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment