-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathANARC05B.cs
91 lines (79 loc) · 3.42 KB
/
ANARC05B.cs
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
81
82
83
84
85
86
87
88
89
90
91
using System;
using System.Collections.Generic;
// https://www.spoj.com/problems/ANARC05B/ #sequence #sorting
// Maximizes the sum while traversing a pair of intersecting, ordered sequences.
public static class ANARC05B
{
// Sequences are effectively one-indexed as the first element is the sequence size.
// And they're strictly increasing. And there's no cost for jumping between sequences,
// so we can just do this greedily. The sequence to use from each intersection point
// is the one whose sum until the next intersection point is greatest.
public static int Solve(int[] firstSequence, int[] secondSequence)
{
int firstSequenceLength = firstSequence[0];
int secondSequenceLength = secondSequence[0];
var intersectionPoints = new Queue<Tuple<int, int>>();
for (int firstSequenceIndex = 1; firstSequenceIndex <= firstSequenceLength; ++firstSequenceIndex)
{
// Could save work by only searching from the previous intersection point (w/o queue).
int secondSequenceIndex = Array.BinarySearch(
secondSequence, 1, secondSequenceLength, firstSequence[firstSequenceIndex]);
if (secondSequenceIndex > 0)
{
intersectionPoints.Enqueue(Tuple.Create(firstSequenceIndex, secondSequenceIndex));
}
}
int maximumSum = 0;
int indexSummedThroughForFirstSequence = 0;
int indexSummedThroughForSecondSequence = 0;
while (intersectionPoints.Count > 0)
{
var intersectionPoint = intersectionPoints.Dequeue();
maximumSum += Math.Max(
GetSumBetween(firstSequence,
startIndex: indexSummedThroughForFirstSequence + 1,
endIndex: intersectionPoint.Item1),
GetSumBetween(secondSequence,
startIndex: indexSummedThroughForSecondSequence + 1,
endIndex: intersectionPoint.Item2));
indexSummedThroughForFirstSequence = intersectionPoint.Item1;
indexSummedThroughForSecondSequence = intersectionPoint.Item2;
}
// One more choice: should we fall off the end of the first or the second (if we haven't already)?
maximumSum += Math.Max(
GetSumBetween(firstSequence,
startIndex: indexSummedThroughForFirstSequence + 1,
endIndex: firstSequenceLength),
GetSumBetween(secondSequence,
startIndex: indexSummedThroughForSecondSequence + 1,
endIndex: secondSequenceLength));
return maximumSum;
}
private static int GetSumBetween(int[] sequence, int startIndex, int endIndex)
{
int sum = 0;
for (int i = startIndex; i <= endIndex; ++i)
{
sum += sequence[i];
}
return sum;
}
}
public static class Program
{
private static void Main()
{
int[] firstSequence;
int[] secondSequence;
while ((firstSequence = Array.ConvertAll(
Console.ReadLine().Split(default(char[]), StringSplitOptions.RemoveEmptyEntries),
int.Parse)).Length != 1)
{
secondSequence = Array.ConvertAll(
Console.ReadLine().Split(default(char[]), StringSplitOptions.RemoveEmptyEntries),
int.Parse);
Console.WriteLine(
ANARC05B.Solve(firstSequence, secondSequence));
}
}
}