当前位置:网站首页>8 find subsequences with a maximum length of K
8 find subsequences with a maximum length of K
2022-07-27 11:09:00 【DHU yanghualin (UV allergy)】
8 Find and the maximum length is K The subsequence
author : Turbo The time limit : 1S chapter : curriculum design
Problem description :
Give you an array of integers nums And an integer k . You need to find nums The middle length is k The subsequence , And this subsequence and Maximum .
Please return arbitrarily A length of k Integer subsequence of .
Subsequence Defined as deleting some elements from an array , The array obtained without changing the order of the remaining elements .
Example 1:
Input :
4
2 1 3 3
2
Output :
3 3
explain :
Subsequences have maximum sum :3 + 3 = 6 .
Example 2:
Input :
4
-1 -2 3 4
3
Output :
-1 3 4
explain :
Subsequences have maximum sum :-1 + 3 + 4 = 6 .
Example 3:
Input :
4
3 4 3 3
2
Output :
3 4
explain :
Subsequences have maximum sum :3 + 4 = 7 .
Another feasible subsequence is [4, 3] .
Enter description :
Enter three lines :
The first line is an integer n Representative array nums The length of .
Second line input n An integer represents an array nums The elements of .
The third is an integer k.
Tips :
1 <= n <= 1000
-10^5 <= nums[i] <= 10^5
1 <= k <= n
Output one line k An integer represents the result , Integers are separated by spaces .
Input example :
5
-3 4 4 2 1
2
----------
The output shows that :
4 4
-------------------
The structure contains its data And his array number
Sort the data first Get the biggest ones
Then for these largest structures Sort the array number Array small front row then for Before cyclic output n Just one
#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;
}边栏推荐
- 最短移动距离和形态复合体的熵
- NFT leaderboard -nft real offer latest address: NFT leaderboard.com
- Redis high availability principle
- Redis+caffeine two-level cache enables smooth access speed
- No identifier specified for entity solution
- 正则form表单判断
- Analysis of new communication security risks brought by quantum computer and Countermeasures
- Tree data conversion
- Analysis of C language pointer function and function pointer
- Asustek unparalleled, this may be the best affordable high brush thin notebook on the screen
猜你喜欢

MySQL index, transaction and storage engine

antd table中排序th阻止悬停变色+table悬停行变色+table表头变色

Non progressive phenomena of entropy and morphology

熵与形态的非递进现象

Overview of user space lock on mobile platform

Codeforces Round #807 (Div 2.) AB

Customized modification based on jira7.9.2

推导重叠积分的详细展开式 STO overlap integrals

Tcp/ip protocol

How to create a.Net image with diagnostic tools
随机推荐
发动机悬置系统冲击仿真-瞬时模态动态分析与响应谱分析
Awesome! VMware esxi installation record, with download
Tdengine helps Siemens' lightweight digital solution simicas simplify data processing process
SQL Server2000 database error
十年架构五年生活-07 年轻气盛的蜕变
tf.AUTO_ Function of reuse
华硕无双,这可能是屏幕最好的平价高刷轻薄笔记本
Why is the data service API the standard configuration of the data midrange when we take the last mile of the data midrange?
MIMO array 3D imaging technology based on mobile terminal
Learning notes - simple server implementation
img src为空或者src不存在,图片出现白色边框
SQL Server2000数据库错误
Use kaggle to run Li Hongyi's machine learning homework
Real time development platform construction practice, in-depth release of real-time data value - 04 live broadcast review
Overview of data security in fog computing
DNS principle and resolution process
[FPGA tutorial case 40] communication case 10 -- Verilog implementation of a simple OFDM system based on FPGA
Analysis of C language pointer function and function pointer
Recruit top talents! The "megeagle creator program" of Kuangshi technology was officially launched
C语言 2:求三数字最大值,求三数字中间值,编写程序步骤