当前位置:网站首页>8 找到和最大的长度为 K 的子序列
8 找到和最大的长度为 K 的子序列
2022-07-17 00:07:00 【DHU杨骅麟(紫外线过敏)】
8 找到和最大的长度为 K 的子序列
作者: Turbo时间限制: 1S章节: 课程设计
问题描述 :
给你一个整数数组 nums 和一个整数 k 。你需要找到 nums 中长度为 k 的子序列 ,且这个子序列的 和 最大 。
请你返回 任意 一个长度为 k 的整数子序列。
子序列 定义为从一个数组里删除一些元素后,不改变剩下元素的顺序得到的数组。
示例 1:
输入:
4
2 1 3 3
2
输出:
3 3
解释:
子序列有最大和:3 + 3 = 6 。
示例 2:
输入:
4
-1 -2 3 4
3
输出:
-1 3 4
解释:
子序列有最大和:-1 + 3 + 4 = 6 。
示例 3:
输入:
4
3 4 3 3
2
输出:
3 4
解释:
子序列有最大和:3 + 4 = 7 。
另一个可行的子序列为 [4, 3] 。
输入说明 :
输入三行:
第一行为一个整数n代表数组nums的长度。
第二行输入n个整数代表数组nums的元素。
第三行为一个整数k。
提示:
1 <= n <= 1000
-10^5 <= nums[i] <= 10^5
1 <= k <= n
输出一行k个整数表示结果,整数间用空格隔开。
输入范例 :
5
-3 4 4 2 1
2
----------
输出说明 :
4 4
-------------------
结构体里面包含他的数据 和他的数组编号
先对数据排序 得到最大的几个
然后对这几个最大的结构体 进行数组编号排序 数组小的排前面 然后for循环输出前n个就行了
#include<iostream>
#include<algorithm>
using namespace std;
struct student
{
int number;
int data;
};
bool cmp(student x, student y)
{
return x.data > y.data;
}
bool cmp2(student x, student y)
{
return x.number < y.number;
}
int main()
{
student arr[1000];
int n = 0;
cin >> n;
for (int i = 0; i < n; i++)
{
cin >> arr[i].data;
arr[i].number = i;
}
int k = 0;
cin >> k;
sort(arr, arr + n, cmp);
sort(arr, arr + k, cmp2);
for (int i = 0; i < k; i++)
{
cout << arr[i].data << " ";
}
return 0;
}边栏推荐
猜你喜欢

软件漏洞分析入门(一)

uni-app微信小程序——商城(3)——商城主页

Differences between let and const, let, const and VaR

iptables和snort基本配置

js获取一个文件名的后缀格式

elemtnui 表格如何修改某行文字颜色(elemtnui table 修改某行文字颜色)

Uni app wechat official account (4) - address management page

Text indent in uniapp doesn't work, and the indentation in the first line of uniapp doesn't work. How to solve it?

mock平台的使用说明

JS intercepts the first few digits of the string or the last few digits of the string
随机推荐
TCP与UDP,TCP服务器与客户端,UDP服务器与客户端
es可选链
JS intercepts the first few digits of the string or the last few digits of the string
Collection and summary of penetration test information
V-cloak and v-bind bind bind classes
uni-app微信小程序——商城(7)——商品详情
列表懒加载和图片懒加载
今天的码农女孩学习了关于事件操作和ref属性的笔记并做了表单双向绑定的练习
Uni app wechat applet - Mall (6) - my home page
08-BTC-分叉
Cento7安装mysql5.5以及升级5.7
小程序swiper高度
Use redis - Zset to make Leaderboard
uni-app微信公众号(5)——新增、修改地址
单页面应用 SPA 和多页面应用 MPA
Use leaflet to copy the original shentiwa Mega map to make a diary
Uniapp calls the map to query the location and mark the location
Deployment of cobalstrike (with resources)
let和const、let、const和var的区别
object-fit:cover; It doesn't work in the applet. How to deal with the deformation of the applet image