AcWing 600. 仰视奶牛 - Java
原题链接
简单
作者:
KYCygni
,
2021-02-02 10:29:27
,
所有人可见
,
阅读 383
Java 代码
import java.util.Scanner;
public class Main
{
public static void main(String[] args) throws Exception
{
Scanner cin = new Scanner(System.in);
int numOfCows = cin.nextInt();
int[] heights = new int[numOfCows];
int[] lookups = new int[numOfCows];
for (int i=0; i < heights.length; i++)
{
heights[i] = cin.nextInt();
}
for (int i=heights.length-1; i >= 0; i--)
{
lookups[i] = 0;
int h = heights[i];
if ( i < heights.length-1)
{
int h2 = heights[i+1];
if (h == h2)
lookups[i] = lookups[i + 1];
else if (h < h2)
lookups [i] = i + 2;
else if (lookups [i+1] == 0)
lookups [i] = 0;
else
{
int j = lookups [i+1] - 1;
boolean done = false;
while ( j < heights.length && h > heights[j] && lookups[j] != 0 )
{
j = lookups[j] -1;
}
if (j < heights.length && h < heights[j] )
lookups[i] = j + 1;
else if (j < heights.length)
lookups[i] = lookups[j];
}
}
}
for (int i=0; i < lookups.length; i++)
{
System.out.println (lookups[i]);
}
}
}