제출 #975286

#제출 시각아이디문제언어결과실행 시간메모리
975286LCJLYFinancial Report (JOI21_financial)C++14
45 / 100
2430 ms69840 KiB
#include <bits/stdc++.h>
using namespace std;
 
#define int long long 
#define ld long double
#define show(x,y) cout << y << " " << #x << endl;
#define show2(x,y,i,j) cout << y << " " << #x << "  " << j << " " << #i << endl;
#define show3(x,y,i,j,p,q) cout << y << " " << #x << "  " << j << " " << #i << "  " << q << " " << #p << endl;
#define show4(x,y) for(auto it:y) cout << it << " "; cout << #x << endl;
typedef pair<long long,long long>pii;
typedef array<int,5>pi2;
mt19937_64 rng(chrono::system_clock::now().time_since_epoch().count());
 
int arr[300005];
int n,k;

inline pi2 combine(pi2 a, pi2 b){
	pi2 temp;
	temp[0]=max(a[0],b[0]);
	temp[1]=max({a[1],a[3]+b[1]});
	temp[2]=max({b[2],b[3]+a[2]});
	if(a[3]==0||b[3]==0) temp[3]=0;
	else temp[3]=a[3]+b[3];
	temp[4]=max({a[4],b[4],a[2]+b[1],a[1],b[2]});
	return temp;
}

struct node{
	int s,e,m;
	node *l,*r;
	pi2 v;
	
	node(int ss, int ee):s(ss),e(ee),m((s+e)>>1){
		v={0,0,0,0,0};
		if(s!=e){
			l=new node(s,m);
			r=new node(m+1,e);
		}
	}
	
	void upd(int x, int y){
		if(s==e){
			v[0]=y;
			v[1]=v[2]=v[3]=v[4]=1;
			return;
		}
		if(x<=m) l->upd(x,y);
		else r->upd(x,y);
		v=combine(l->v,r->v);
	}
	
	pi2 query(int x, int y){
		if(x<=s&&y>=e){
			return v;
		}
		if(y<=m) return l->query(x,y);
		if(x>m) return r->query(x,y);
		return combine(l->query(x,m),r->query(m+1,y));
	}
};

bool cmp(const pii a, const pii b){
	if(a.first==b.first){
		return a.second < b.second;
	}
	return a.first > b.first;
}
 
void solve(){
	cin >> n >> k;
	vector<pii>v;
	for(int x=1;x<=n;x++){
		cin >> arr[x];
		v.push_back({arr[x],x});
	}
	sort(v.begin(),v.end(),cmp);
	
	node st(0,n+5);
		
	int dp[n+5];
	memset(dp,0,sizeof(dp));
	int ans=0;
	for(int x=0;x<n;x++){
		int index=v[x].second;
		//show(index,index);
		int l=index+1;
		int r=n;
		int best=-1;
		int mid;
		while(l<=r){
			mid=(l+r)/2;
			pi2 hold=st.query(index,mid);
			if(hold[4]<=k){
				best=mid;
				l=mid+1;
			}
			else r=mid-1;
		}
		dp[index]=1;
		if(best!=-1){
			dp[index]=max(dp[index],st.query(index,best)[0]+1);
		}
		ans=max(ans,dp[index]);
		//show2(dp[index],dp[index],best,best);
		st.upd(index,dp[index]);
	}
	cout << ans;
}
 
int32_t main(){
	ios::sync_with_stdio(0);
	cin.tie(0);
	int t=1;
	//freopen("in.txt","r",stdin);
	//cin >> t;
	while(t--){
		solve();
	}
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...