当前位置:网站首页>ACM. Hj24 chorus ●●
ACM. Hj24 chorus ●●
2022-06-22 21:29:00 【chenyfan_】
HJ24 chorus ●●
describe
N Three students stand in a row , The music teacher should ask the least students to stand out , Make the rest K Three students form a chorus .
set up K The students are numbered from left to right 1,2…,K , Their heights are T k T_k Tk, Can find a classmate , His Both sides Of the students are in order of height Strictly reduce The formation of is the chorus formation .
Example :
123 124 125 123 121 It's a chorus formation
123 123 124 122 Not a chorus formation , Because the first two students are equal in height , Unqualified
123 122 121 122 Not a chorus formation , Because I can't find a classmate , His classmates on both sides are decreasing in height .
Your task is , All known N The height of a classmate , At least a few students are required for calculation , It can make the rest of the students form a chorus .
Be careful : Changing the order of queue elements is not allowed And Does not require The number of top students must be equal
Data range : 1 ≤ n ≤ 3000 1 \le n \le 3000 1≤n≤3000
Input description :
Use case two lines of data , The first line is the total number of students N , The second line is N The height of a classmate , Space off
Output description :
At least a few students are required to stand out
Example
Input :
8
186 186 150 200 160 130 197 200
Output :
4
explain :
Since it is not allowed to change the order of queue elements , So the final remaining queue should be 186 200 160 130 or 150 200 160 130
Answer key
1. Dynamic programming ( The longest increasing subsequence )
In order to get the minimum number of people out of the queue , So we can calculate that when we take a certain height as the midpoint , The maximum number of eligible people on both sides of him and , The final result is the total number of people minus the number of chorus lines .
therefore , We can use two arrays left、right, Record a certain height as the end point 、 The longest increment at the starting point / Decreasing subsequence sum .
such , Then the problem can be transformed into 300. The longest increasing subsequence ●● A similar situation .
- Time complexity : O ( n 2 ) O(n^2) O(n2), The array needs to be traversed multiple times , Two layers of for A nested loop .
- Spatial complexity : O ( n ) O(n) O(n), Save in every location dp Increment and decrement arrays .
#include <iostream>
#include <vector>
using namespace std;
int main(){
int n;
cin >> n;
vector<int> statures(n, 0);
for(int i = 0; i < n; ++i){
cin >> statures[i];
}
vector<int> left(n, 0);
vector<int> right(n, 0);
for(int i = 0; i < n; ++i){
for(int j = i-1; j >= 0; --j){
if(statures[i] > statures[j]){
left[i] = max(left[i], left[j] + 1); // staturs[i] The longest increasing subsequence before
}
}
}
for(int i = n-1; i >= 0; --i){
for(int j = i+1; j < n; ++j){
if(statures[i] > statures[j]){
right[i] = max(right[i], right[j] + 1); // staturs[i] The longest decreasing subsequence after
}
}
}
int ans = 0;
for(int i = 0; i < n; ++i){
// Calculation and comparison results
ans = max(ans, (left[i]+right[i]+1) * (left[i]>0 && right[i]>0));
}
cout << n-ans;
return 0;
}
2. Dynamic programming + Dichotomy
Use a dynamic array to record the current sequence , And use the dichotomy to insert and sort , Get the length of the longest subsequence , Specific view 300. The longest increasing subsequence ●●
- Time complexity : O ( n l o g 2 ( n ) ) O(nlog_2(n)) O(nlog2(n)), With the help of dichotomy .
- Spatial complexity : O ( n ) O(n) O(n), An array of left and right subsequence values .
#include <iostream>
#include <vector>
using namespace std;
int main(){
int n;
cin >> n;
vector<int> statures(n, 0);
for(int i = 0; i < n; ++i){
cin >> statures[i];
}
vector<int> left(n, 1); // left Express staturs[i] The length of the longest increasing subsequence at the end
vector<int> dpl;
dpl.emplace_back(statures[0]); // Dynamic programming array
for(int i = 1; i < n; ++i){
if(statures[i] > dpl[dpl.size()-1]){
dpl.emplace_back(statures[i]);
left[i] = dpl.size();
continue;
}
int l = 0, r = dpl.size()-1;
while(l <= r){
int mid = (l+r) >> 1;
if(dpl[mid] >= statures[i]){
r = mid - 1;
}else{
l = mid + 1;
}
}
dpl[l] = statures[i]; // Replace position
left[i] = l + 1; // Update here staturs[i] Corresponding subsequence length , namely l+1
}
vector<int> right(n, 1); // right Express staturs[i] The longest decreasing subsequence length at the beginning
vector<int> dpr;
dpr.emplace_back(statures[n-1]); // Dynamic programming array
for(int i = n-2; i >= 0; --i){
if(statures[i] > dpr[dpr.size()-1]){
dpr.emplace_back(statures[i]);
right[i] = dpr.size();
continue;
}
int l = 0, r = dpr.size()-1;
while(l <= r){
int mid = (l+r) >> 1;
if(dpr[mid] >= statures[i]){
r = mid - 1;
}else{
l = mid + 1;
}
}
dpr[l] = statures[i];
right[i] = l + 1; // Update here staturs[i] Corresponding subsequence length , namely l+1
}
int ans = 0;
for(int i = 0; i < n; ++i){
// The queue length and when all heights are midpoint
ans = max(ans, (left[i]+right[i]-1) * (left[i]>0 && right[i]>0));
}
cout << n-ans; // The number of people listed
return 0;
}
边栏推荐
- 快速排序模板 & 注意事项
- 大势智慧创建倾斜模型和切割单体化
- [513. find the value in the lower left corner of the tree]
- 2022年起重机械指挥考试模拟100题及模拟考试
- [redis]redis6 master-slave replication
- 为了不曾忘却的纪念:孙剑专题
- 53 page intelligent campus intelligent system design scheme (download attached)
- 杰理之MUSIC 模式获取播放文件的目录【篇】
- [138. copy linked list with random pointer]
- 第025讲:字典:当索引不好用时 | 课后测试题及答案
猜你喜欢
![DACL output on Jerry's hardware, DAC output sound of left and right channels [chapter]](/img/8a/ce164a5538bd8edf10eba5e4e8abe6.png)
DACL output on Jerry's hardware, DAC output sound of left and right channels [chapter]

第029讲:文件:一个任务 | 课后测试题及答案

Cannot re-register id: PommeFFACompetition-v0问题解决

软考必备资料大放送,全科目软考资料都给你备好了!

杰理之外挂 4M 的 flash 在 PC 上查看大小只有 1M 的处理方法【篇】

53页智慧校园智能化系统设计方案(附下载)

2022危险化学品经营单位主要负责人上岗证题库及模拟考试

Arcgis中las点云数据抽稀

【876. 链表的中间结点】
![[876. intermediate node of linked list]](/img/c8/463d150bc6c88cfb57e94795957b0e.png)
[876. intermediate node of linked list]
随机推荐
513. find the value in the lower left corner of the tree / Sword finger offer II 091 Paint the house
软考必备资料大放送,全科目软考资料都给你备好了!
牛客 52次月赛 B牛牛的身高 (思维题 模拟题)
第023、024讲:递归:这帮小兔崽子、汉诺塔 | 课后测试题及答案
2022 a special equipment related management (elevator) examination questions and simulation examination
redis学习笔记
杰理之动态切换 EQ 文件【篇】
85- have you learned any of these SQL tuning tips?
第026讲:字典:当索引不好用时2 | 课后测试题及答案
[redis] profile
[redis]集群与常见错误
The second harmonyos application development training
第025讲:字典:当索引不好用时 | 课后测试题及答案
MySQL adds (appends) prefix and suffix to a column field
Learning websites that programmers must see
Flutter System Architecture(Flutter系统架构图)
Fluent system architecture
[876. intermediate node of linked list]
[the penultimate node in the linked list]
安卓kotlin sp dp转px