20220305美团笔试

四篇代码示例分别展示了数组处理、动态规划求解最长递增子序列、字符串操作和树状数组/线段树的运用。第一段代码通过处理数组找出连续的不递减元素个数;第二段实现动态规划计算数组中窗口最大和;第三段处理字符串操作,根据给定操作计算字符串长度;第四段疑似涉及动态更新区间最大值的问题。这些代码涵盖了基础算法到高级数据结构的应用。

1

#include <iostream>
#include <set>
#include <vector>
#include <algorithm>

using namespace std;

const int N = 200010;
bool num[N];

int main(){
    int n; scanf("%d", &n);

    for(int i=0; i<n; i++){
        int t; scanf("%d", &t);
        num[t] = true;
    }

    for(int i=1; i<N; i++){
        if(num[i] && num[i-1]) num[i] = false;
    }

    int cnt = 0;
    for(int i=0; i<N; i++){
        if(num[i]) cnt ++;
    }

    printf("%d", cnt);
}

#2
https://blog.youkuaiyun.com/winter2121/article/details/72848482

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=1e6+5;
const int INT_INF=0x3f3f3f3f;
int n,m = 2;
ll a[N],multi_dp[2][N];  
int main()
{
    scanf("%d",&n);
    for(int i=1;i<=n;i++)
        scanf("%lld",a+i);
    for(int i=0;i<=n;i++)
        multi_dp[0][i]=0,multi_dp[1][i]=0;   

    for(int i=1,k=1;i<=m;i++,k^=1)    
    {
        multi_dp[k][i-1]=-INT_INF;   
        ll maxpre=-INT_INF;    
        for(int j=i;j<=n-m+i;j++)
        {
            maxpre=max(maxpre,multi_dp[k^1][j-1]);     
            multi_dp[k][j]=max(multi_dp[k][j-1],maxpre)+a[j];
        }
    }
    ll ans=-INT_INF;
    for(int i=m;i<=n;i++)
        ans=max(ans,multi_dp[m&1][i]);
    printf("%lld\n",ans);
}

3

#include <iostream>
using namespace std;

const int N = 1005;

char ops[N];
int vals[N];
bool gps[3][N];
int n, k;

int help(int i)
{
    int m = 0;

    int l = 0, r = 0;
    while (l <= n && r <= n){
        if(!gps[i][r]) r++;
        else{
            m = max(m, r - l);
            l = r; r++;
        }
    }
    return m;
}


int main(){
    cin >> n >> k;

    for(int i=0; i<k; i++){
        cin >> ops[i];
    }

    for(int i=0; i<k; i++){
        cin >> vals[i];
    }

    gps[0][0] = gps[0][n] = true;
    gps[1][0] = gps[1][n] = true;
    gps[2][0] = gps[2][n] = true;

    int mx = n, my = n, mz = n;

    for(int i=0; i<k; i++){
        char c = ops[i];
        int v = vals[i];
        if(c == 'x') {gps[0][vals[i]] = true; mx = help(0); }
        if(c == 'y') {gps[1][vals[i]] = true; my = help(1); }
        if(c == 'z') {gps[2][vals[i]] = true; mz = help(2); }
        printf("%d\n", mx*my*mz);
    }


}

4

树状数组?线段树? 变形?

5

https://blog.youkuaiyun.com/benbendlianlian/article/details/108614510

/*
*

4 5
1234 3 0
1204 3 0
5678 0 1
5679 0 0
5670 0 0

1284
*/

美团笔试算法题目汇总如下: - **2021春招笔试题 - 学生值日问题**:小美和小团的班上有n个人,分别编号为1到n,小美的编号为1,小团的编号为2。给定一个n×n的矩阵`duty`,用于确定每天值日的学生。第一天小美值日,第二天小团值日,从第三天开始,第i天值日的学生编号由`duty[a2 - 1][a1 - 1]`确定,其中`a1`是前一天值日学生的编号,`a2`是当天值日学生的编号。要求根据输入的天数m,计算并输出第m天值日学生的编号。示例输入:`3 7`,`0 3 2`,`3 0 3`,`2 1 0` [^1]。 ```java package MT; import java.util.Scanner; public class MT4 { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int m = sc.nextInt(); int[][] duty = new int[n][n]; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { duty[i][j] = sc.nextInt(); } } System.out.println(studentID(m, duty)); } static int studentID(int m, int[][] dutyTable) { int id = 0; int a1 = 1, a2 = 2; int i = 0; while (i < (m - 2)) { id = dutyTable[a2 - 1][a1 - 1]; a1 = a2; a2 = id; i++; } return id; } } ``` - **美团笔试题目 - 深度优先搜索问题**:给定一个二维数组`a`,通过深度优先搜索(DFS)遍历数组元素。根据不同的边界条件,递归调用`dfs`函数进行元素的输出和继续搜索。当数组长度为0时返回 -1,否则通过`dfs`函数进行搜索后返回1 [^2]。 ```java public static int solve(int[][] a) { int N = a.length; if (N == 0) return -1; dfs(a, 0, N - 1, N); return 1; } public static void dfs(int[][] arr, int row, int col, int N) { if (row < N - 1 && col == N - 1) { System.out.println(arr[row][col]); dfs(arr, 0, N - row - 2, N); return; } if (row == N - 1 && col == N - 1) { System.out.println(arr[row][col]); dfs(arr, 1, 0, N); return; } if (row == N - 1 && col < N - 1 && col > 0) { System.out.println(arr[row][col]); dfs(arr, N - col, 0, N); return; } if (row == N - 1 && col == 0) { System.out.println(arr[row][col]); return; } System.out.print(arr[row][col]); System.out.print(" "); dfs(arr, row + 1, col + 1, N); return; } ``` - **2024 - 03 - 23美团春招笔试题**:包含多个问题,如K小姐的华丽灯光秀、K小姐的字符串修改、A先生的玩具摆放、K小姐的密码设计、LYA小姐的花园整理、K小姐的树林漫步等。每个问题都有相应的问题描述、输入格式、输出格式、样例输入、样例输出、数据范围和题解及参考代码 [^3]。 - **美团笔试 - 数字查找问题**:给定一个长度不超过1000的字符串,字符串仅由数字字符组成。要求找出未出现过的最小正整数,如果所有1 - 9的数字都出现过,则找出出现次数最少的数字,若有多个数字出现次数最少,则输出其中最小的数字,若所有数字都出现过且出现次数相同,则输出1后面跟和出现次数相同数量的0 [^4]。 ```cpp #include <iostream> #include <string.h> using namespace std; int main() { char c[1001]; int su = 0, num[10]; for (int i = 0; i < 10; i++) num[i] = 0; cin >> c; int len = strlen(c); for (int i = 0; i < len; i++) num[c[i] - '0']++; for (int i = 1; i <= 9; i++) if (!num[i]) { cout << i; return 0; } if (!num[0]) { cout << 1 << 0; return 0; } int minn = 10000; for (int i = 0; i <= 9; i++) if (minn > num[i]) minn = num[i]; for (int i = 1; i <= 9; i++) if (num[i] == minn) { while (minn--) cout << i; cout << i; return 0; } cout << 1; while (minn--) cout << 0; cout << 0; return 0; } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ColaForced

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值