이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ull unsigned long long
#define pb(e) push_back(e)
#define sv(a) sort(a.begin(),a.end())
#define sa(a,n) sort(a,a+n)
#define mp(a,b) make_pair(a,b)
#define vf first
#define vs second
#define ar array
#define all(x) x.begin(),x.end()
const int inf = 0x3f3f3f3f;
const int mod = 1000000007;
const double PI=3.14159265358979323846264338327950288419716939937510582097494459230;
bool remender(ll a , ll b){return a%b;}
//freopen("problemname.in", "r", stdin);
//freopen("problemname.out", "w", stdout);
struct Seg {
int siz;
vector<int> v;
void init(int n){
siz = 1;
while(siz < n)siz*=2;
siz*=2;
v.assign(siz * 2 , 0);
}
void set(int i , int val , int x , int lx , int rx){
if(rx - lx == 1){
v[x] = val;
return;
}
int m = (lx + rx)/2;
if(i < m)set(i , val , 2 *x + 1 , lx , m);
else set(i , val , 2 * x + 2 , m , rx);
v[x] = max(v[2 * x + 1] , v[2 * x + 2]);
}
void set(int i , int val){
set(i , val , 0 , 0 , siz);
}
int mx(int l , int r , int x , int lx , int rx){
if(r <= lx || rx <= l)return 0;
if(lx >= l && rx <= r)return v[x];
int m = (lx + rx)/2;
return max(mx(l , r , 2 * x+ 1 , lx , m) , mx(l , r , 2 * x + 2 , m , rx));
}
int mx(int l , int r){
return mx(l , r , 0 , 0 , siz);
}
};
void solve(){
int n;
cin >> n;
int d;
cin >> d;
int arr[n];
for(int i = 0; i < n; i++)cin >> arr[i];
map<int,int> m;
for(int i = 0; i < n; i++){
m[arr[i]]++;
}
int cur = 0;
for(auto it : m){
m[it.vf] = cur;
cur++;
}
for(int i = 0; i < n; i++){
arr[i] = m[arr[i]];
}
set<pair<pair<int,int>,int>> s;
pair<int,int> dp[n];
Seg st;
st.init(n + 5);
int pos[n + 1];
memset(pos ,-1 , sizeof pos);
int pre[n + 1];
memset(pre,0,sizeof pre);
for(int i = 0; i < n; i++){
if(i - (d + 1) >= 0){
s.erase(mp(dp[i - (d + 1)] , i - (d + 1)));
if(pos[pre[i - (d + 1)]] == i - (d + 1)){
st.set(pre[i - (d + 1)] , 0);
}
if(pos[dp[i - (d + 1)].vs] == i - (d + 1)){
st.set(dp[i - (d + 1)].vs , 0);
}
}
dp[i] = mp(-1 , arr[i]);
if(arr[i]!=0){
int x = st.mx(0 , arr[i]);
dp[i] = mp(-(x+1) , arr[i]);
}
st.set(dp[i].vs , -dp[i].vf);
pre[i] = dp[i].vs;
pos[dp[i].vs] = i;
if(s.size()){
pair<int,int> p = (*s.begin()).vf;
if(p.vf < dp[i].vf){
dp[i] = p;
}
}
dp[i].vs = max(dp[i].vs , arr[i]);
s.insert(mp(dp[i],i));
st.set(dp[i].vs , -dp[i].vf);
pos[dp[i].vs] = i;
//cout << dp[i].vf << ' ' << dp[i].vs << '\n';
}
cout << -dp[n-1].vf << '\n';
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
//int t;cin >> t;while(t--)
solve();
return 0;
}
# | 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... |