結果

問題 No.297 カードの数式
ユーザー bluemeganebluemegane
提出日時 2021-05-08 09:21:07
言語 C#(csc)
(csc 3.9.0)
結果
AC  
実行時間 68 ms / 1,000 ms
コード長 4,589 bytes
コンパイル時間 1,638 ms
コンパイル使用メモリ 63,880 KB
実行使用メモリ 22,936 KB
最終ジャッジ日時 2023-10-14 12:00:48
合計ジャッジ時間 4,259 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 65 ms
22,844 KB
testcase_01 AC 66 ms
20,908 KB
testcase_02 AC 67 ms
20,816 KB
testcase_03 AC 65 ms
18,884 KB
testcase_04 AC 68 ms
18,880 KB
testcase_05 AC 66 ms
22,900 KB
testcase_06 AC 66 ms
20,940 KB
testcase_07 AC 68 ms
22,936 KB
testcase_08 AC 68 ms
20,916 KB
testcase_09 AC 66 ms
20,976 KB
testcase_10 AC 67 ms
20,896 KB
testcase_11 AC 67 ms
20,920 KB
testcase_12 AC 66 ms
22,932 KB
testcase_13 AC 67 ms
18,868 KB
testcase_14 AC 67 ms
20,884 KB
testcase_15 AC 67 ms
22,920 KB
testcase_16 AC 66 ms
20,880 KB
testcase_17 AC 67 ms
20,884 KB
testcase_18 AC 68 ms
22,828 KB
testcase_19 AC 67 ms
20,812 KB
testcase_20 AC 66 ms
20,804 KB
testcase_21 AC 65 ms
20,880 KB
testcase_22 AC 65 ms
22,832 KB
testcase_23 AC 65 ms
18,880 KB
testcase_24 AC 66 ms
18,924 KB
testcase_25 AC 66 ms
18,820 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

using System.Collections.Generic;
using System;

public class Deque<T>
{
    T[] buf;
    int offset, count, capacity;
    public int Count { get { return count; } }
    public Deque(int cap) { buf = new T[capacity = cap]; }
    public Deque() { buf = new T[capacity = 16]; }

    public void Init()
    {
        count = 0;
    }

    public T this[int index]
    {
        get { return buf[getIndex(index)]; }
        set { buf[getIndex(index)] = value; }
    }
    private int getIndex(int index)
    {
        if (index >= capacity)
            throw new IndexOutOfRangeException("out of range");
        var ret = index + offset;
        if (ret >= capacity)
            ret -= capacity;
        return ret;
    }
    public void PushFront(T item)
    {
        if (count == capacity) Extend();
        if (--offset < 0) offset += buf.Length;
        buf[offset] = item;
        ++count;
    }
    public T PopFront()
    {
        if (count == 0)
            throw new InvalidOperationException("collection is empty");
        --count;
        var ret = buf[offset++];
        if (offset >= capacity) offset -= capacity;
        return ret;
    }
    public void PushBack(T item)
    {
        if (count == capacity) Extend();
        var id = count++ + offset;
        if (id >= capacity) id -= capacity;
        buf[id] = item;
    }
    public T PopBack()
    {
        if (count == 0)
            throw new InvalidOperationException("collection is empty");
        return buf[getIndex(--count)];
    }
    public void Insert(int index, T item)
    {
        if (index > count) throw new IndexOutOfRangeException();
        this.PushFront(item);
        for (int i = 0; i < index; i++)
            this[i] = this[i + 1];
        this[index] = item;
    }
    public T RemoveAt(int index)
    {
        if (index < 0 || index >= count) throw new IndexOutOfRangeException();
        var ret = this[index];
        for (int i = index; i > 0; i--)
            this[i] = this[i - 1];
        this.PopFront();
        return ret;
    }
    private void Extend()
    {
        T[] newBuffer = new T[capacity << 1];
        if (offset > capacity - count)
        {
            var len = buf.Length - offset;
            Array.Copy(buf, offset, newBuffer, 0, len);
            Array.Copy(buf, 0, newBuffer, len, count - len);
        }
        else Array.Copy(buf, offset, newBuffer, 0, count);
        buf = newBuffer;
        offset = 0;
        capacity <<= 1;
    }
}

public class Hello
{
    public static Deque<int> d;
    static void Main()
    {
        var n = int.Parse(Console.ReadLine().Trim());
        string[] line = Console.ReadLine().Trim().Split(' ');
        var a = new List<int>();
        var p = 0;
        var m = 0;
        for (int i = 0; i < n; i++)
        {
            if (line[i] == "+") p++;
            else if (line[i] == "-") m++;
            else a.Add(int.Parse(line[i]));
        }
        d = new Deque<int>(n);
        a.Sort();
        foreach (var x in a) d.PushBack(x);
        getAns(p, m, n - p - m, a);
    }
    static long getMin2(int n)
    {
        var dc = d.Count;
        var k = 1;
        var res = 0L;
        var pc = 0;
        while (pc < dc)
        {
            for (int i = 0; i < n; i++)
            {
                var w = d.PopBack();
                res += w * k;
                pc++;
                if (pc == dc) break;
            }
            k *= 10;
        }
        return res;
    }
    static long getMax() => d.PopBack();
    static long getMax(int t)
    {
        var a = "";
        for (int i = 0; i < t; i++)
            a += d.PopBack().ToString();
        return long.Parse(a);
    }
    static long getMin() => d.PopFront();
    static void getAns(int p, int m, int c, List<int> a)
    {
        var fc = p + m;
        var tmax = getMax(c - fc);
        for (int i = 0; i < p; i++) tmax += getMax();
        for (int i = 0; i < m; i++) tmax -= getMin();

        foreach (var x in a) d.PushBack(x);

        long tmin;
        if (m == 0) tmin = getMin2(fc + 1);
        else
        {
            if (p == 0)
            {
                tmin = getMin();
                tmin -= getMax(c - fc);
                m--;
                for (int i = 0; i < m; i++) tmin -= getMax();
            }

            else
            {
                tmin = -getMax(c - fc);
                m--;
                for (int i = 0; i < p+1; i++) tmin += getMin();
                for (int i = 0; i < m; i++) tmin -= getMax();
            }

        }
        Console.WriteLine("{0} {1}", tmax, tmin);
    }
}
0