codeforces1407D Discrete Centrifugal Jumps

本文详细解析了Codeforces比赛中的D题解决方案,通过使用树状数组预处理和动态规划技术,实现了高效查找并更新最短路径。文章深入讨论了如何根据序列特性优化搜索策略。

https://codeforces.com/contest/1407/problem/D

mdA题没做出来,吧D过了以后一看cf predictor竟然还能上分,刷牙的时候想着不会fst哪题吧?回来一看这题fst了我透,日常优化常数把人剪没了。

可以发现,我们如果要从i跳到j,那么假设我们当前在i,如果a[i+1]>a[i],那么只能按照一个严格下降子序列转移,否则则是按照一个严格上升子序列转移,

所以用树状数组预处理出每个位置右边离它最近的比他大的nxtup[i]和比他小的nxtd[i],然后一直向右判断能否转移就行了,如果还没更新过或者不如当前转移优就能转移。

 

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;

const int maxl=3e5+10;

int n,m,cnt,tot,cas,ans;
int a[maxl],dp[maxl],num[maxl];
int b[maxl],nxtup[maxl],nxtd[maxl];
char s[maxl];

inline void upd(int i,int x)
{
	while(i<=tot)
	{
		b[i]=min(x,b[i]);
		i+=i&-i;
	}
}

inline int qry(int i)
{
	int ret=n+1;
	while(i)
	{
		ret=min(ret,b[i]);
		i-=i&-i;
	}
	return ret;
}

inline void prework()
{
	scanf("%d",&n);
	for(int i=1;i<=n;i++)
		scanf("%d",&a[i]),num[i]=a[i];
	sort(num+1,num+1+n);
	tot=unique(num+1,num+1+n)-num-1;
	for(int i=1;i<=n;i++)
		a[i]=lower_bound(num+1,num+1+tot,a[i])-num;
	for(int i=0;i<=tot;i++)
		b[i]=n+1;
	int c;
	for(int i=n;i>=1;i--)
	{
		c=a[i];
		nxtd[i]=qry(c-1);
		upd(c,i);
	}
	for(int i=0;i<=tot;i++)
		b[i]=n+1;
	for(int i=n;i>=1;i--)
	{
		c=tot-a[i]+1;
		nxtup[i]=qry(c-1);
		upd(c,i);
	}
}

inline void mainwork()
{
	dp[1]=0;int now,last;
	for(int i=2;i<=n;i++)
		dp[i]=n-1;
	for(int i=1;i<=n-1;i++)
	{
		dp[i+1]=min(dp[i+1],dp[i]+1);
		if(a[i+1]>a[i])
		{
			now=i+1;
			while(nxtd[now]<=n)
			{
				last=now;now=nxtd[now];
				if(dp[now]<=dp[i]+1 || a[last]<=a[i])
					break;
				dp[now]=min(dp[now],dp[i]+1);
			}
		}
		if(a[i+1]<a[i])
		{
			now=i+1;
			while(nxtup[now]<=n)
			{
				last=now;now=nxtup[now];
				if(dp[now]<=dp[i]+1 || a[last]>=a[i])
					break;
				dp[now]=min(dp[now],dp[i]+1);
			}
		}
	}
}

inline void print()
{
	printf("%d\n",dp[n]);
}

int main()
{
	int t=1;
	//scanf("%d",&t);
	for(cas=1;cas<=t;cas++)
	{
		prework();
		mainwork();
		print();
	}
	return 0;
}

 

### Codeforces 1487D Problem Solution The problem described involves determining the maximum amount of a product that can be created from given quantities of ingredients under an idealized production process. For this specific case on Codeforces with problem number 1487D, while direct details about this exact question are not provided here, similar problems often involve resource allocation or limiting reagent type calculations. For instance, when faced with such constraints-based questions where multiple resources contribute to producing one unit of output but at different ratios, finding the bottleneck becomes crucial. In another context related to crafting items using various materials, it was determined that the formula `min(a[0],a[1],a[2]/2,a[3]/7,a[4]/4)` could represent how these limits interact[^1]. However, applying this directly without knowing specifics like what each array element represents in relation to the actual requirements for creating "philosophical stones" as mentioned would require adjustments based upon the precise conditions outlined within 1487D itself. To solve or discuss solutions effectively regarding Codeforces' challenge numbered 1487D: - Carefully read through all aspects presented by the contest organizers. - Identify which ingredient or component acts as the primary constraint towards achieving full capacity utilization. - Implement logic reflecting those relationships accurately; typically involving loops, conditionals, and possibly dynamic programming depending on complexity level required beyond simple minimum value determination across adjusted inputs. ```cpp #include <iostream> #include <vector> using namespace std; int main() { int n; cin >> n; vector<long long> a(n); for(int i=0;i<n;++i){ cin>>a[i]; } // Assuming indices correspond appropriately per problem statement's ratio requirement cout << min({a[0], a[1], a[2]/2LL, a[3]/7LL, a[4]/4LL}) << endl; } ``` --related questions-- 1. How does identifying bottlenecks help optimize algorithms solving constrained optimization problems? 2. What strategies should contestants adopt when translating mathematical formulas into code during competitive coding events? 3. Can you explain why understanding input-output relations is critical before implementing any algorithmic approach? 4. In what ways do prefix-suffix-middle frameworks enhance model training efficiency outside of just tokenization improvements? 5. Why might adjusting sample proportions specifically benefit models designed for tasks requiring both strong linguistic comprehension alongside logical reasoning skills?
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值