Untitled

 avatar
unknown
java
17 days ago
2.0 kB
4
Indexable
import java.util.*;

public class FootpathBeautyForQueries {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        
        // Input number of blocks (n)
        int n = sc.nextInt();
        
        // Input number of queries (q)
        int q = sc.nextInt();
        
        // Input the block colors
        int[] blockColors = new int[n];
        for (int i = 0; i < n; i++) {
            blockColors[i] = sc.nextInt();
        }
        
        // Input the favorite colors for the queries
        int[] favoriteColors = new int[q];
        for (int i = 0; i < q; i++) {
            favoriteColors[i] = sc.nextInt();
        }
        
        // Preprocess to calculate maximum contiguous blocks for each color
        Map<Integer, Integer> colorMaxBeauty = new HashMap<>();
        int currentColor = -1;
        int currentCount = 0;
        
        for (int i = 0; i < n; i++) {
            if (blockColors[i] == currentColor) {
                currentCount++;
            } else {
                // Update the maximum beauty value for the previous color
                if (currentColor != -1) {
                    colorMaxBeauty.put(currentColor, Math.max(colorMaxBeauty.getOrDefault(currentColor, 0), currentCount));
                }
                currentColor = blockColors[i];
                currentCount = 1;
            }
        }
        // Update for the last color
        colorMaxBeauty.put(currentColor, Math.max(colorMaxBeauty.getOrDefault(currentColor, 0), currentCount));
        
        // Process each query and return the beauty value for each favorite color
        StringBuilder result = new StringBuilder();
        for (int color : favoriteColors) {
            result.append(colorMaxBeauty.getOrDefault(color, 0)).append("\n");
        }
        
        // Output the results
        System.out.println(result.toString());
        
        sc.close();
    }
}
Leave a Comment