• 6155. 找出数组的第 K 大和(力扣周赛)(思维转换 + 堆)


    题目链接

    虽然一眼知道是堆维护,但是还是不会写…

    分析

    首先第一大的子序列和就是原数组中所有的正数的和sum;
    然后找第k大子序列和,考虑让sum减小;相当于就是sum减去原序列的一个子序列和;
    那么 第k大子序列和 = sum - 第k-1小的子序列和;

    下面考虑一下数组中的负数:
    要让sum减小,如果是一个正数,那么我们sum就减去它,负数的话就加上它;
    可以发现正负数对sum的本质作用是一样的,因此我们可以把负数加上绝对值变成正数;
    这样统一考虑让sum减去就行了;

    到这里我们的问题就转换成:给一个数组,找第k-1小的子序列和;
    很显然有2^n-1个子序列,也就是有这么多个和,暴力不可取;
    考虑下面这个数组[3,4,5](排好序的)
    显然第1小的子序列和为3;考虑通过3去扩展大一点的序列和;
    3的话就可以扩展出3+4-3 = 4(3不选了),3+4(选3);
    再考虑通过4,3+4扩展序列和:4+5-4,4+5,(3+4)+5-4,(3+4)+5;
    这样就能扩展出所有的子序列和。为什么?证明可以看一下b站《灵茶山艾府》的视频;

    因此我们需要维护三个东西,当前子序列和,当前到第几个下标,当前要转移到哪个下标

    我们可以用一个堆来维护,每次取出堆中最小的数;
    取到第k-1个,就是第k-1小的数了,这样就找到了第k-1小的子序列和;

    具体可以看代码

    代码

    #include<iostream>
    #include<queue>
    #include<cstring>
    #include<vector>
    #include<stdio.h>
    #include<map>
    #include<algorithm>
    #include<deque>
    #include<stack>
    #include<set>
    #include <random>
    #include<bitset>
    // #include 
    #include<math.h>
    #include<string.h>
    #define IOS ios::sync_with_stdio(false),cin.tie(0);
    using namespace std;
     
    #define pb push_back
    #define coutl cout<<"------------"<<endl;
    #define fi first
    #define se second
    
    #define ire(x) scanf("%d",&x)
    #define iire(a,b) scanf("%d %d",&a,&b)
    #define lre(x) scanf("%lld",&x)
    #define llre(a,b) scanf("%lld %lld",&a,&b)
    #define dre(x) scanf("%lf",&x)
    #define ddre(a,b) scanf("%lf %lf",&a,&b)
    #define rep(i,a,b) for(int i=a;i<=b;i++)
    #define endl "\n"
    #define PI acos(-1.0)
    // #define int long long
    // #define double long double
    // typedef __int128 ll;
    typedef long long ll;
    typedef unsigned long long ull;
    
    typedef pair<int, int> PII;
    typedef pair<double, int> PDI;
    typedef pair<ll, ll> PLL;
    typedef pair<double, double> PDD;
    typedef pair<double, pair<int, double> > PDID;
    typedef pair<char, char> PCC;
    typedef pair<char, pair<int, int> > PCII;
    typedef pair<int, pair<int, int> > PIII;
    typedef pair<ll, pair<ll, ll> > PLLL;
    typedef pair<int, pair<int, pair<int, int> > > PIIII;
    typedef pair<ll, pair<int, int> > PLII;
    
    const int maxn = 1e5 + 7;
    const int N = 405;
    const int M = 4e6 + 7;
    const int mod = 998244353;
    const int inv = mod - mod/2;
    const int inf = 0x3f3f3f3f;
    const ll INF = 0x3f3f3f3f3f3f3f3f;
    const double pi = acos(-1);
    const double eps = 1e-8;
      
    ll gcd(ll a,ll b) {return b==0 ? a : gcd(b,a%b);}
    ll lcm(ll a,ll b) {return a*b / gcd(a,b);}
    ll qmi(ll a,ll b,ll p) {ll ans = 1; while(b) { if(b & 1) ans = ans * a % p; a = a * a % p; b >>= 1; } return ans;}
    int lowbit(int x) {return x & (-x);}
    
    class Solution {
    public:
        long long kSum(vector<int>& nums, int k) {
            
            ll sum = 0;
            for(auto &x : nums)
            	if(x > 0) sum += x;
    			else x = -x;
            
            if(k-1 == 0) return sum;
            
    		//找数组中第k-1小的子序列的和
    		sort(nums.begin(),nums.end());
            
            for(auto x : nums) cout<<x<<' ';
            cout<<'\n';
            
    		priority_queue<PLLL,vector<PLLL>,greater<PLLL>> q;
    		q.push({nums[0],{0,1}});	//当前的值,当前下标,下一个要转移的下标
    		
    		ll ans = 0;
    		
    		int cnt = 0;	//当前第几个出队
    		while(q.size())
    		{
    			PLLL p = q.top();
    			q.pop();
    			
    			cnt ++;
    			ll v = p.first;
    			ll idx1 = p.second.fi;
                ll idx2 = p.se.se;
    			
    			if(cnt == k - 1)	//第k-1小的数
    			{
    				ans = v;
    				break;
    			}
    			
    			if(idx2 < nums.size())
    			{
    				//不选前面的
    				q.push({v+nums[idx2]-nums[idx1],{idx2,idx2+1}});
    				
    				//选
    				q.push({v + nums[idx2],{idx2,idx2+1}});
    			}
    		}
    		
            return sum - ans;
        }
    };
    
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
  • 相关阅读:
    总结tab栏切换实现的方法,以及增加滚动实现tab栏切换的效果
    R Markdown 格式
    PW2330原厂规格书12V转5V,3.3V稳压电源芯片,1A-3安
    企业电子招标采购系统项目说明+开发类型+解决方案+功能描述
    虚拟机改IP地址
    苹果汽车项目的败局:起步失误与方向迷茫
    2022年PMP考试换大纲了,但是PMBOK还没更新,该如何准备?
    前端报表如何实现无预览打印解决方案或静默打印
    50道软件测试面试题
    从零开始 DIY 智能家居 - AC791N通过单线SPI驱动WS2812
  • 原文地址:https://blog.csdn.net/qq_53398102/article/details/126462751