我正在尝试解决Google CodeJam 2017 "Bathroom Stalls" problem C - 解决方案在链接中提供,我的C#代码在small1和2集上运行良好。大集看起来没问题,但在线评判认为它不正确。有谁有想法为什么?我尝试使用ulong
s,我试图检测溢出,我通过bruteforce与小集进行比较以找到相同的解决方案。
private string SeatTreeSkip(ulong n, ulong k)
{
var q = new SortedSet<ulong>();
q.Add(n);
var c = new Dictionary<ulong,ulong>();
c[n] = 1;
for (ulong i = 0; i < n; )
{
ulong p = q.Last();
ulong x0 = (ulong)Math.Ceiling((p - 1) / 2.0);
ulong x1 = (ulong)Math.Floor((p - 1) / 2.0);
if (p - 1 < 0)
p = p;
i += c[p];
if (i < 0 || x0 < 0 || x1 < 0)
throw new Exception("overflow");
if (i >= k)
return x0 + " " + x1;
q.Remove(p);
q.Add(x0);
q.Add(x1);
if (!c.ContainsKey(x0)) c.Add(x0, 0);
if (!c.ContainsKey(x1)) c.Add(x1, 0);
c[x0] += c[p];
c[x1] += c[p];
}
throw new Exception("k is over");
}
输入代码段:
4 2
5 2
6 2
1000 1000
1000 1
500000000000000000 249999999999999999
1000000000000000000 500000000000000000
999999999999999999 423539247696576511
3 2
500000000000000000 144115188075855872
1000000000000000000 1
输出片段:
Case #1: 1 0
Case #2: 1 0
Case #3: 1 1
Case #4: 0 0
Case #5: 500 499
Case #6: 1 0
Case #7: 1 0
Case #8: 1 1
Case #9: 0 0
Case #10: 1 1
Case #11: 500000000000000000 500000000000000000