讨论/《美团 2021 届秋季校园招聘笔试真题》 - 小团的复制粘贴/
《美团 2021 届秋季校园招聘笔试真题》 - 小团的复制粘贴
共 7 个回复
#include <iostream>

using namespace std;

const int N = 100010;

int n, m;
int a[N], b[N];

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    cin >> n;
    for (int i = 1; i <= n; i++) {
        cin >> a[i];
        b[i] = -1;
    }

    cin >> m;
    while (m--) {
        int op, k, x, y;
        cin >> op;

        if (op == 1) {
            cin >> k >> x >> y;
            for (int i = x, j = y; i < x + k; i++, j++) b[j] = a[i];
        } else {
            cin >> x;
            cout << b[x] << endl;
        }
    }

    return 0;
}
import java.io.*;
import java.util.*;

class Solution {
    static final int N = 100010;
    
    static int n, m;
    static int[] a = new int[N], b = new int[N];

    public static void main(String[] args) throws IOException {
        var reader = new BufferedReader(new InputStreamReader(System.in));
        var writer = new BufferedWriter(new OutputStreamWriter(System.out));

        n = Integer.parseInt(reader.readLine());
        var strings = reader.readLine().split(" ");
        for (int i = 1; i <= n; i++) {
            a[i] = Integer.parseInt(strings[i - 1]);
            b[i] = -1;
        }

        m = Integer.parseInt(reader.readLine());
        while (m-- != 0) {
            strings = reader.readLine().split(" ");
            int op = Integer.parseInt(strings[0]);

            if (op == 1) {
                int k = Integer.parseInt(strings[1]);
                int x = Integer.parseInt(strings[2]);
                int y = Integer.parseInt(strings[3]);
                for (int i = x, j = y; i < x + k; i++, j++) b[j] = a[i];
            } else {
                int x = Integer.parseInt(strings[1]);
                writer.write(b[x] + "\n");
            }
        }

        writer.flush();
        reader.close();
        writer.close();
    }
}
1
#include <iostream>
#include <vector>
using namespace std;

int main(){
    int size;
    cin >> size;
    vector<int> A(size,0);
    vector<int> B(size,-1);
    for (int i = 0; i < size; i ++){
        cin >> A[i];
    }
    int count;
    cin >> count;
    for (int i = 0; i < count; i ++){
        int type;
        cin >> type;
        if (type == 1){
            int size_copy ;
            cin >> size_copy;
            int begin_idxA,begin_idxB;
            cin >> begin_idxA >> begin_idxB;
            begin_idxA -= 1;
            begin_idxB -= 1;
            size_copy = min(size - begin_idxA,size_copy);
            for (int j = 0; j < size_copy; j ++){
                if (begin_idxB + j >= size)
                break;
                B[begin_idxB + j] = A[begin_idxA + j];
            }
        }else{
            int idx_B;
            cin >> idx_B;
            cout << B[idx_B - 1] << endl;

        }
    }
    return 0;
}


n = int(input())
A = list(map(int, input().strip().split()))
m = int(input())
B = [-1]*n

while m:
    m-=1
    command = list(map(int, input().strip().split()))
    if command[0] == 2:
        print(B[command[1]-1])
    if command[0] == 1:
        k,x,y= command[1],command[2],command[3]
        B[y:y+k] = A[x:x+k]
        print(A,B)

我还纳闷怎么过不了呢。。。

前面的操作总是会被后面重复的覆盖,从后往前遍历就ok了

效率可以

import java.util.*;
import java.lang.*;

class Solution{
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int[] nums = new int[n+1];

        for(int i = 1;i <= n;i++){
            nums[i] = sc.nextInt();
        }

        int m = sc.nextInt();

        List<int[]> list = new ArrayList<>();
        while(m-- > 0){
            int type =sc.nextInt();
            if(type == 1){
                int k = sc.nextInt();
                int x = sc.nextInt();
                int y = sc.nextInt();        
                list.add(new int[]{x,y,k});     
            }else{
                int idx = sc.nextInt();
                int i = list.size() - 1;
                for(;i >= 0;i--){
                    int[] arr = list.get(i);
                    if(arr[1] <= idx && arr[1] + arr[2] > idx){
                        System.out.println(nums[idx-arr[1]+arr[0]]);
                        break;
                    }
                }
                if(i < 0){
                    System.out.println(-1);
                }
            }
        }
    }
}
/**
 * 记录每次复制粘贴的操作[1, k, x, y],从队尾放入队列 optionList 中,例如:[op2, op1]
 * 当读取 b 序列 x 的值时,从队头遍历 optionList:
 *   - 判断 op.y <= x && x < op.y + op.k
 *      - 如果是,表示值在本次操作中,则返回 v = A[op.x + (x - op.y)]
 *   
 *   - 当遍历完操作列表,没有找到 v , 则返回 -1
 */

写着1<=n<=2000,实际上数组开到200000才过。数组开2010的话只能过6个点,有点坑人