結果
問題 | No.297 カードの数式 |
ユーザー | bluemegane |
提出日時 | 2021-05-08 09:21:07 |
言語 | C#(csc) (csc 3.9.0) |
結果 |
AC
|
実行時間 | 31 ms / 1,000 ms |
コード長 | 4,589 bytes |
コンパイル時間 | 955 ms |
コンパイル使用メモリ | 113,784 KB |
実行使用メモリ | 18,304 KB |
最終ジャッジ日時 | 2024-09-16 06:50:30 |
合計ジャッジ時間 | 2,831 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge4 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 28 ms
18,176 KB |
testcase_01 | AC | 28 ms
18,304 KB |
testcase_02 | AC | 29 ms
18,048 KB |
testcase_03 | AC | 27 ms
18,176 KB |
testcase_04 | AC | 28 ms
18,048 KB |
testcase_05 | AC | 28 ms
18,176 KB |
testcase_06 | AC | 28 ms
18,048 KB |
testcase_07 | AC | 28 ms
18,176 KB |
testcase_08 | AC | 27 ms
18,304 KB |
testcase_09 | AC | 27 ms
17,920 KB |
testcase_10 | AC | 28 ms
18,304 KB |
testcase_11 | AC | 29 ms
17,920 KB |
testcase_12 | AC | 31 ms
17,792 KB |
testcase_13 | AC | 28 ms
18,048 KB |
testcase_14 | AC | 27 ms
18,176 KB |
testcase_15 | AC | 28 ms
18,304 KB |
testcase_16 | AC | 28 ms
18,304 KB |
testcase_17 | AC | 29 ms
18,048 KB |
testcase_18 | AC | 28 ms
18,048 KB |
testcase_19 | AC | 28 ms
18,176 KB |
testcase_20 | AC | 28 ms
18,304 KB |
testcase_21 | AC | 28 ms
18,048 KB |
testcase_22 | AC | 28 ms
18,176 KB |
testcase_23 | AC | 28 ms
18,176 KB |
testcase_24 | AC | 28 ms
18,048 KB |
testcase_25 | AC | 28 ms
18,048 KB |
コンパイルメッセージ
Microsoft (R) Visual C# Compiler version 3.9.0-6.21124.20 (db94f4cc) Copyright (C) Microsoft Corporation. All rights reserved.
ソースコード
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); } }