-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClass1.cs
More file actions
80 lines (65 loc) · 1.94 KB
/
Class1.cs
File metadata and controls
80 lines (65 loc) · 1.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
using System;
using System.Collections.Generic;
using System.Text;
namespace CodeForecs
{
class Program1
{
public static void Main1()
{
int size = Convert.ToInt32(Console.ReadLine());
var str = Console.ReadLine().Split(' ');
long[] arr = new long[size];
for(int i = 0; i < size; i++)
{
arr[i] = (long)(Convert.ToInt32(str[i]));
}
long[] temp = Merge(arr);
int size1 = 0;
for(int i = 0; i < size; i++)
{
if (temp[i] != -1)
size1++;
}
Console.WriteLine(size1);
for(int i = 0; i < temp.Length - 1; i++)
{
if (temp[i] != -1)
Console.Write(temp[i] + " ");
}
Console.Write(temp[temp.Length - 1]);
}
public static long[] Merge(long[] arr)
{
SortedDictionary<long, List<long>> dict = new SortedDictionary<long, List<long>>();
for(int i = 0; i < arr.Length; i++)
{
if(arr[i] != -1)
{
if(dict.ContainsKey(arr[i]))
{
dict[arr[i]].Add(i);
}
else
{
dict.Add(arr[i], new List<long>());
dict[arr[i]].Add(i);
}
}
}
bool needed = false;
foreach( KeyValuePair<long, List<long>> kvp in dict)
{
List<long> lst = kvp.Value;
if(lst.Count >= 2)
{
needed = true;
arr[lst[1]] += arr[lst[0]];
arr[lst[0]] = -1;
break;
}
}
return needed ? Merge(arr) : arr;
}
}
}