Submission #102990

# Submission time Handle Problem Language Result Execution time Memory
102990 2019-03-28T11:02:55 Z mraron Akvizna (COCI19_akvizna) C++14
130 / 130
598 ms 7276 KB
#include<iostream>
#include<vector>
#include<map>
#include<set>
#include<cassert>
#include<cassert>
#include<unordered_map>
#include<unordered_set>
#include<functional>
#include<queue>
#include<stack>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<sstream>
#include<iomanip>
#include<cstdio>
#include<cstdlib>
#include<numeric>
using namespace std;

#define all(x) (x).begin(), (x).end()
#define pb push_back
#define xx first
#define yy second
#define sz(x) (int)(x).size()
#define gc getchar
#define IO ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0)
#define mp make_pair

#ifndef ONLINE_JUDGE
#  define LOG(x) (cerr << #x << " = " << (x) << endl)
#else
#  define LOG(x) ((void)0)
#endif

typedef long long ll;
typedef unsigned long long ull;
typedef double ld;

const double PI=3.1415926535897932384626433832795;
const ll INF = 1LL<<62;
const ll MINF = -1LL<<62;

template<typename T> T getint() {
	T val=0;
	char c;
	
	bool neg=false;
	while((c=gc()) && !(c>='0' && c<='9')) {
		neg|=c=='-';
	}

	do {
		val=(val*10)+c-'0';
	} while((c=gc()) && (c>='0' && c<='9'));

	return val*(neg?-1:1);
}

//
// SQRT time convex hull trick
//

struct line {
	ld xx;
	ld yy;
	int cnt;
	//int par;
	
};

struct sqrt_CHT {
	int mn;
	int blksz;
	
	vector<line> hull, reserve;
	
	sqrt_CHT(bool min_, int blksz_) {
		mn=min_?1:-1;
		blksz=blksz_;
	}
		
	//
	// Checks wheter b is optimal if a<b<c holds
	// Complexity: O(1)
	//
	
	bool optimal(line& a, line& b, line& c) {
		return double(b.yy-a.yy)*double(c.xx-b.xx)>double(b.yy-c.yy)*double(a.xx-b.xx);
	}
	
	//
	// Inserts linear function into the hull
	// Complexity: O(1) if reserve is not full else O(sz(hull) log sz(hull) + blksz)
	//
	
	void insert(line a) {
		static auto cmp = [](const line& a, const line& b) -> bool {
			if(a.xx==b.xx) {
				return a.yy>b.yy;
			}
			
			return a.xx>b.xx;
		};
		
		a.xx*=mn;a.yy*=mn;
		
		
		//reserve.pb(a);
		hull.pb(a);
			
		while((sz(hull)>=2 && hull[sz(hull)-1].xx==hull[sz(hull)-2].xx) ||
			      (sz(hull)>=3 && !optimal(hull[sz(hull)-3], hull[sz(hull)-2], hull[sz(hull)-1]))) {
				line tmp=hull.back();
				hull.pop_back();
				hull.pop_back();
				
				hull.pb(tmp);
		}
		
		if(sz(reserve)>=blksz) { //újjáépítés ha a tartalék mérete meghaladja a megadott korlátot	
			vector<line> nhull;
			for(int i=0,j=0;i<sz(hull)||j<sz(reserve);) {
				if(i<sz(hull) && (j==sz(reserve) || (j<sz(reserve) && cmp(hull[i], reserve[j])))) {
					nhull.pb(hull[i++]);
				}else {
					nhull.pb(reserve[j++]);
				}
			
			}
		
			reserve.resize(0);
			hull.swap(nhull);
		}
	}
	
	//
	// Gets the optimal (minimal or maximal) value of inserted function at a given x point
	// Complexity: O(log(sz(hull)) + blksz)
	//

	pair<ld, line> get(ld x) {
		pair<ld,line> ans;
		ans.xx=100000000.0;
		
		for(auto i:reserve) {
			if(ans.xx>ld(i.xx*x+i.yy)) {
				ans.xx=ld(i.xx*x+i.yy);
				ans.yy=i;
			}
		}

		if(hull.empty()) {
			ans.xx*=mn;
			return ans;
		}
		
		int L=0, R=sz(hull)-1;
		while(L<R) {
			int mid=(L+R+1)/2;
			
			ld val1=hull[mid-1].xx*x+hull[mid-1].yy, val2=hull[mid].xx*x+hull[mid].yy;
			if(val1<val2) {
				R=mid-1;
			}else {
				L=mid;
			}
		}
		
		if(ans.xx>hull[L].xx*x+hull[L].yy) {
			ans.xx=hull[L].xx*x+hull[L].yy;
			ans.yy=hull[L];
		}
		
		ans.xx*=mn;
		return ans;
	}
	
	private: 
};




double dp[100001];
int cnt[100001];
int par[100001];

int main() {
	IO;
	int n,k,it=0;
	cin>>n>>k;
	double L=0, R=2;
	while(1) {
		double mid=(L+R)/2;
		dp[0]=0;
		cnt[0]=0;
		sqrt_CHT cht(false, 900);
		cht.insert({double(1)/double(n),0,0});
		for(int i=1;i<=n;++i) {
			pair<ld, line> val=cht.get(i);
			
			dp[i]=val.xx-mid;
			cnt[i]=val.yy.cnt+1;
			//par[i]=val.yy.par;
			if(i<n) {
				cht.insert({double(1)/double(n-i),dp[i]-double(i)/double(n-i),cnt[i],});
			}
		}
		//cerr<<mid<<" "<<cnt[n]<<"\n";
		if(cnt[n]<=k) {
			R=mid;
		}else {
			L=mid;
		}
		it++;
		
		if(it>=40 && cnt[n]==k) break ;
	}
	
	cout<<fixed<<setprecision(9);
	cout<<dp[n]+L*cnt[n]<<"\n";
	
/*	int akt=n;
	while(akt>0) {
		cerr<<akt-par[akt]<<"/"<<n-par[akt]<<"\n";
		akt=par[akt];
	}*/
	return 0;
}



/*
double dp[3001][3001];

int main() {
	IO;
	int n,k;
	cin>>n>>k;
	dp[n][0]=0.0;
	for(int i=1;i<=k;++i) {
		for(int j=0;j<=n;++j) {
			for(int l=0;l<=n/i && j+l<=n;l++) {
				dp[j][i]=max(dp[j][i], dp[j+l][i-1]+(l>0?(double)l/(double)(j+l):0));
			}
		}
	}
	cout<<fixed<<setprecision(10);	
	cout<<dp[0][k]<<"\n";
	return 0;
}*/



# Verdict Execution time Memory Grader output
1 Correct 3 ms 384 KB Output is correct
2 Correct 2 ms 384 KB Output is correct
3 Correct 3 ms 384 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 384 KB Output is correct
2 Correct 2 ms 384 KB Output is correct
3 Correct 2 ms 384 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 384 KB Output is correct
2 Correct 2 ms 384 KB Output is correct
3 Correct 3 ms 384 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 384 KB Output is correct
2 Correct 2 ms 384 KB Output is correct
3 Correct 3 ms 384 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 10 ms 620 KB Output is correct
2 Correct 9 ms 596 KB Output is correct
3 Correct 10 ms 692 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 11 ms 628 KB Output is correct
2 Correct 11 ms 588 KB Output is correct
3 Correct 9 ms 624 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 8 ms 588 KB Output is correct
2 Correct 10 ms 572 KB Output is correct
3 Correct 10 ms 608 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 11 ms 624 KB Output is correct
2 Correct 10 ms 628 KB Output is correct
3 Correct 9 ms 580 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 12 ms 624 KB Output is correct
2 Correct 12 ms 588 KB Output is correct
3 Correct 12 ms 568 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 11 ms 576 KB Output is correct
2 Correct 10 ms 628 KB Output is correct
3 Correct 12 ms 624 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 13 ms 624 KB Output is correct
2 Correct 12 ms 588 KB Output is correct
3 Correct 9 ms 592 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 10 ms 584 KB Output is correct
2 Correct 10 ms 588 KB Output is correct
3 Correct 10 ms 624 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 12 ms 472 KB Output is correct
2 Correct 15 ms 628 KB Output is correct
3 Correct 11 ms 628 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 508 ms 6936 KB Output is correct
2 Correct 515 ms 7040 KB Output is correct
3 Correct 592 ms 6832 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 448 ms 6972 KB Output is correct
2 Correct 461 ms 6908 KB Output is correct
3 Correct 451 ms 7092 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 434 ms 6772 KB Output is correct
2 Correct 574 ms 7060 KB Output is correct
3 Correct 582 ms 7092 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 467 ms 6900 KB Output is correct
2 Correct 509 ms 7064 KB Output is correct
3 Correct 557 ms 6972 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 394 ms 6816 KB Output is correct
2 Correct 553 ms 7072 KB Output is correct
3 Correct 523 ms 6984 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 480 ms 6980 KB Output is correct
2 Correct 485 ms 7000 KB Output is correct
3 Correct 501 ms 6972 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 578 ms 6956 KB Output is correct
2 Correct 543 ms 7068 KB Output is correct
3 Correct 562 ms 6804 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 419 ms 6864 KB Output is correct
2 Correct 447 ms 7028 KB Output is correct
3 Correct 598 ms 6960 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 461 ms 7052 KB Output is correct
2 Correct 503 ms 7076 KB Output is correct
3 Correct 542 ms 7084 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 436 ms 7224 KB Output is correct
2 Correct 416 ms 6944 KB Output is correct
3 Correct 501 ms 7172 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 440 ms 7036 KB Output is correct
2 Correct 487 ms 7244 KB Output is correct
3 Correct 558 ms 7212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 451 ms 7120 KB Output is correct
2 Correct 470 ms 7276 KB Output is correct
3 Correct 518 ms 7192 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 521 ms 7164 KB Output is correct
2 Correct 485 ms 7208 KB Output is correct
3 Correct 551 ms 7204 KB Output is correct