美团笔试code

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();
    }
}

#美团##美团笔试#
全部评论
这个思路一模一样,但是 k * (i+1) 忘了取 long,臭掉了啊,只过了 25%
1 回复 分享
发布于 2023-08-26 12:17 江西
这t4排序就完事了?我特么想复杂了
1 回复 分享
发布于 2023-08-26 12:19 广东
为什么最后一题平均数 没人用滑动窗口 不行吗 我只能过30%,解答错误
1 回复 分享
发布于 2023-08-26 12:24 广东
佬,看看realme手机秋招,全球Top5厂商,hc多多,岗位多多
1 回复 分享
发布于 2023-08-26 23:06 广东
大佬t1的怎么写可以看一下嘛?我为啥一直通不过
点赞 回复 分享
发布于 2023-08-26 12:15 广东
t1通过率6% t3测试用例通过了不知道为啥提交通过率是0 t5超时
点赞 回复 分享
发布于 2023-08-26 12:29 山东

相关推荐

4 24 评论
分享
牛客网
牛客企业服务