最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

【今日三题】压缩字符串(模拟)chika和蜜柑(topK)01背包

网站源码admin4浏览0评论

【今日三题】压缩字符串(模拟) / chika和蜜柑(topK) / 01背包

压缩字符串 (模拟)
  • 压缩字符串
代码语言:javascript代码运行次数:0运行复制
class Solution {
public:
    string compressString(string param) {
        string ret;
        int n = param.size();
        for (int l = 0; l < n; l++)
        {
            char ch = param[l];
            ret += ch;
            int r = l;
            while (r + 1 < n && param[r] == param[r + 1]) r++;
            if (r - l + 1 > 1) ret += to_string(r - l + 1);
            l = r;
        }
        return ret;
    }
};

chika和蜜柑 (topK)

  • chika和蜜柑

先选最大的前k个,如果有相等的再判断酸度是否更小。

代码语言:javascript代码运行次数:0运行复制
#include <iostream>
#include <vector>
#include <queue>
#include <algorithm>
using namespace std;

using ll = long long;
const int N = 2e5 + 10;
int n, k;
int a[N], b[N];
priority_queue<pair<int, int>> pq;
ll suma, sumb;

int main()
{
    cin >> n >> k;
    for (int i = 1; i <= n; i++) cin >> a[i];
    for (int i = 1; i <= n; i++) 
    {
        int b; cin >> b;
        pq.push({b, i});
    }
    int tmp = 0, id = 0;
    for (int i = 1; i <= k; i++) 
    {
        tmp = pq.top().first;
        id = pq.top().second;
        sumb += tmp;
        pq.pop();
        suma += a[id];
    }
    while (pq.size() && tmp == pq.top().first)
    {
        int i = pq.top().second;
        if (a[id] > a[i]) 
            suma -= a[id] - a[i];
        pq.pop();
    } 
    cout << suma << " " << sumb << endl;
    return 0;
}

借助优先级队列,重写排序规则。

代码语言:javascript代码运行次数:0运行复制
#include <iostream>
#include <queue>
using namespace std;

using pii = pair<int, int>;

struct cmp{
    bool operator()(const pii& p1, const pii& p2)
    {
        if (p1.first == p2.first) return p1.second > p2.second;
        else return p1.first < p2.first;
    }
};

const int N = 2e5 + 10;
using ll = long long;
priority_queue<pii, vector<pii>, cmp> pq;
int n, k;
int a[N], b[N];
ll suma, sumb;

int main()
{
    cin >> n >> k;
    for (int i = 0; i < n; i++) cin >> a[i];
    for (int i = 0; i < n; i++) cin >> b[i];
    for (int i = 0; i < n; i++)
        pq.push({b[i], a[i]});
    while (k--)
    {
        sumb += pq.top().first;
        suma += pq.top().second;
        pq.pop();
    }
    cout << suma << " " << sumb << endl;
    return 0;
}

直接用数组存储pair<int, int>类型,分别为甜度和酸度,再对数组重写排序规则。

代码语言:javascript代码运行次数:0运行复制
#include <iostream>
#include <algorithm>
using namespace std;

using pii = pair<int, int>;
const int N = 2e5 + 10;
pii arr[N];
int n, k;

int main()
{
    cin >> n >> k;
    for (int i = 0; i < n; i++) cin >> arr[i].first;
    for (int i = 0; i < n; i++) cin >> arr[i].second;
    sort(arr, arr + n, [&](const pii& p1, const pii& p2){
        if (p1.second == p2.second) return p1.first < p2.first;
        else return p1.second > p2.second;
    });
    long s = 0, t = 0;
    for (int i = 0; i < k; i++)
    {
        s += arr[i].second;
        t += arr[i].first;
    }
    cout << t << " " << s << endl;
    return 0;
}

01背包

  • 01背包

经典01背包,已无需多言。

代码语言:javascript代码运行次数:0运行复制
class Solution {
public:
    int knapsack(int V, int n, vector<vector<int> >& vw) {
        vector<int> dp(V + 1);
        for (int i = 0; i < n; i++)
            for (int j = V; j >= vw[i][0]; j--)
                dp[j] = max(dp[j], dp[j - vw[i][0]] + vw[i][1]);
        return dp[V];
    }
};
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。 原始发表:2025-04-12,如有侵权请联系 cloudcommunity@tencent 删除数组压缩字符串int排序
发布评论

评论列表(0)

  1. 暂无评论