P1392 取数

传送门:取数

如若你看完题解后,仍有问题,欢迎评论

首先说一下 我首先想到的思路 ( 20%通过率 ):通过dfs , 将所有的情况放入priority_queue中(greater<int>),维持堆中的数据为k个 -> TLE

代码:

cpp 复制代码
#include<bits/stdc++.h>
using namespace std;
int n, m, k;
const int N = 810;
int a[N][N];
priority_queue<int> heap;
void dfs(int i  , int sum)
{
    if (i == n + 1) {
        if (heap.size() == k && sum < heap.top()) {
            heap.pop(); heap.push(sum);
        }
        else if (heap.size() < k)heap.push(sum);
        return;
    }
    for (int k = 1; k <= m; k++)
    {
        dfs(i + 1, sum + a[i][k]);
    }
}
int main()
{
    scanf("%d%d%d", &n, &m, &k);
    for (int i = 1; i <= n; i++)
        for (int j = 1; j <= m; j++)
            scanf("%d", &a[i][j]);
    dfs(1, 0);
    
    vector<int> res;
    while (heap.size())
    {
        res.push_back(heap.top()); heap.pop();
    }
    reverse(res.begin(), res.end());
    for (auto e : res)
        printf("%d ", e);
    return 0;
}

错误代码的时间复杂度分析: n * m * klongk (建堆 + 遍历整个矩阵)> 10 ^ 8 因此TLE

优化方法:

时间复杂度分析:n * ( mlongm ( 排序 ) + klongk ( 调和级数 ) ) == nmlongm + nklongk = 10 ^ 6 (约等于 ) < 10 ^ 8 可以AC

AC Code:

cpp 复制代码
#include<bits/stdc++.h>
using namespace std;
const int N = 810;
int a[N] , b[N];
int n , m , k;
int x,y;
priority_queue<int> heap;
int main()
{
    scanf("%d%d%d",&n,&m,&k);
    heap.push(0);
    for( int i = 1 ; i <= n; i++)
    {
        x = 0; y = 0;
        while( heap.size()) b[++y] = heap.top(),heap.pop();
        for( int j= 1 ; j<= m;j++)scanf("%d",&a[j]);
        sort( a + 1 , a + m + 1 );
        for( int l = 1 ; l <= m;l++)
        {
            for( int r = y ; r ; r--)
            {
                if( x < k )heap.push( a[l] + b[r] ),x++;
                else if( heap.top() < a[l] + b[r] )break;
                else heap.pop(),heap.push(a[l] + b[r] );
            }
        }
    }
    vector<int> res;
    while( heap.size())
    {
        res.push_back(heap.top());
        heap.pop();
    }
    reverse( res.begin(),res.end());
    for( auto e : res  )
    {
        printf("%d ",e );
    }
    return 0;
}
相关推荐
music&movie2 分钟前
算法工程师认知水平要求总结
人工智能·算法
laocui11 小时前
Σ∆ 数字滤波
人工智能·算法
yzx9910131 小时前
Linux 系统中的算法技巧与性能优化
linux·算法·性能优化
全栈凯哥2 小时前
Java详解LeetCode 热题 100(26):LeetCode 142. 环形链表 II(Linked List Cycle II)详解
java·算法·leetcode·链表
全栈凯哥2 小时前
Java详解LeetCode 热题 100(27):LeetCode 21. 合并两个有序链表(Merge Two Sorted Lists)详解
java·算法·leetcode·链表
SuperCandyXu2 小时前
leetcode2368. 受限条件下可到达节点的数目-medium
数据结构·c++·算法·leetcode
Humbunklung2 小时前
机器学习算法分类
算法·机器学习·分类
Ai多利2 小时前
深度学习登上Nature子刊!特征选择创新思路
人工智能·算法·计算机视觉·多模态·特征选择
蒟蒻小袁3 小时前
力扣面试150题--被围绕的区域
leetcode·面试·深度优先
Q8137574603 小时前
中阳视角下的资产配置趋势分析与算法支持
算法