admin管理员组文章数量:1031258
【今日三题】压缩字符串(模拟) / chika和蜜柑(topK) / 01背包
- 压缩字符串
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和蜜柑
代码语言:javascript代码运行次数:0运行复制先选最大的前k个,如果有相等的再判断酸度是否更小。
#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;
}
代码语言:javascript代码运行次数:0运行复制直接用数组存储pair<int, int>类型,分别为甜度和酸度,再对数组重写排序规则。
#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背包
代码语言:javascript代码运行次数:0运行复制经典01背包,已无需多言。
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排序【今日三题】压缩字符串(模拟) / chika和蜜柑(topK) / 01背包
- 压缩字符串
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和蜜柑
代码语言:javascript代码运行次数:0运行复制先选最大的前k个,如果有相等的再判断酸度是否更小。
#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;
}
代码语言:javascript代码运行次数:0运行复制直接用数组存储pair<int, int>类型,分别为甜度和酸度,再对数组重写排序规则。
#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背包
代码语言:javascript代码运行次数:0运行复制经典01背包,已无需多言。
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排序本文标签: 今日三题压缩字符串(模拟)chika和蜜柑(topK)01背包
版权声明:本文标题:【今日三题】压缩字符串(模拟)chika和蜜柑(topK)01背包 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/jiaocheng/1747718869a2208428.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论