【九度OJ合集】P1465-P1515
2014-09-04 16:43
169 查看
P1497
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.StreamTokenizer;
public class Exercise1497
{
public static void main(String[] args) throws IOException
{
StreamTokenizer sc = new StreamTokenizer(new BufferedReader(
new InputStreamReader(System.in)));
while (sc.nextToken() != StreamTokenizer.TT_EOF) {
int m = (int) sc.nval;
sc.nextToken();
int n = (int) sc.nval;
int[][] a = new int[m]
;
for (int i = 0; i < m; i++)
for (int j = 0; j < n; j++) {
sc.nextToken();
a[i][j] = (int) sc.nval;
}
int[] h = new int
;
int left;
int right;
int max = 0;
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
if (a[i][j] == 1)
h[j]++;
else
h[j] = 0;
}
for (int j = 0; j < n; j++) {
if (h[j] == 0)
continue;
left = j;
right = j;
while (left >= 0 && h[left] >= h[j])
left--;
while (right <= n - 1 && h[right] >= h[j])
right++;
int square = h[j] * (right - left - 1);
if (square > max)
max = square;
}
}
System.out.println(max);
}
}
}
找出第i行第j列的高度,并寻找左右的宽度,超时
优化:对于左边,如果h[j-1]<h[j],则找到左边界j
如果h[j-1]>h[j],则可以从h[j-1]对应的列开始继续向左寻找
右边同理(注意从n递减)
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.StreamTokenizer;
public class Exercise1497
{
public static void main(String[] args) throws IOException
{
StreamTokenizer sc = new StreamTokenizer(new BufferedReader(
new InputStreamReader(System.in)));
while (sc.nextToken() != StreamTokenizer.TT_EOF) {
int m = (int) sc.nval;
sc.nextToken();
int n = (int) sc.nval;
int[][] a = new int[m]
;
for (int i = 0; i < m; i++)
for (int j = 0; j < n; j++) {
sc.nextToken();
a[i][j] = (int) sc.nval;
}
int[] h = new int
;
int[] left=new int
;
int[] right=new int
;
int max = 0;
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
if (a[i][j] == 1)
h[j]++;
else
h[j] = 0;
}
for (int j = 0; j < n; j++) {
if (h[j] == 0)
continue;
int k=j;
while (k> 0 && h[k-1] >= h[j])
k=left[k-1];
left[j]=k;
}
for (int j = n-1; j >=0; j--) {
if (h[j] == 0)
continue;
int k=j;
while (k<n-1&&h[k+1]>=h[j])
k=right[k+1];
right[j]=k;
int square = h[j] * (right[j] - left[j] + 1);
if (square > max)
max = square;
}
}
System.out.println(max);
}
}
}
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.StreamTokenizer;
public class Exercise1497
{
public static void main(String[] args) throws IOException
{
StreamTokenizer sc = new StreamTokenizer(new BufferedReader(
new InputStreamReader(System.in)));
while (sc.nextToken() != StreamTokenizer.TT_EOF) {
int m = (int) sc.nval;
sc.nextToken();
int n = (int) sc.nval;
int[][] a = new int[m]
;
for (int i = 0; i < m; i++)
for (int j = 0; j < n; j++) {
sc.nextToken();
a[i][j] = (int) sc.nval;
}
int[] h = new int
;
int left;
int right;
int max = 0;
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
if (a[i][j] == 1)
h[j]++;
else
h[j] = 0;
}
for (int j = 0; j < n; j++) {
if (h[j] == 0)
continue;
left = j;
right = j;
while (left >= 0 && h[left] >= h[j])
left--;
while (right <= n - 1 && h[right] >= h[j])
right++;
int square = h[j] * (right - left - 1);
if (square > max)
max = square;
}
}
System.out.println(max);
}
}
}
找出第i行第j列的高度,并寻找左右的宽度,超时
优化:对于左边,如果h[j-1]<h[j],则找到左边界j
如果h[j-1]>h[j],则可以从h[j-1]对应的列开始继续向左寻找
右边同理(注意从n递减)
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.StreamTokenizer;
public class Exercise1497
{
public static void main(String[] args) throws IOException
{
StreamTokenizer sc = new StreamTokenizer(new BufferedReader(
new InputStreamReader(System.in)));
while (sc.nextToken() != StreamTokenizer.TT_EOF) {
int m = (int) sc.nval;
sc.nextToken();
int n = (int) sc.nval;
int[][] a = new int[m]
;
for (int i = 0; i < m; i++)
for (int j = 0; j < n; j++) {
sc.nextToken();
a[i][j] = (int) sc.nval;
}
int[] h = new int
;
int[] left=new int
;
int[] right=new int
;
int max = 0;
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
if (a[i][j] == 1)
h[j]++;
else
h[j] = 0;
}
for (int j = 0; j < n; j++) {
if (h[j] == 0)
continue;
int k=j;
while (k> 0 && h[k-1] >= h[j])
k=left[k-1];
left[j]=k;
}
for (int j = n-1; j >=0; j--) {
if (h[j] == 0)
continue;
int k=j;
while (k<n-1&&h[k+1]>=h[j])
k=right[k+1];
right[j]=k;
int square = h[j] * (right[j] - left[j] + 1);
if (square > max)
max = square;
}
}
System.out.println(max);
}
}
}
相关文章推荐
- 【九度OJ合集】P1050-P1100
- 【九度OJ合集】P1516-P1557
- 【九度OJ合集】P1359-1414
- 【九度OJ合集】P1201-P1358
- 【九度OJ合集】P1000-P1049
- 九度oj-1059-abc
- 九度OJ-浙大机试-题目1437:To Fill or Not to Fill
- 九度OJ 1089 递推数列
- 九度OJ 1109 连通图
- 【剑指Offer面试题】 九度OJ1372:最大子向量和(连续子数组的最大和)
- 九度oj 题目1467:二叉排序树
- 【剑指Offer面试题】 九度OJ1373:整数中1出现的次数(从1到n整数中1出现的次数)
- 九度oj 1020
- 九度oj 1048
- <C/C++基础>九度OJ题目1000--1049解题练习(二)
- 九度OJ 题目1070:今年的第几天?
- <九度 OJ>题目1059:abc
- 九度OJ 题目1081:递推数列
- 九度oj 1132
- C++ 数字翻转(九度OJ 1089)