結果
| 問題 |
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 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 23 |
コンパイルメッセージ
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);
}
}
bluemegane