Skip to content

Instantly share code, notes, and snippets.

@mikedll
Created March 29, 2024 14:19
Show Gist options
  • Save mikedll/80590ba13a028fb58916abf1438f998a to your computer and use it in GitHub Desktop.
Save mikedll/80590ba13a028fb58916abf1438f998a to your computer and use it in GitHub Desktop.
Top Coder SRM 450
using System;
class EnemyTowers
{
const int INF = int.MaxValue;
public int sim( int s, int h, int a, int t)
{
int ret = 0;
int p = h;
while( s > 0 && t > 0 ) {
ret++;
int d = s;
p -= d;
if( p <= 0 ) {
t -= 1 + (-p / h);
p = h - (-p % h);
}
s -= t * a;
}
return (s <= 0) ? INF : ret;
}
public int attack(int s, int h, int a, int wo, int st)
{
const int INF = int.MaxValue;
int lb = 0, ub = s;
int ret = INF;
while (ub >= lb)
{
int mid = lb + (ub - lb) / 2;
//Console.WriteLine("lb: {0}, ub: {1}, mid: {2}", lb, ub, mid);
int swo = mid;
int sst = (s - mid);
int wsr = sim(swo, h, a, wo);
int ssr = sim(sst, h, a, st);
ret = Math.Min(ret, Math.Max(wsr, ssr));
if (wsr > ssr) lb = mid + 1;
else ub = mid - 1;
}
return(ret == INF) ? -1 : ret;
}
}
var e = new EnemyTowers();
Console.WriteLine(e.attack(7, 2, 1, 2, 3));
Console.WriteLine(e.attack(120, 10, 40000, 6, 6));
Console.WriteLine(e.attack(119, 10, 40000, 6, 6));
Console.WriteLine(e.attack(200, 50, 3, 10, 5));
Console.WriteLine(e.attack(1000000000, 40000, 40000, 20223, 20226));
Console.WriteLine(e.attack(1000000000, 1, 1, 40000, 40000));
class StrangeComputer
{
public int setMemory(String mem)
{
var c = 0;
char cur = '0';
for( int i = 0; i < mem.Length; i++ )
if( mem[i] != cur ) {
if (cur == '0') cur = '1';
else cur = '0';
c++;
}
return c;
}
}
StrangeComputer s = new StrangeComputer();
Console.WriteLine(s.setMemory("0011"));
Console.WriteLine(s.setMemory("000"));
Console.WriteLine(s.setMemory("0100"));
Console.WriteLine(s.setMemory("111000111"));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment