답안 #244797

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
244797 2020-07-05T04:16:14 Z kshitij_sodani K개의 묶음 (IZhO14_blocks) C++17
0 / 100
1000 ms 384 KB
#include <bits/stdc++.h>
using namespace std;
typedef int64_t llo;
#define mp make_pair
#define pb push_back
#define a first 
#define b second
//#define endl '\n'

int n,k;

int dp[100001];
int dp2[100001];

int it[100001];
int tree[400001];
int pre[100001];
void build(int no,int l,int r){
	if(l==r){
		tree[no]=dp[l];
	}
	else{
		int mid=(l+r)/2;
		build(no*2+1,l,mid);
		build(no*2+2,mid+1,r);
		tree[no]=min(tree[no*2+1],tree[no*2+2]);
	}
}
int query(int no,int l,int r,int aa,int bb){
	if(r<aa or l>bb){
		return 1e9;
	}
	if(aa<=l and r<=bb){
		return tree[no];
	}
	else{
		int mid=(l+r)/2;
		return min(query(no*2+1,l,mid,aa,bb),query(no*2+2,mid+1,r,aa,bb));
	}
}
int main(){
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cin>>n>>k;
	vector<pair<int,int>> ac;
	for(int i=0;i<n;i++){
		cin>>it[i];
		while(ac.size()){
			if(ac.back().a<=it[i]){
				ac.pop_back();
			}
			else{
				break;
			}
		}
		if(ac.size()==0){
			pre[i]=-1;
		}
		else{
			pre[i]=ac.back().b;
			ac.pb({it[i],i});
		}
		for(int kk=pre[i]+1;kk<i;kk++){
			if(it[kk]>it[i]){
				while(true){
					continue;
				}
			}
		}

	//	cout<<pre[i]<<",";
	}
	//cout<<endl;


	int ma=0;
	for(int i=0;i<n;i++){
		ma=max(ma,it[i]);
		dp[i]=ma;
		dp2[i]=1e9;
	}


	for(int i=1;i<k;i++){
		build(0,0,n-1);
		for(int j=i;j<n;j++){

			if(pre[j]>=i){
				dp2[j]=dp2[pre[j]];
			}
			if(pre[j]==-1){
				dp2[j]=min(dp2[j],query(0,0,n-1,0,j-1)+it[j]);
			}
			else{
				dp2[j]=min(dp2[j],query(0,0,n-1,pre[j],j-1)+it[j]);
			}
		}
		for(int j=0;j<n;j++){
			dp[j]=dp2[j];
			dp2[j]=1e9;
		}
	}

	cout<<dp[n-1]<<endl;






	return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 384 KB Output is correct
2 Execution timed out 1095 ms 384 KB Time limit exceeded
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 384 KB Output is correct
2 Execution timed out 1090 ms 256 KB Time limit exceeded
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 1087 ms 384 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 1089 ms 384 KB Time limit exceeded
2 Halted 0 ms 0 KB -