507 Lusir的游戏 二分 数论 [代码源][namomo spring camp]每日一题div2

507 Lusir的游戏 二分 数论 [代码源][namomo spring camp]每日一题div2,第1张


可以二分硬搞一下,注意check里面res容易炸,加特判

除此以外还可以纯数学搞,思路写这里
假设我们到达了第i个点
那么

E i = 2 ∗ E i − 1 − H ( i ) = 2 i E 0 − ∑ j = 1 i 2 i − j H ( j ) E i > = 0 E 0 > = ∑ j = 1 N H ( j ) 2 j \begin{array}{l} {E_i} = 2*{E_{i - 1}} - H(i)\\ = {2^i}{E_0} - \sum\nolimits_{j = 1}^i {{2^{i - j}}} H(j)\\ {E_i} > = 0\\ {E_0} > = \sum\nolimits_{j = 1}^N {\frac{{H(j)}}{{{2^j}}}} \end{array} Ei=2Ei1H(i)=2iE0j=1i2ijH(j)Ei>=0E0>=j=1N2jH(j)

求后面的那个就可以了

//acmer mxc
#include
#define mst(s,x) memset(s,x,sizeof(s));
#define sr(x) scanf("%d",&x); 
#define sr2(a,b) scanf("%d%d",&a,&b); 
#define sr3(a,b,c) scanf("%d%d%d",&a,&b,&c); 
#define f(i,a,n) for(int i=a;i<=n;i++)
#define sc(x) cout<<#x<<" : "<<x<<endl;
#define hh cout<<endl;
//#define int __int128
#define pii pair<int,int>
#define pb(x) push_back(x)
#define mk(a,b) make_pair(a,b)
#define ls(x) x<<1
#define rs(x) x<<1|1
#define fi first
#define se second
#define ll __int128
#define inf 0x3f3f3f3f
using namespace std;
inline ll read()
{
	ll x=0,w=1; char c=getchar();
	while(c<'0'||c>'9') {if(c=='-') w=-1; c=getchar();}
	while(c<='9'&&c>='0') x=(x<<1)+(x<<3)+c-'0',c=getchar();
	return w==1?x:-x;
}
inline void write(ll x)
{
	if(x>=10) write(x/10);
	putchar(x%10+'0');
}
//------------------------------------------------
// #define int long long
#define int __int128
#define LL long long
#define dd double
const int N=1e6+5;
const int mod=1e9+7;
int a[N];
int n;
bool check(int mid)
{
	int res=mid;
	for(int i=1;i<=n;i++)
	{
		if(res>a[i]) res=res+(res-a[i]);
		else res=res-(a[i]-res);
		if(res>1e5) return true;
		if(res<0) return false;
	}
	return true;
}
inline void solve()
{
	n=read();
	for(int i=1;i<=n;i++)a[i]=read();
	int l=0,r=1e5;
	while(l<r)
	{
		int mid=(l+r)>>1;
		if(check(mid))r=mid;
		else l=mid+1;
	}
	write(l);
}

//------------------------------------------------
signed main()
{
	clock_t c1=clock();
#ifdef LOCAL
    freopen("in.in","r",stdin);
    freopen("out.out","w",stdout);
#endif
//==================================
    int T=1;
	// cin>>T;
	for(int i=1;i<=T;i++)
	{
		solve();
	}
//==================================
end:
    cerr<<endl<< "Time used:" << clock() - c1 << endl;
	return 0;
}

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/733363.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-04-27
下一篇 2022-04-28

发表评论

登录后才能评论

评论列表(0条)

保存