提出 #39854709
ソースコード 拡げる
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Numerics;
namespace Tasks
{
public class D
{
public static void Main()
{
using var sw = new StreamWriter(Console.OpenStandardOutput()) { AutoFlush = false };
Console.SetOut(sw);
Solve();
Console.Out.Flush();
}
public static void Solve()
{
var (N, Q) = Scanner.Scan<int, int>();
var set0 = new Set<int>(Enumerable.Range(0, N));
var set1 = new Set<int>();
while (Q-- > 0)
{
var e = Scanner.ScanEnumerable<int>().ToArray();
if (e[0] == 1)
{
var x = set0.ElementAt(0);
set0.Remove(x);
set1.Add(x);
}
else if (e[0] == 2)
{
var x = e[1] - 1;
set1.Remove(x);
}
else
{
var x = set1.ElementAt(0);
Console.WriteLine(x + 1);
}
}
}
public class Set<T> : IReadOnlyCollection<T>
{
private readonly RandomizedBinarySearchTree<T> _tree;
private readonly bool _allowDuplication;
public Set(bool allowDuplication = false) : this(Comparer<T>.Default, allowDuplication) { }
public Set(IEnumerable<T> source, bool allowDuplication = false) : this(allowDuplication)
{
foreach (var value in source) Add(value);
}
public Set(IEnumerable<T> source, IComparer<T> comparer, bool allowDuplication = false)
: this(comparer, allowDuplication)
{
foreach (var value in source) Add(value);
}
public Set(IEnumerable<T> source, Comparison<T> comparison, bool allowDuplication = false)
: this(comparison, allowDuplication)
{
foreach (var value in source) Add(value);
}
public Set(IComparer<T> comparer, bool allowDuplication = false)
: this((comparer ?? Comparer<T>.Default).Compare, allowDuplication)
{
}
public Set(Comparison<T> comparison, bool allowDuplication = false)
{
_tree = new RandomizedBinarySearchTree<T>(comparison);
_allowDuplication = allowDuplication;
}
public void Add(T value)
{
if (_allowDuplication || !_tree.Contains(value)) _tree.Insert(value);
}
public void Remove(T value)
{
_tree.Remove(value);
}
public bool Contains(T value)
{
return _tree.Contains(value);
}
public T ElementAt(int index)
{
return _tree.ElementAt(index);
}
public int LowerBound(T value)
{
return _tree.LowerBound(value);
}
public int UpperBound(T value)
{
return _tree.UpperBound(value);
}
public int Count => _tree.Count;
public IEnumerator<T> GetEnumerator() => _tree.GetEnumerator();
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
}
public class RandomizedBinarySearchTree<T> : IReadOnlyCollection<T>
{
private readonly Comparison<T> _comparison;
private readonly Random _random;
private Node _root;
public RandomizedBinarySearchTree(int seed = 0) : this(comparer: null, seed) { }
public RandomizedBinarySearchTree(Comparer<T> comparer, int seed = 0) : this(
(comparer ?? Comparer<T>.Default).Compare, seed)
{
}
public RandomizedBinarySearchTree(Comparison<T> comparison, int seed = 0)
{
_comparison = comparison;
_random = new Random(seed);
}
public void Insert(T value)
{
if (_root is null) _root = new Node(value);
else InsertAt(LowerBound(value), value);
}
public bool Remove(T value)
{
var index = LowerBound(value);
if (index < 0) return false;
RemoveAt(index);
return true;
}
public T ElementAt(int index)
{
if (index < 0 || Count <= index) throw new ArgumentOutOfRangeException(nameof(index));
var node = _root;
var idx = CountOf(node) - CountOf(node.R) - 1;
while (node is { })
{
if (idx == index) return node.Value;
if (idx > index)
{
node = node.L;
idx -= CountOf(node?.R) + 1;
}
else
{
node = node.R;
idx += CountOf(node?.L) + 1;
}
}
throw new ArgumentOutOfRangeException(nameof(index));
}
public bool Contains(T value)
{
return Find(value) is { };
}
public int Count => CountOf(_root);
public IEnumerator<T> GetEnumerator() => Enumerate(_root).GetEnumerator();
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
public int UpperBound(T value) => Bound(value, (x, y) => _comparison(x, y) > 0);
public int LowerBound(T value) => Bound(value, (x, y) => _comparison(x, y) >= 0);
public int Bound(T value, Func<T, T, bool> compare)
{
var node = _root;
if (node is null) return -1;
var bound = CountOf(node);
var idx = bound - CountOf(node.R) - 1;
while (node is { })
{
if (compare(node.Value, value))
{
node = node.L;
bound = Math.Min(bound, idx);
idx -= CountOf(node?.R) + 1;
}
else
{
node = node.R;
idx += CountOf(node?.L) + 1;
}
}
return bound;
}
private double GetProbability() => _random.NextDouble();
private void InsertAt(int index, T value)
{
var (l, r) = Split(_root, index);
_root = Merge(Merge(l, new Node(value)), r);
}
private void RemoveAt(int index)
{
var (l, r1) = Split(_root, index);
var (_, r2) = Split(r1, 1);
_root = Merge(l, r2);
}
private Node Merge(Node l, Node r)
{
if (l is null || r is null) return l ?? r;
var (n, m) = (CountOf(l), CountOf(r));
if ((double)n / (n + m) > GetProbability())
{
l.R = Merge(l.R, r);
return l;
}
else
{
r.L = Merge(l, r.L);
return r;
}
}
private (Node, Node) Split(Node node, int k)
{
if (node is null) return (null, null);
if (k <= CountOf(node.L))
{
var (l, r) = Split(node.L, k);
node.L = r;
return (l, node);
}
else
{
var (l, r) = Split(node.R, k - CountOf(node.L) - 1);
node.R = l;
return (node, r);
}
}
private Node Find(T value)
{
var node = _root;
while (node is { })
{
var cmp = _comparison(node.Value, value);
if (cmp > 0) node = node.L;
else if (cmp < 0) node = node.R;
else break;
}
return node;
}
private static int CountOf(Node node) => node?.Count ?? 0;
private static IEnumerable<T> Enumerate(Node node = null)
{
if (node is null) yield break;
foreach (var value in Enumerate(node.L)) yield return value;
yield return node.Value;
foreach (var value in Enumerate(node.R)) yield return value;
}
private class Node
{
internal T Value { get; }
internal Node L
{
get => _l;
set
{
_l = value;
UpdateCount();
}
}
internal Node R
{
get => _r;
set
{
_r = value;
UpdateCount();
}
}
internal int Count { get; private set; }
private Node _l;
private Node _r;
internal Node(T value)
{
Value = value;
Count = 1;
}
private void UpdateCount()
{
Count = (L?.Count ?? 0) + (R?.Count ?? 0) + 1;
}
}
}
public class PriorityQueue<T> : IReadOnlyCollection<T>
{
private readonly Comparison<T> _comparison;
private readonly List<T> _heap;
public PriorityQueue(IEnumerable<T> items, IComparer<T> comparer = null) : this(comparer)
{
foreach (var item in items) Enqueue(item);
}
public PriorityQueue(IEnumerable<T> items, Comparison<T> comparison) : this(comparison)
{
foreach (var item in items) Enqueue(item);
}
public PriorityQueue(IComparer<T> comparer = null) : this((comparer ?? Comparer<T>.Default).Compare) { }
public PriorityQueue(Comparison<T> comparison)
{
_heap = new List<T>();
_comparison = comparison;
}
public int Count => _heap.Count;
public IEnumerator<T> GetEnumerator() => _heap.GetEnumerator();
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
public void Enqueue(T item)
{
var child = Count;
_heap.Add(item);
while (child > 0)
{
var parent = (child - 1) / 2;
if (_comparison(_heap[parent], _heap[child]) <= 0) break;
(_heap[parent], _heap[child]) = (_heap[child], _heap[parent]);
child = parent;
}
}
public T Dequeue()
{
if (Count == 0) throw new InvalidOperationException();
var result = _heap[0];
_heap[0] = _heap[Count - 1];
_heap.RemoveAt(Count - 1);
var parent = 0;
while (parent * 2 + 1 < Count)
{
var left = parent * 2 + 1;
var right = parent * 2 + 2;
if (right < Count && _comparison(_heap[left], _heap[right]) > 0)
left = right;
if (_comparison(_heap[parent], _heap[left]) <= 0) break;
(_heap[parent], _heap[left]) = (_heap[left], _heap[parent]);
parent = left;
}
return result;
}
public T Peek()
{
if (Count == 0) throw new InvalidOperationException();
return _heap[0];
}
public bool TryDequeue(out T result)
{
if (Count > 0)
{
result = Dequeue();
return true;
}
result = default;
return false;
}
public bool TryPeek(out T result)
{
if (Count > 0)
{
result = Peek();
return true;
}
result = default;
return false;
}
public void Clear() => _heap.Clear();
public bool Contains(T item) => _heap.Contains(item);
}
public static class Scanner
{
public static T Scan<T>() where T : IConvertible => Convert<T>(Scan()[0]);
public static (T1, T2) Scan<T1, T2>() where T1 : IConvertible where T2 : IConvertible
{
var buffer = Scan();
return (Convert<T1>(buffer[0]), Convert<T2>(buffer[1]));
}
public static (T1, T2, T3) Scan<T1, T2, T3>() where T1 : IConvertible where T2 : IConvertible where T3 : IConvertible
{
var buffer = Scan();
return (Convert<T1>(buffer[0]), Convert<T2>(buffer[1]), Convert<T3>(buffer[2]));
}
public static (T1, T2, T3, T4) Scan<T1, T2, T3, T4>() where T1 : IConvertible where T2 : IConvertible where T3 : IConvertible where T4 : IConvertible
{
var buffer = Scan();
return (Convert<T1>(buffer[0]), Convert<T2>(buffer[1]), Convert<T3>(buffer[2]), Convert<T4>(buffer[3]));
}
public static (T1, T2, T3, T4, T5) Scan<T1, T2, T3, T4, T5>() where T1 : IConvertible where T2 : IConvertible where T3 : IConvertible where T4 : IConvertible where T5 : IConvertible
{
var buffer = Scan();
return (Convert<T1>(buffer[0]), Convert<T2>(buffer[1]), Convert<T3>(buffer[2]), Convert<T4>(buffer[3]), Convert<T5>(buffer[4]));
}
public static (T1, T2, T3, T4, T5, T6) Scan<T1, T2, T3, T4, T5, T6>() where T1 : IConvertible where T2 : IConvertible where T3 : IConvertible where T4 : IConvertible where T5 : IConvertible where T6 : IConvertible
{
var buffer = Scan();
return (Convert<T1>(buffer[0]), Convert<T2>(buffer[1]), Convert<T3>(buffer[2]), Convert<T4>(buffer[3]), Convert<T5>(buffer[4]), Convert<T6>(buffer[5]));
}
public static IEnumerable<T> ScanEnumerable<T>() where T : IConvertible => Scan().Select(Convert<T>);
private static string[] Scan()
{
var line = Console.ReadLine()?.Trim() ?? string.Empty;
return string.IsNullOrEmpty(line) ? Array.Empty<string>() : line.Split(' ');
}
private static T Convert<T>(string value) where T : IConvertible => (T)System.Convert.ChangeType(value, typeof(T));
}
}
}
提出情報
| 提出日時 |
|
| 問題 |
D - Bank |
| ユーザ |
AconCavy |
| 言語 |
C# (.NET Core 3.1.201) |
| 得点 |
400 |
| コード長 |
15924 Byte |
| 結果 |
AC |
| 実行時間 |
1716 ms |
| メモリ |
81380 KiB |
ジャッジ結果
| セット名 |
Sample |
All |
| 得点 / 配点 |
0 / 0 |
400 / 400 |
| 結果 |
|
|
| セット名 |
テストケース |
| Sample |
00_sample_00.txt |
| All |
00_sample_00.txt, 01_random_00.txt, 01_random_01.txt, 01_random_02.txt, 01_random_03.txt, 01_random_04.txt, 01_random_05.txt, 01_random_06.txt, 01_random_07.txt, 01_random_08.txt, 01_random_09.txt, 01_random_10.txt, 02_corner_00.txt, 02_corner_01.txt, 02_corner_02.txt, 02_corner_03.txt, 02_corner_04.txt, 02_corner_05.txt, 02_corner_06.txt, 02_corner_07.txt, 02_corner_08.txt, 02_corner_09.txt, 02_corner_10.txt, 03_min_00.txt |
| ケース名 |
結果 |
実行時間 |
メモリ |
| 00_sample_00.txt |
AC |
87 ms |
28168 KiB |
| 01_random_00.txt |
AC |
866 ms |
59992 KiB |
| 01_random_01.txt |
AC |
1034 ms |
78992 KiB |
| 01_random_02.txt |
AC |
1027 ms |
76872 KiB |
| 01_random_03.txt |
AC |
1039 ms |
78952 KiB |
| 01_random_04.txt |
AC |
799 ms |
58472 KiB |
| 01_random_05.txt |
AC |
792 ms |
78984 KiB |
| 01_random_06.txt |
AC |
1408 ms |
79872 KiB |
| 01_random_07.txt |
AC |
957 ms |
79220 KiB |
| 01_random_08.txt |
AC |
497 ms |
55032 KiB |
| 01_random_09.txt |
AC |
1620 ms |
80872 KiB |
| 01_random_10.txt |
AC |
211 ms |
48568 KiB |
| 02_corner_00.txt |
AC |
1393 ms |
81280 KiB |
| 02_corner_01.txt |
AC |
1397 ms |
81128 KiB |
| 02_corner_02.txt |
AC |
1395 ms |
81304 KiB |
| 02_corner_03.txt |
AC |
1358 ms |
81156 KiB |
| 02_corner_04.txt |
AC |
1339 ms |
80988 KiB |
| 02_corner_05.txt |
AC |
1716 ms |
81136 KiB |
| 02_corner_06.txt |
AC |
1716 ms |
81036 KiB |
| 02_corner_07.txt |
AC |
1604 ms |
81132 KiB |
| 02_corner_08.txt |
AC |
1599 ms |
81380 KiB |
| 02_corner_09.txt |
AC |
1106 ms |
81152 KiB |
| 02_corner_10.txt |
AC |
1211 ms |
81156 KiB |
| 03_min_00.txt |
AC |
80 ms |
28372 KiB |