AtCoder Beginner Contest D - Lamp Difficulty: 1080
Dieses Thema, 2D-Array
Was Sie tun, ist einfach.
...#..#. | Ursprüngliches Array |
---|---|
12301201 | Scannen Sie das Original-Array von links nach rechts |
33302201 | Scannen Sie das gescannte Array von rechts nach links |
Scannen Sie insgesamt zweimal, um den Lichtbereich in linker und rechter Richtung zu ermitteln. Scannen Sie anschließend zweimal nach oben und unten, um den Lichtbereich in Auf- und Abwärtsrichtung zu ermitteln.
Summieren Sie die Werte des linken und rechten Arrays sowie des oberen und unteren Arrays, um den Maximalwert zu ermitteln. Java
lamp.java
import java.util.*;
class Main {
public static void main(String[] args) {
final Scanner sc = new Scanner(System.in);
final int H = Integer.parseInt(sc.next());
final int W = Integer.parseInt(sc.next());
final char S[][] = new char[H+2][W+2];
for (int i=1; i<H+1; i++) {
S[i] = ("#" + sc.next() + "#").toCharArray();
}
sc.close();
for (int i=0; i<W+2; i++) {
S[0][i] = '#';
S[H+1][i] = '#';
}
int lr[][] = new int[H+2][W+2];
int ud[][] = new int[H+2][W+2];
for (int i=1; i<H+1; i++) {
int cnt = 0;
for (int j=0; j<W+1; j++) {
if (S[i][j]=='.') {
cnt++;
} else {
cnt = 0;
}
lr[i][j] = cnt;
}
for (int j=W; j>0; j--) {
if (lr[i][j]==0) {
cnt = 0;
} else if (cnt==0) {
cnt = lr[i][j];
} else {
lr[i][j] = cnt;
}
}
}
for (int j=1; j<W+1; j++) {
int cnt = 0;
for (int i=0; i<H+1; i++) {
if (S[i][j]=='.') {
cnt++;
} else {
cnt = 0;
}
ud[i][j] = cnt;
}
for (int i=H; i>0; i--) {
if (ud[i][j]==0) {
cnt = 0;
} else if (cnt==0) {
cnt = ud[i][j];
} else {
ud[i][j] = cnt;
}
}
}
int ans = 0;
for (int i=1; i<H+1; i++) {
for (int j=1; j<W+1; j++) {
int cnt = lr[i][j] + ud[i][j];
if (ans<cnt) ans = cnt;
}
}
ans -= 1;
System.out.println(ans);
}
}
Es ist eine Schnur ohne Verdrehung.
Ein Blog über die Geschwindigkeit von Skriptsprachen. Unter diesen wird als striktes Beispiel in der Skriptsprache dieses * D - Lamp * verwendet.
Ruby
ruby.rb
h, w = gets.split.map(&:to_i)
s = Array.new(h + 1).map{Array.new(w + 1, 0)}
1.upto(h) do |i|
c = 0
l = 1
b = gets
1.upto(w) do |j|
if b[j - 1] == '.'
l = j if c == 0
c += 1
elsif c > 0
l.upto(j - 1) do |k|
s[i][k] = c
end
c = 0
end
if j == w && c > 0
l.upto(j) do |k|
s[i][k] = c
end
end
end
end
ans = 0
1.upto(w) do |j|
c = 0
l = 1
1.upto(h) do |i|
if (s[i][j] > 0)
l = i if c == 0
c += 1
elsif c > 0
l.upto(i - 1) do |k|
ans = s[k][j] + c if ans < s[k][j] + c
end
c = 0
end
if i == h && c > 0
l.upto(i) do |k|
ans = s[k][j] + c if ans < s[k][j] + c
end
end
end
end
puts ans - 1
Der Code mit einer leicht festgelegten Anzahl von Scans war "TLE" in der alten Umgebung "Ruby (2.3.3)", aber er scheint in der neuen Umgebung "Ruby (2.7.1)" irgendwie zu bestehen.
C++14 | Java | Ruby 2.3.1 | Ruby 2.7.1 | |
---|---|---|---|---|
Alte Umgebung | Alte Umgebung | Alte Umgebung | Neue Umgebung | |
Codelänge(Byte) | 1420 | 2044 | 797 | 797 |
Ausführungszeit(ms) | 94 | 465 | TLE | 1567 |
Erinnerung(KB) | 35328 | 108364 | 36988 | 47324 |
Fallname | Ausführungszeit (alt) | Ausführungszeit (neu) | Alt und Neu |
---|---|---|---|
01.txt | 7 | 59 | |
02.txt | 7 | 63 | |
12.txt | 1460 | 1089 | 1.34 |
13.txt | 1844 | 1250 | 1.48 |
18.txt | 1983 | 1289 | 1.54 |
19.txt | 1949 | 1360 | 1.43 |
20.txt | 11 | 59 | |
21.txt | 7 | 63 | |
22.txt | 22 | 70 | |
23.txt | 1449 | 981 | 1.48 |
24.txt | 9 | 62 | |
25.txt | 10 | 63 |
Es scheint ungefähr 1,4-mal schneller zu sein. Die Ära des Rubins ist angebrochen.
Referenzierte Site
Recommended Posts