wps 二分求调
查看原帖
wps 二分求调
846661
ARIS1_0楼主2025/1/13 20:00

eps 开到 1e-18 都没法过,是精度问题还是哪里有其他问题?

#include<bits/stdc++.h>
#define ll long long
using namespace std;
mt19937 myrand(time(0));
inline ll read(){
	ll x=0,w=1;
	char ch=0;
	while(ch<'0'||ch>'9'){
		if(ch=='-')w=-1;
		ch=getchar();
	}
	while(ch>='0'&&ch<='9'){
		x=(x<<3)+(x<<1)+(ch^48);
		ch=getchar();
	}
	return x*w;
}
void write(ll x){
	if(x<0){
		putchar('-');
		x=-x;
	}
	static int sta[35];
	int top=0;
	do{
		sta[top++]=x%10,x/=10;
	}while(x);
	while(top)putchar(sta[--top]+'0');
}
const long double eps=1e-15;
int n,k,cnt[100005],q[100005];
long double dp[100005],l,r=1e6;
inline long double getx(int x,int y){return dp[x]-dp[y];}
inline long double gety(int x,int y){return x-y;}
inline long double getk(int x,int y){return gety(x,y)/getx(x,y);}
bool check(long double x){
	for(int i=1;i<=n;i++)cnt[i]=0;
	int st=1,ed=1;
	q[st]=0;
	for(int i=1;i<=n;i++){
//		for(int m=ed;m<=st;m++)cout<<q[m]<<" ";
//		system("pause");
		while(ed<st&&getk(q[ed+1],q[ed])>1/(long double)i+eps)ed++;
		dp[i]=dp[q[ed]]+(long double)(i-q[ed])/i-x;
		cnt[i]=cnt[q[ed]]+1;
		while(ed<st&&getk(q[st],q[st-1])+eps<getk(i,q[st]))st--;
		q[++st]=i;
	}
	return cnt[n]>=k;
}
int main(){
	n=read();k=read();
	while(l+eps<r){
		long double mid=(l+r)/2.0;
		if(check(mid))l=mid;
		else r=mid;
	}
	printf("%.9Lf",dp[n]+l*k);
	return 0;
}
2025/1/13 20:00
加载中...