Syntax
C
o
l
o
r
i
z
e
r
Format
using System; using System.Collections.Generic; using System.Text; public class GetToTheTop { private Stair[] stairs; private bool[,] compAdj; private class Stair : IComparable<Stair> { public int X, Y, Len, Sweets; public Stair(int x, int y, int len, int sweets) { X = x; Y = y; Len = len; Sweets = sweets; } public int CompareTo(Stair other) { if (Y != other.Y) return Y.CompareTo(other.Y); return X.CompareTo(other.X); } public int EndX { get { return X + Len; } } public bool CanReach(Stair other, int maxDist) { if (other.Y < Y) return false; if (Intersect(X, EndX, other.X, other.EndX)) return other.Y - Y <= maxDist; else { int ourX, otherX; if (other.X > EndX) { ourX = EndX; otherX = other.X; } else { ourX = X; otherX = other.EndX; } return Sqr(ourX - otherX) + Sqr(Y - other.Y) <= Sqr(maxDist); } } private bool Intersect(int a1, int b1, int a2, int b2) { return !(a2 > b1 || a1 > b2); } private long Sqr(int x) { return x * x; } } private class Component { private List<Stair> stairs = new List<Stair>(); public int Sweets; public int Y; public bool Empty { get { return stairs.Count == 0; } } public void Add(Stair st) { stairs.Add(st); Sweets += st.Sweets; Y = st.Y; } public bool CanReach(Component other, int maxDist) { for (int i = 0; i < stairs.Count; i++) { for (int j = 0; j < other.stairs.Count; j++) { if (stairs[i].CanReach(other.stairs[j], maxDist)) return true; } } return false; } public bool Belongs(Stair stair, int maxDist) { Stair last = stairs[stairs.Count - 1]; return stairs.Count == 0 || (stair.Y == last.Y && last.EndX + maxDist >= stair.X); } } public int collectSweets(int K, int[] sweets, int[] x, int[] y, int[] stairLength) { stairs = new Stair[sweets.Length]; for (int i = 0; i < sweets.Length; i++) stairs[i] = new Stair(x[i], y[i], stairLength[i], sweets[i]); Array.Sort(stairs); Component comp = new Component(); for (int i = 0; i < stairs.Length; i++) { if (i > 0 && !comp.Belongs(stairs[i], K)) { components.Add(comp); comp = new Component(); } comp.Add(stairs[i]); } if (!comp.Empty) components.Add(comp); compAdj = new bool[components.Count,components.Count]; best = new int[components.Count]; for (int i = 0; i < best.Length; i++) best[i] = -1; for (int i = 0; i < components.Count; i++) { for (int j = i + 1; j < components.Count; j++) { if (components[i].CanReach(components[j], K)) compAdj[i, j] = true; } } int res = 0; for (int i = 0; i < components.Count; i++) { if (components[i].Y <= K) res = Math.Max(res, Rec(i)); } return res; } private int[] best; private List<Component> components = new List<Component>(); private int Rec(int startComp) { if (best[startComp] != -1) return best[startComp]; int res = 0; for (int i = 0; i < components.Count; i++) { if (compAdj[startComp, i]) res = Math.Max(res, Rec(i)); } return best[startComp] = res + components[startComp].Sweets; } }
Autodetect
C
C++
C#
Java
JavaScript
Scala
SQL
Python
XML
Generic
Format
Edit
permlink
using
System
;
using
System.Collections.Generic
;
using
System.Text
;
public
class
GetToTheTop
{
private
Stair
[
]
stairs
;
private
bool
[
,
]
compAdj
;
private
class
Stair
:
IComparable
<
Stair
>
{
public
int
X
,
Y
,
Len
,
Sweets
;
public
Stair
(
int
x
,
int
y
,
int
len
,
int
sweets
)
{
X
=
x
;
Y
=
y
;
Len
=
len
;
Sweets
=
sweets
;
}
public
int
CompareTo
(
Stair
other
)
{
if
(
Y
!
=
other
.
Y
)
return
Y
.
CompareTo
(
other
.
Y
)
;
return
X
.
CompareTo
(
other
.
X
)
;
}
public
int
EndX
{
get
{
return
X
+
Len
;
}
}
public
bool
CanReach
(
Stair
other
,
int
maxDist
)
{
if
(
other
.
Y
<
Y
)
return
false
;
if
(
Intersect
(
X
,
EndX
,
other
.
X
,
other
.
EndX
)
)
return
other
.
Y
-
Y
<
=
maxDist
;
else
{
int
ourX
,
otherX
;
if
(
other
.
X
>
EndX
)
{
ourX
=
EndX
;
otherX
=
other
.
X
;
}
else
{
ourX
=
X
;
otherX
=
other
.
EndX
;
}
return
Sqr
(
ourX
-
otherX
)
+
Sqr
(
Y
-
other
.
Y
)
<
=
Sqr
(
maxDist
)
;
}
}
private
bool
Intersect
(
int
a1
,
int
b1
,
int
a2
,
int
b2
)
{
return
!
(
a2
>
b1
|
|
a1
>
b2
)
;
}
private
long
Sqr
(
int
x
)
{
return
x
*
x
;
}
}
private
class
Component
{
private
List
<
Stair
>
stairs
=
new
List
<
Stair
>
(
)
;
public
int
Sweets
;
public
int
Y
;
public
bool
Empty
{
get
{
return
stairs
.
Count
=
=
0
;
}
}
public
void
Add
(
Stair
st
)
{
stairs
.
Add
(
st
)
;
Sweets
+
=
st
.
Sweets
;
Y
=
st
.
Y
;
}
public
bool
CanReach
(
Component
other
,
int
maxDist
)
{
for
(
int
i
=
0
;
i
<
stairs
.
Count
;
i
+
+
)
{
for
(
int
j
=
0
;
j
<
other
.
stairs
.
Count
;
j
+
+
)
{
if
(
stairs
[
i
]
.
CanReach
(
other
.
stairs
[
j
]
,
maxDist
)
)
return
true
;
}
}
return
false
;
}
public
bool
Belongs
(
Stair
stair
,
int
maxDist
)
{
Stair
last
=
stairs
[
stairs
.
Count
-
1
]
;
return
stairs
.
Count
=
=
0
|
|
(
stair
.
Y
=
=
last
.
Y
&
&
last
.
EndX
+
maxDist
>
=
stair
.
X
)
;
}
}
public
int
collectSweets
(
int
K
,
int
[
]
sweets
,
int
[
]
x
,
int
[
]
y
,
int
[
]
stairLength
)
{
stairs
=
new
Stair
[
sweets
.
Length
]
;
for
(
int
i
=
0
;
i
<
sweets
.
Length
;
i
+
+
)
stairs
[
i
]
=
new
Stair
(
x
[
i
]
,
y
[
i
]
,
stairLength
[
i
]
,
sweets
[
i
]
)
;
Array
.
Sort
(
stairs
)
;
Component
comp
=
new
Component
(
)
;
for
(
int
i
=
0
;
i
<
stairs
.
Length
;
i
+
+
)
{
if
(
i
>
0
&
&
!
comp
.
Belongs
(
stairs
[
i
]
,
K
)
)
{
components
.
Add
(
comp
)
;
comp
=
new
Component
(
)
;
}
comp
.
Add
(
stairs
[
i
]
)
;
}
if
(
!
comp
.
Empty
)
components
.
Add
(
comp
)
;
compAdj
=
new
bool
[
components
.
Count
,
components
.
Count
]
;
best
=
new
int
[
components
.
Count
]
;
for
(
int
i
=
0
;
i
<
best
.
Length
;
i
+
+
)
best
[
i
]
=
-
1
;
for
(
int
i
=
0
;
i
<
components
.
Count
;
i
+
+
)
{
for
(
int
j
=
i
+
1
;
j
<
components
.
Count
;
j
+
+
)
{
if
(
components
[
i
]
.
CanReach
(
components
[
j
]
,
K
)
)
compAdj
[
i
,
j
]
=
true
;
}
}
int
res
=
0
;
for
(
int
i
=
0
;
i
<
components
.
Count
;
i
+
+
)
{
if
(
components
[
i
]
.
Y
<
=
K
)
res
=
Math
.
Max
(
res
,
Rec
(
i
)
)
;
}
return
res
;
}
private
int
[
]
best
;
private
List
<
Component
>
components
=
new
List
<
Component
>
(
)
;
private
int
Rec
(
int
startComp
)
{
if
(
best
[
startComp
]
!
=
-
1
)
return
best
[
startComp
]
;
int
res
=
0
;
for
(
int
i
=
0
;
i
<
components
.
Count
;
i
+
+
)
{
if
(
compAdj
[
startComp
,
i
]
)
res
=
Math
.
Max
(
res
,
Rec
(
i
)
)
;
}
return
best
[
startComp
]
=
res
+
components
[
startComp
]
.
Sweets
;
}
}
Edit
For you blog
Insert <br> for newlines
Insert newlines for newlines
Insert for indent
Surround code with <pre> element
For web page
About
|
Send us feedback