Solution to the 284th weekly game of Lituo

A.6031. Find all K-Nearest Neighbor Subscripts in an Array

find each nums[j] == key nums[j] == keynums[j]==jjof k e yj , combine allmax ( 0 , j − k ) , min ( j + k , n − 1 ) max(0,jk),min(j+k,n-1)max(0,jk),m i n ( j+k,n1 ) The subscripts can be counted up. 1 <= nums.length <= 1000 1 <= nums.length <= 10001<=nums.length<=1 0 0 0 , without optimization, it can be directlyO ( n 2 ) O(n^2)O ( n2 )Over.

AC code:

class Solution
{
    
    
public:
    vector<int> findKDistantIndices(vector<int> &nums, int key, int k)
    {
    
    
        int n = nums.size();
        set<int> st;
        for (int i = 0; i < n; i++)
            if (nums[i] == key)
                for (int j = max(0, i - k); j <= min(n - 1, i + k); j++)
                    st.insert(j);
        vector<int> ans;
        for (auto it : st)
            ans.emplace_back(it);
        return ans;
    }
};

B.5203. Counting artifacts that can be extracted

It seems that the topic is very complicated, but the topic guarantees that each dig digThe d i g elements are not repeated, that is, they are not repeated every time they are excavated, so it is only necessary to countwhose area = =the number of excavations.

AC code:

class Solution
{
    
    
public:
    int digArtifacts(int n, vector<vector<int>> &artifacts, vector<vector<int>> &dig)
    {
    
    
        map<int, int> num;
        int mp[1010][1010];
        memset(mp, 0, sizeof mp);
        int id = 0;//给元件编号
        for (auto it : artifacts)
        {
    
    
            num[++id] = (it[2] - it[0] + 1) * (it[3] - it[1] + 1);
            for (int i = it[0]; i <= it[2]; i++)
                for (int j = it[1]; j <= it[3]; j++)
                    mp[i][j] = id;
        }
        int ans = 0;
        for (auto it : dig)
        {
    
    
            int val = mp[it[0]][it[1]];
            if (num[val] == 1)
                ans++;
            else
                num[val]--;
        }
        return ans;
    }
};

C.5227. Maximize top element after K operations

The front row is also quite miserable, and I personally feel that the title is ambiguous.

Two situations:

  1. First use k − 1 k-1k1 time put the firstk − 1 k-1k1 element removed, the last restores the largest value among the removed.
  2. kk before deletionk elements.

And − 1 -11 andk > n k>nk>In the special case of n ,special judgethat is fine.

AC code:

class Solution
{
    
    
public:
    int maximumTop(vector<int> &nums, int k)
    {
    
    
        int ans = 0, mx = 0;
        int n = nums.size();
        if (n == 1 && k % 2)
            return -1;
        for (auto it : nums)
            mx = max(mx, it);
        if (k > n)
            return mx;

        int p = min(n, k - 1);
        for (int i = 0; i < p; i++)
            ans = max(ans, nums[i]);
        if (p + 1 < n)
            ans = max(ans, nums[p + 1]);
        return ans;
    }
};

D.6032. Get the minimum weighted subgraph of the required path

  1. Starting from the starting point 1, find the shortest distance to each point dis 1 [ ] dis1[]d i s 1 [ ]
  2. Starting from the starting point 2, find the shortest distance to each point dis 2 [ ] dis2[]dis2[]
  3. Build an inverse graph, starting from the end point, find the shortest distance to each point dis 3 [ ] dis3[]dis3[]

Answer is min ⁡ 0 n − 1 dis 1 [ i ] + dis 2 [ i ] + dis 3 [ i ] \min ^{n-1}_0{dis1[i]+dis2[i]+dis3[i]}min0n1dis1[i]+dis2[i]+dis3[i]

AC code:

class Solution
{
    
    
public:
    long long minimumWeight(int n, vector<vector<int>> &edges, int src1, int src2, int dest)
    {
    
    
        const int N = 1e5 + 5;
        vector<pair<int, int>> mp[N], rev_mp[N];
        long long dis1[N], dis2[N], dis3[N];
        int vis1[N], vis2[N], vis3[N];

        for (auto it : edges)
        {
    
    
            mp[it[0]].emplace_back(make_pair(it[1], it[2]));
            rev_mp[it[1]].emplace_back(make_pair(it[0], it[2]));
        }
        for (int i = 0; i < n; i++)
        {
    
    
            dis1[i] = dis2[i] = dis3[i] = 1e18;
            vis1[i] = vis2[i] = vis3[i] = 0;
        }

        priority_queue<pair<long long, int>> q;
        dis1[src1] = 0;
        q.push(make_pair(0, src1));
        while (q.size()) // src1 出发 单源最短路
        {
    
    
            int u = q.top().second;
            long long w = -q.top().first;
            q.pop();
            if (vis1[u])
                continue;
            vis1[u] = 1;
            for (auto it : mp[u])
            {
    
    
                int v = it.first;
                if (w + it.second < dis1[v])
                {
    
    
                    dis1[v] = w + it.second;
                    q.push(make_pair(-dis1[v], v));
                }
            }
        }

        dis2[src2] = 0;
        q.push(make_pair(0, src2));
        while (q.size()) // src2 出发 单源最短路
        {
    
    
            int u = q.top().second;
            long long w = -q.top().first;
            q.pop();
            if (vis2[u])
                continue;
            vis2[u] = 1;
            for (auto it : mp[u])
            {
    
    
                int v = it.first;
                if (w + it.second < dis2[v])
                {
    
    
                    dis2[v] = w + it.second;
                    q.push(make_pair(-dis2[v], v));
                }
            }
        }

        dis3[dest] = 0;
        q.push(make_pair(0, dest));
        while (q.size()) //反图  dest 出发 单源最短路
        {
    
    
            int u = q.top().second;
            long long w = -q.top().first;
            q.pop();
            if (vis3[u])
                continue;
            vis3[u] = 1;
            for (auto it : rev_mp[u])
            {
    
    
                int v = it.first;
                if (w + it.second < dis3[v])
                {
    
    
                    dis3[v] = w + it.second;
                    q.push(make_pair(-dis3[v], v));
                }
            }
        }
        long long ans = 1e18;
        for (int i = 0; i < n; i++)
            ans = min(ans, dis1[i] + dis2[i] + dis3[i]);
        return ans == 1e18 ? -1 : ans;
    }
};

Guess you like

Origin blog.csdn.net/hesorchen/article/details/123457086