Google CodeJam浴室档位2017年资格赛圆形大数据集错误

时间:2017-04-18 03:02:28

标签: c# tree long-integer biginteger

我正在尝试解决Google CodeJam 2017 "Bathroom Stalls" problem C - 解决方案在链接中提供,我的C#代码在small1& 2套。大集合看起来没问题,但在线评判认为它不是不正确。有谁有想法为什么?我尝试使用UINavigationBar,我试图检测溢出,我通过bruteforce与小集进行比较以找到相同的解决方案。

ulong

输入摘录:

    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

1 个答案:

答案 0 :(得分:0)

正如@Jo在评论中所说,你对案例#11的回答是不正确的(它应该是:

Case #11: 500000000000000000 499999999999999999

我怀疑错误出现在您用于将整数拆分为子问题的公式中(再次转回ulong时可能会丢失精度)。

您可以通过避免浮点除以2.0并改变值来解决此问题。以下是我解决它的方法(在Python中):

def split(value):

    n = value >> 1

    return (n, n-1) if value % 2 == 0 else (n, n)