t5 前缀和+hashmap
package meituan;
import java.io.*;
import java.util.HashMap;
import java.util.Map;
public class Solution10 {
static StreamTokenizer in = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
static PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out));
static int n, k;
static final int N = 200010;
static long[] a = new long[N], s = new long[N];
static int nextInt() throws IOException {
in.nextToken();
return (int) in.nval;
}
// s[i + 1] - s[j] / i + 1 - j == k
// s[i + 1] = k*i + k -k*j + s[j]
// s[i + 1] - k(i + 1) = s[j] - k*j
public static void main(String[] args) throws IOException {
n = nextInt();
k = nextInt();
Map<Long, Integer> map = new HashMap<>();
for (int i = 0; i < n; i++) {
a[i] = nextInt();
s[i + 1] = a[i] + s[i];
}
int ans = -1;
map.put(0L, 0);
for (int i = 0; i < n; i++) {
long v = s[i + 1] - (long) k * (i + 1);
if(map.containsKey(v)){
int j = map.get(v);
ans = Math.max(ans, i - j + 1);
}else{
map.put(v, i + 1);
}
}
out.println(ans);
out.close();
}
}
t3 直接用大数
package meituan;
import java.io.*;
import java.math.BigDecimal;
import java.util.PriorityQueue;
public class Solution11 {
static StreamTokenizer in = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
static PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out));
static int n, k;
static final int MOD = (int) 1e9 + 7;
static int nextInt() throws IOException {
in.nextToken();
return (int) in.nval;
}
public static void main(String[] args) throws IOException {
n = nextInt();
k = nextInt();
PriorityQueue<BigDecimal> pq = new PriorityQueue<>((a, b) -> b.compareTo(a));
for (int i = 0; i < n; i++) {
long x = nextInt();
pq.offer(BigDecimal.valueOf(x));
}
BigDecimal ans = BigDecimal.valueOf(0);
while (k-- > 0){
BigDecimal x = pq.poll(), y = pq.poll();
System.out.println(x + " " + y);
pq.offer(x.multiply(y));
pq.offer(BigDecimal.valueOf(1));
}
while (!pq.isEmpty()){
ans = ans.add(pq.poll());
}
out.println(ans.divideAndRemainder(BigDecimal.valueOf(MOD))[1]);
out.close();
}
}
t1模拟
package meituan;
import java.io.*;
public class Solution6 {
static StreamTokenizer in = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
static PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out));
static int x, y, z;
static int nextInt() throws IOException {
in.nextToken();
return (int) in.nval;
}
public static void main(String[] args) throws IOException {
x = nextInt();
y = nextInt();
z = nextInt();
int ans = 0, sum = 0, i =0;
while (sum < z){
if(i % 3 == 0){
sum += x + y;
}else{
sum += x;
}
ans++;
i++;
}
out.println(ans);
out.close();
}
}
t2模拟
package meituan;
import java.io.*;
public class Solution7 {
static StreamTokenizer in = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
static PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out));
static int n, m, k, c;
static final int N = 100010;
static long[] cost = new long[N];
static int nextInt() throws IOException {
in.nextToken();
return (int) in.nval;
}
public static void main(String[] args) throws IOException {
n = nextInt();
m = nextInt();
while (n-- > 0) {
k = nextInt();
c = nextInt();
int avg = (c + k - 1) / k;
while (k-- > 1) {
int x = nextInt();
cost[x] += avg;
}
}
for (int i = 1; i <= m; i++) {
out.print(cost[i] + " ");
}
out.close();
}
}
t4排序
package meituan;
import java.io.*;
import java.lang.reflect.Array;
import java.util.Arrays;
public class Solution9 {
static StreamTokenizer in = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
static PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out));
static int q, n, m;
static final int N = 510;
//static int[] a = new int[N], b = new int[N];
static int nextInt() throws IOException {
in.nextToken();
return (int) in.nval;
}
public static void main(String[] args) throws IOException {
q = nextInt();
while (q-- > 0) {
n = nextInt();
m = nextInt();
int[] a = new int[n], b = new int[n];
for (int i = 0; i < n; i++) {
a[i] = nextInt();
}
for (int i = 0; i < n; i++) {
b[i] = nextInt();
}
Arrays.sort(a);
Arrays.sort(b);
boolean flag = true;
for (int i = 0, j = n - 1; i < n; i++, j--) {
if (!(a[i] + b[j] >= 1 && a[i] + b[j] <= m)) {
flag = false;
break;
}
}
out.println(flag ? "YES" : "NO");
}
out.close();
}
}
#美团##美团笔试#