1.城市建设
import java.util.Scanner;
class Demo1
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
sc.nextLine();
long x_max=Long.MIN_VALUE;
long y_max=Long.MIN_VALUE;
long x_min=Long.MAX_VALUE;
long y_min=Long.MAX_VALUE;
while(n-->0)
{
int x=sc.nextInt();
int y=sc.nextInt();
sc.nextLine();
x_max=x>x_max?x:x_max;
y_max=y>y_max?y:y_max;
x_min=x<x_min?x:x_min;
y_min=y<y_min?y:y_min;
}
long edge=(y_max-y_min)*(x_max-x_min);
System.out.println("edge="+edge);
}
}
2.看花
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner in=new Scanner(System.in);
int n=in.nextInt();
int m=in.nextInt();
int[] res=new int[n];
for(int i=0;i<n;i++) {
res[i]=in.nextInt();
}
int Q=in.nextInt();
ArrayList<int[]> list=new ArrayList<int[]>();
for(int i=0;i<Q;i++) {
int[] tt=new int[2];
tt[0]=in.nextInt();
tt[1]=in.nextInt();
list.add(tt);
}
for(int i=0;i<list.size();i++) {
HashSet<Integer> set=new HashSet<>();
int left=list.get(i)[0];
int right=list.get(i)[1];
for(int j=left-1;j<right;j++) {
set.add(res[j]);
}
System.out.println(set.size());
}
}
}
3.Array
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();// 数组长度n
int[] a = new int[n];
int[] b = new int[n];
for (int i = 0; i < n; i++) {
a[i] = sc.nextInt();
}
for (int i = 0; i < n; i++) {
b[i] = sc.nextInt();
}
int r = compute(a, b, n);
System.out.println(r);
}
public static int compute(int[] a, int[] b, int n) {
int MaxLen = 0;
for (int i = 0; i < n; i++) {
int Cj = a[i];
for (int j = i + 1; j < n; j++) {
int Ci = a[j];
int inde = index(b, Ci);
for (int k = inde; k < n; k++) {
int len = 0;
if (b[k] == Cj)
len = k - inde + 1;
if (len > MaxLen)
MaxLen = len;
}
}
}
return MaxLen;
}
public static int index(int[] arr, int key) {
for (int i = 0; i < arr.length; i++) {
if (arr[i] == key)
return i;
}
return -1;
}
}