이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
# include <bits/stdc++.h>
#define f first
#define s second
#define pb push_back
#define pii pair <int, int>
using namespace std;
const int N = 3e5 + 5;
int t,n,a[N],p[N],pas,ans,d,par,tree[4*N],idx,idx1,val;
multiset <int> ms,ms1;
multiset <int> :: iterator it;
vector <pii > v;
int get_col(int a) {
if (a == p[a]) return a;
else return p[a] = get_col(p[a]);
}
void col(int a, int b) {
a = get_col(a);
b = get_col(b);
if (a == b) return ;
if (a > b) swap(a,b);
p[b] = a;
}
void update(int node, int le, int ri, int idx, int val) {
if (le > idx || ri < idx) return ;
if (le == ri) {
tree[node] = val;
return ;
}
int mid = (le + ri) / 2;
update(2*node,le,mid,idx,val);
update(2*node + 1, mid + 1, ri, idx, val);
tree[node] = max(tree[2*node], tree[2 * node + 1]);
}
int getans(int node, int le, int ri, int start, int end) {
if (le > end || ri < start) return 0;
if (le >= start && ri <= end)
return tree[node];
int mid = (le + ri) / 2;
return max(getans(2*node,le,mid,start,end), getans(2*node + 1, mid + 1, ri, start,end));
}
main() {
std::ios_base::sync_with_stdio(false),cin.tie(0),cout.tie(0);
cin>>n>>d;
for(int i = 1; i <= n; i++) {
cin>>a[i];
v.pb({a[i],-i});
}
sort(v.begin(),v.end());
for (int i = 0; i < v.size(); i++){
a[-v[i].s] = i + 1;
}
v.clear();
for (int i = 1; i <= n; i++) {
v.pb({a[i],i});
}
sort(v.begin(),v.end());
for (int i = 1; i <= n; i++) {
p[i] = i;
}
for (int i = 0; i < v.size(); i++) {
val = v[i].f; idx = v[i].s;
it = ms.upper_bound(idx);
if (it != ms.end()) {
idx1 = *it;
if (idx1 - d <= idx) col(idx,idx1);
}
it = ms1.upper_bound(-idx);
if (it != ms1.end()) {
idx1 = -(*it);
if (idx - d <= idx1) col(idx1,idx);
}
par = get_col(idx);
pas = getans(1,1,n,par,idx - 1) + 1;
update(1,1,n,idx,pas);
ans = max(ans,pas);
ms.insert(idx); ms1.insert(-idx);
}
cout<<ans<<"\n";
}
컴파일 시 표준 에러 (stderr) 메시지
Main.cpp:41:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
41 | main() {
| ^~~~
Main.cpp: In function 'int main()':
Main.cpp:49:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
49 | for (int i = 0; i < v.size(); i++){
| ~~^~~~~~~~~~
Main.cpp:60:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
60 | for (int i = 0; i < v.size(); i++) {
| ~~^~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |