结果记得用 long 就行,题目数据给的是 1e9,防范越界即可。
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
int minX = Integer.MAX_VALUE;
int maxX = Integer.MIN_VALUE;
int minY = Integer.MAX_VALUE;
int maxY = Integer.MIN_VALUE;
for (int i = 0; i < n; i++) {
int x = scanner.nextInt();
if (x > maxX){
maxX = x;
}
if (x < minX){
minX = x;
}
int y = scanner.nextInt();
if (y > maxY){
maxY = y;
}
if (y < minY){
minY = y;
}
}
long x = maxX-minX;
long y = maxY-minY;
long len = x>y?x:y;
System.out.println(len*len);
}
}
作者:顽木芽
链接:https://www.nowcoder.com/discuss/99566
来源:牛客网
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
while (scanner.hasNext()) {
int n = scanner.nextInt();
int m = scanner.nextInt();
int[] watch = new int[n+1];
int i = 1;
while (i < n+1) {
watch[i++] = scanner.nextInt();
}
int q = scanner.nextInt();
i = 0;
int[][] questions = new int[q][2];
while (i < q) {
questions[i][0] = scanner.nextInt();
questions[i][1] = scanner.nextInt();
i++;
}
processData(watch, questions, n);
}
}
public static void processData(int[] watch, int[][] ques, int n) {
Set<Integer> flours = null;
for (int i = 0; i < ques.length; i++) {
int start = ques[i][0];
int end = ques[i][1];
if (start < 1) start = 1;
if (end > n) end = n;
flours = new HashSet<>();
while (start <= end) {
flours.add(watch[start++]);
}
System.out.println(flours.size());
}
}
作者:给个offer吧T^T
链接:https://www.nowcoder.com/discuss/99566
来源:牛客网
假设A中 X 在 Y 前面,那么B中肯定是反过来的。即是一个逆序。
因此可以直接把B反过来,求数组A和B的 LCS。
但是发现题目给的数据量 N = 50000.直接用普通的 LCS DP 解法肯定超时。
所以改成 LIS 的做法来做。虽然存在退化问题,但是Case里面明显没有包含这个情况。
因为数组中全是 1~n 的元素,并且不重复。
作者:易易易易易拉罐
链接:https://www.nowcoder.com/discuss/99612
来源:牛客网
#include <iostream>
#pragma warning (disable:4996)
#include <algorithm>
using namespace std;
#define MAXN 50010
const int INF = 1e9;
int idx[MAXN];
int b[MAXN];
int a;
int dp[MAXN];
int main(){
int n;
scanf("%d", &n);
for (int i = 0; i < n; i++) {
scanf("%d", &a);
idx[a - 1] = i;
}
for (int i = 0; i < n; i++) {
scanf("%d", &a);
b[i] = idx[a - 1];
dp[i] = INF; // 初始化为无限大
} //最长递增子序列
int pos = 0; // 记录dp当前最后一位的下标
dp[0] = b[0]; // dp[0]值显然为a[0]
for (int i = 1; i < n; i++) {
if (b[i] > dp[pos]) // 若a[i]大于dp数组最大值,则直接添加
dp[++pos] = b[i];
else // 否则找到dp中第一个大于等于a[i]的位置,用a[i]替换之。
dp[lower_bound(dp, dp + pos + 1, b[i]) - dp] = b[i]; // 二分查找
}
printf("%d\n", pos + 1);
return 0;
}