Dãy con tăng dài nhất (bản dễ)
View as PDF
Submit solution
Points:
0.04
Time limit:
1.0s
Memory limit:
512M
Input:
stdin
Output:
stdout
Problem source:
Problem type
Allowed languages
C, C++, Go, Java, Kotlin, Pascal, PyPy, Python, Rust, Scratch
Cho một dãy số nguyên gồm ~N~ phần tử ~A_1, A_2, \dots,A_N~.
Biết rằng dãy con tăng đơn điệu là ~1~ dãy ~A_{i_{1}}, \dots, A_{i_{k}}~ thỏa mãn ~i_{1} < i_{2} < \dots < i_{k}~ và ~A_{i_{1}} < A_{i_{2}} < \dots < A_{i_{k}}~. Hãy cho biết dãy con tăng đơn điệu dài nhất của dãy này có bao nhiêu phần tử?
Input
- Dòng ~1~ gồm ~1~ số nguyên là số ~N (1 \le N \le 1000)~.
- Dòng thứ ~2~ ghi ~N~ số nguyên ~A_1, A_2, \dots, A_N (1 \le A_i \le 10000)~.
Output
Ghi ra độ dài của dãy con tăng đơn điệu dài nhất.
Sample Input
6
1 2 5 4 6 2
Sample Output
4
Note
Dãy con dài nhất là dãy ~A_1 = 1 < A_2 = 2 < A_4 = 4 < A_5 = 6~, độ dài dãy này là ~4~.
Download test và solution tại đây.
Comments
include <bits/stdc++.h>
//#define FOR(i, a, b) for (int i = a; i < b; i++);
using namespace std;
int main() { int N; cin >> N; vector <int> A(N); for (int i = 0; i < N; i++) cin >> A[i]; vector <int> L(N, 1); for (int i = 0; i < N; i++) for (int j = 0; j < i; j++) if (A[i] > A[j]) L[i] = max(L[i], L[j] + 1); cout << *max_element(L.begin(), L.end()); return 0; }
include<bits/stdc++.h>
using namespace std;
define vec vector
define ps push_back
int main(){ iosbase::syncwithstdio(0); cin.tie(0);cout.tie(0); // freopen("LIS.INP","r",stdin); // freopen("LIS.OUT","w",stdout); int n;cin>>n; vec<int>a(n); for(int&x:a)cin>>x; vec<int>b; for(auto&x:a){ auto it=lowerbound(b.begin(),b.end(),x); if(it==b.end())b.ps(x); else *it=x; } cout<<b.size(); }
ý là phiên bản tối ưu gọn hơn á
This comment is hidden due to too much negative feedback. Show it anyway.
This comment is hidden due to too much negative feedback. Show it anyway.
This comment is hidden due to too much negative feedback. Show it anyway.
This comment is hidden due to too much negative feedback. Show it anyway.
chatgpt ?
100%
This comment is hidden due to too much negative feedback. Show it anyway.
This comment is hidden due to too much negative feedback. Show it anyway.
This comment is hidden due to too much negative feedback. Show it anyway.
This comment is hidden due to too much negative feedback. Show it anyway.
This comment is hidden due to too much negative feedback. Show it anyway.
This comment is hidden due to too much negative feedback. Show it anyway.
This comment is hidden due to too much negative feedback. Show it anyway.
This comment is hidden due to too much negative feedback. Show it anyway.
This comment is hidden due to too much negative feedback. Show it anyway.
This comment is hidden due to too much negative feedback. Show it anyway.
This comment is hidden due to too much negative feedback. Show it anyway.
This comment is hidden due to too much negative feedback. Show it anyway.
This comment is hidden due to too much negative feedback. Show it anyway.
This comment is hidden due to too much negative feedback. Show it anyway.
This comment is hidden due to too much negative feedback. Show it anyway.
This comment is hidden due to too much negative feedback. Show it anyway.
This comment is hidden due to too much negative feedback. Show it anyway.
This comment is hidden due to too much negative feedback. Show it anyway.
This comment is hidden due to too much negative feedback. Show it anyway.
This comment is hidden due to too much negative feedback. Show it anyway.
This comment is hidden due to too much negative feedback. Show it anyway.
This comment is hidden due to too much negative feedback. Show it anyway.
This comment is hidden due to too much negative feedback. Show it anyway.