Initial data commit
This commit is contained in:
parent
72d218235f
commit
f23f22d71c
199087 changed files with 3378941 additions and 0 deletions
|
|
@ -0,0 +1,30 @@
|
|||
import std.stdio;
|
||||
|
||||
inout(T[]) maxSubseq(T)(inout T[] sequence) pure nothrow @nogc {
|
||||
int maxSum, thisSum, i, start, end = -1;
|
||||
|
||||
foreach (immutable j, immutable x; sequence) {
|
||||
thisSum += x;
|
||||
if (thisSum < 0) {
|
||||
i = j + 1;
|
||||
thisSum = 0;
|
||||
} else if (thisSum > maxSum) {
|
||||
maxSum = thisSum;
|
||||
start = i;
|
||||
end = j;
|
||||
}
|
||||
}
|
||||
|
||||
if (start <= end && start >= 0 && end >= 0)
|
||||
return sequence[start .. end + 1];
|
||||
else
|
||||
return [];
|
||||
}
|
||||
|
||||
void main() {
|
||||
const a1 = [-1, -2, 3, 5, 6, -2, -1, 4, -4, 2, -1];
|
||||
writeln("Maximal subsequence: ", a1.maxSubseq);
|
||||
|
||||
const a2 = [-1, -2, -3, -5, -6, -2, -1, -4, -4, -2, -1];
|
||||
writeln("Maximal subsequence: ", a2.maxSubseq);
|
||||
}
|
||||
|
|
@ -0,0 +1,34 @@
|
|||
import std.stdio, std.algorithm, std.range, std.typecons;
|
||||
|
||||
mixin template InitsTails(T) {
|
||||
T[] data;
|
||||
size_t pos;
|
||||
@property bool empty() pure nothrow @nogc {
|
||||
return pos > data.length;
|
||||
}
|
||||
void popFront() pure nothrow @nogc { pos++; }
|
||||
}
|
||||
|
||||
struct Inits(T) {
|
||||
mixin InitsTails!T;
|
||||
@property T[] front() pure nothrow @nogc { return data[0 .. pos]; }
|
||||
}
|
||||
|
||||
auto inits(T)(T[] seq) pure nothrow @nogc { return seq.Inits!T; }
|
||||
|
||||
struct Tails(T) {
|
||||
mixin InitsTails!T;
|
||||
@property T[] front() pure nothrow @nogc { return data[pos .. $]; }
|
||||
}
|
||||
|
||||
auto tails(T)(T[] seq) pure nothrow @nogc { return seq.Tails!T; }
|
||||
|
||||
T[] maxSubseq(T)(T[] seq) pure nothrow /*@nogc*/ {
|
||||
//return seq.tails.map!inits.joiner.reduce!(max!sum);
|
||||
return seq.tails.map!inits.join.minPos!q{ a.sum > b.sum }[0];
|
||||
}
|
||||
|
||||
void main() {
|
||||
[-1, -2, 3, 5, 6, -2, -1, 4, -4, 2, -1].maxSubseq.writeln;
|
||||
[-1, -2, -3, -5, -6, -2, -1, -4, -4, -2, -1].maxSubseq.writeln;
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue