leetcode 春季编程大赛-个人赛

第一次打leetcode比赛,扑街...

第一题:LCP 28. 采购方案

解决方法:排序;对于每个元素,二分查找target-nums[i]

class Solution {
public:
    int purchasePlans(vector<int>& nums, int target) {
        int ans = 0;
        sort(nums.begin(), nums.end());
        for(auto it = nums.begin();it != nums.end();it++) {
            if(*it >= target)  break;
            int pos = upper_bound(nums.begin(), it, (target-(*it))) - nums.begin();
            // cout << *it << " " << pos << endl;
            ans = (ans + pos) % 1000000007;
        }
        return ans;
    }
};

第二题:LCP 29. 乐团站位

解决方法:求出在第几圈;判断在上、下、左、右哪个方位;求出编号再取模。

代码可以整理合并一下

class Solution {
public:
    int orchestraLayout(int num, int xPos, int yPos) {
    long long pos;
    long long len;
    if(xPos <= yPos) {
        if(xPos + yPos  <= num-1) {  // 1
            len = xPos;
            pos = 4*((num+1)*len - (1+len)*len);
            pos += yPos+1-len;
        } else {  // 2
            len = num - 1 - yPos;
            pos = 4*((num+1)*len - (1+len)*len);
            pos += num-2*len-1;
            pos += xPos+1-len;
        }
    } else {
        if(xPos + yPos >= num-1) {  // 3
            len = num - 1 - xPos;
            pos = 4*((num+1)*len - (1+len)*len);
            pos += (num-2*len-1)*2;
            pos += (num-yPos-len);
        } else { // 4
            len = yPos;
            pos = 4*((num+1)*len - (1+len)*len);
            pos += (num-2*len-1)*3;
            pos += num-xPos-len;
        }  // 2 2
    }
    int ans = pos % 9;
    if(ans == 0)  ans = 9;
    return ans;
    }
};

第三题:LCP 30. 魔塔游戏

解决方法:和《加油站》那题类似,优先队列+贪心

开long long,开long long,开long long

class Solution {
public:
    int magicTower(vector<int>& nums) {
        long long sum = 1;
        for(int num : nums)  sum += num;
        if(sum <= 0)  return -1;

        deque<int>mynums;
        for(int num : nums)  mynums.push_back(num);

        priority_queue<int>q;
        long long cur = 1;
        long long cnt = 0;
        while(!mynums.empty()) {
            int tmp = mynums.front();mynums.pop_front();
            // cout << tmp << endl;
            if(tmp >= 0) {
                cur += tmp;
            } else {
                cur += tmp;
                q.push(-tmp);

                while(cur <= 0 && (!q.empty())) {
                    int t = q.top();q.pop();
                    cur += t;
                    mynums.push_back(-t);
                    cnt++;
                }
            }
            // cout << "cur: " << cur << endl;
        }
        return cnt;
    }
};

第四题:LCP 31. 变换的迷宫

解决方法:记忆化搜索 yyds,dp[t][i][j][tmp][forever][used] 表示t时刻,(i, j)位置,tmp=1表示使用了临时卷轴,forever=1表示使用了永久卷轴,used=1表示处于使用了永久卷轴的地方

参考https://leetcode-cn.com/problems/Db3wC1/comments/873211

class Solution {
public:
    bool dp[105][55][55][2][2][2];
    const int dx[4] = {-1, 0, 1, 0};
    const int dy[4] = {0, 1, 0, -1};
    int n, m, T;
    void dfs(int t, int i, int j, int tmp, int forever, int used, vector<vector<string>>& maze) {
        bool& ans = dp[t][i][j][tmp][forever][used];
        if(ans)  return;
        ans = true;
        if(t == T-1)  return;
        // 停在原地
        if(used)  dfs(t+1, i, j, tmp, forever, used, maze);
        if(maze[t+1][i][j] == '.') 
            dfs(t+1, i, j, tmp, forever, used, maze);
        else {
            if(tmp == 0)  dfs(t+1, i, j, 1, forever, used, maze);
            if(forever == 0)  dfs(t+1, i, j, tmp, 1, 1, maze);
        }
        // 四周走动
        for(int k = 0;k < 4;k++) {
            int xx = i+dx[k], yy = j+dy[k];
            if(xx >= 0 && xx < n && yy >= 0 && yy < m) {
                if(maze[t+1][xx][yy] == '.')
                    dfs(t+1, xx, yy, tmp, forever, 0, maze);  // used改为0
                else {
                    if(tmp == 0)
                        dfs(t+1, xx, yy, 1, forever, 0, maze);
                    if(forever == 0)
                        dfs(t+1, xx, yy, tmp, 1, 1, maze);
                }
            }
        }
    }
    bool escapeMaze(vector<vector<string>>& maze) {
        T = maze.size();
        n = maze[0].size(), m = maze[0][0].size();
        dfs(0, 0, 0, 0, 0, 0, maze);
        for(int t = 0;t < T;t++)
            for(int tmp = 0;tmp < 2;tmp++)
                for(int forever = 0;forever < 2;forever++)
                    for(int used = 0; used < 2;used++)
                        if(dp[t][n-1][m-1][tmp][forever][used])  return true;

        return false;
    }
};

第五题:LCP 32. 批量处理任务

据说是经典题,略

posted @ 2021-04-07 15:43  Rogn  阅读(173)  评论(0编辑  收藏  举报