이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
// cre: Nguyen Ngoc Hung - Train VOI 2024
#include<bits/stdc++.h>
using namespace std;
#define __nnhzzz__ signed main()
#define BIT(i,j) ((i>>j)&1LL)
#define MASK(i) (1LL<<i)
#define ALL(x) (x).begin(),(x).end()
#define SZ(x) (int)(x).size()
#define fi first
#define se second
#define ll long long
#define ull unsigned long long
#define ld long double
#define vi vector<int>
#define vvi vector<vi>
#define vvvi vector<vvi>
#define pii pair<int,int>
#define vpii vector<pii>
#define REP(i,a,b) for(int i = (a); i<=(b); ++i)
#define REPD(i,a,b) for(int i = (a); i>=(b); --i)
#define REPDIS(i,a,b,c) for(int i = (a); i<=(b); i+=c)
#define int ll
//-------------------------------------------------------------//
const int oo = 1e9,LOG = 20,MAXN = 4e5+7,N = 1e2+3;
const int MOD = 1e9+7,MOD1 = 1e9+207,MOD2 = 1e9+407,MOD3 = 998244353;
//-------------------------------------------------------------//
template<typename T1, typename T2> bool mini(T1 &a, T2 b){if(a>b){a=b;return true;}return false;}
template<typename T1, typename T2> bool maxi(T1 &a, T2 b){if(a<b){a=b;return true;}return false;}
/*
----------------------------------------------------------------
END OF TEMPLATE
----------------------------------------------------------------
Nguyen Ngoc Hung - nnhzzz
Training for VOI24 gold medal
----------------------------------------------------------------
*/
int dp[MAXN][3],a[MAXN];
int n,k;
class fenwick_tree{
private:
int f[MAXN];
public:
void update(int id, int val){
for(; id<MAXN; id += id&-id){
maxi(f[id],val);
}
}
int get(int id){
int res = 0;
for(; id>0; id-=id&-id){
maxi(res,f[id]);
}
return res;
}
fenwick_tree(int n = 0){
memset(f,0,sizeof f);
}
};
void sub4(){
vi cur;
REP(i,1,n){
cur.push_back(a[i]);
}
sort(ALL(cur)); cur.resize(unique(ALL(cur))-cur.begin());
fenwick_tree fen;
REP(i,1,n){
a[i] = lower_bound(ALL(cur),a[i])-cur.begin()+1;
int val = fen.get(a[i]-1);
fen.update(a[i],val+1);
}
cout << fen.get(n);
}
void sub3(){
int res = 0;
REP(i,1,n){
REP(j,1,i-1){
if(a[i]>a[j]) maxi(dp[i][0],dp[j][0]);
if(a[i]>a[j] || abs(a[i]-a[j])<k){
maxi(dp[i][1],dp[j][0]);
}
if(a[i]>a[j]) maxi(dp[i][2],max(dp[j][0],max(dp[j][1],dp[j][2])));
}
++dp[i][0];
++dp[i][1];
++dp[i][2];
maxi(res,max(dp[i][0],max(dp[i][1],dp[i][2])));
}
cout << res;
}
void sub7(){
int res = 0;
vi cur;
REP(i,1,n){
cur.push_back(a[i]-1);
cur.push_back(a[i]);
cur.push_back(a[i]+k);
cur.push_back(a[i]+k-1);
}
sort(ALL(cur)); cur.resize(unique(ALL(cur))-cur.begin());
// REP(i,1,n) a[i] = lower_bound(ALL(cur),a[i])-cur.begin()+1;
fenwick_tree fen1,fen2,fen3;
REP(i,1,n){
int tmp = lower_bound(ALL(cur),a[i]+k)-cur.begin()+1;
int ai = lower_bound(ALL(cur),a[i])-cur.begin()+1;
dp[i][0] = fen1.get(ai-1);
dp[i][1] = fen1.get(tmp-1);
dp[i][2] = max(dp[i][0],max(fen2.get(ai-1),fen3.get(a[i]-1)));
++dp[i][0]; ++dp[i][1]; ++dp[i][2];
fen1.update(ai,dp[i][0]);
fen2.update(ai,dp[i][1]);
fen3.update(ai,dp[i][2]);
maxi(res,max(dp[i][0],max(dp[i][1],dp[i][2])));
}
cout << res;
}
void solve(){
cin >> n >> k;
REP(i,1,n){
cin >> a[i];
}
if(k==0){
sub4(); return ;
}
// sub3();
sub7();
}
__nnhzzz__{
ios::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
#define name "test"
if(fopen(name".inp","r")){
freopen(name".inp","r",stdin);
freopen(name".out","w",stdout);
}
#define name1 "nnhzzz"
if(fopen(name1".inp","r")){
freopen(name1".inp","r",stdin);
freopen(name1".out","w",stdout);
}
int test = 1;
while(test--){
solve();
}
cerr << "\nTime elapsed: " << 1000*clock()/CLOCKS_PER_SEC << "ms\n";
return 0;
}
컴파일 시 표준 에러 (stderr) 메시지
glo.cpp: In function 'int main()':
glo.cpp:145:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
145 | freopen(name".inp","r",stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
glo.cpp:146:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
146 | freopen(name".out","w",stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
glo.cpp:150:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
150 | freopen(name1".inp","r",stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
glo.cpp:151:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
151 | freopen(name1".out","w",stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
# | 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... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |