codeforces1355E Restorer Distance

本文详细解析了Codeforces比赛中的E题,通过分析题目特点,提出了一种基于排序和离散化的高效解题策略。文章介绍了如何利用一次函数特性找到最小花费方案,以及如何在O(1)时间内更新所需砖头数量。

https://codeforces.com/contest/1355/problem/E

这场难度c>a>e>d,作为e题偏水,d题更偏水,a又是机智题我没get到点,20分钟看了3k人过了果断换成小号打,结果发现好像打大号也能上分。

假设最后的高度是h,需要添加的砖头总数是dec,需要拿走的砖头总数是res,根据,a,r,m的关系,我们要么尽可能使用a,要么尽可能使用b,要么尽可能是用m。

那么就对所有高度排序,由于相邻高度之间的花费是一个一次函数,那么一定在端点取到最小值。

注意第3个样例,如果要尽量取m,那么高度是sum/n,sum/n+1的时候m最多,a,r最少,所以也要把这两个高度加进去离散化。

离散化以后对每个高度有多少个位置,搞个前缀和,就可以O(1)转移dec和res了

然后选取也只有两种情况,要么取a和r,要么尽量去m

其实还可以三分高度以后O(n)判断,一样的

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

const int maxl=3e5+10;

int n,cas,tot;
ll ans,a,r,m;
int h[maxl],num[maxl];
ll b[maxl],sum[maxl];
char s[maxl];
bool in[maxl]; 

inline int id(int x)
{
	return lower_bound(num+1,num+1+tot,x)-num;
}

inline void prework()
{
	ll cnt=0;
	scanf("%d%lld%lld%lld",&n,&a,&r,&m);
	for(int i=1;i<=n;i++)
	{
		scanf("%d",&h[i]);
		num[++tot]=h[i];cnt+=h[i];
	}
	num[++tot]=cnt/n;num[++tot]=cnt/n+1;
	sort(num+1,num+1+tot);
	tot=unique(num+1,num+1+tot)-num-1;
	for(int i=1;i<=n;i++)
		b[id(h[i])]++;
	for(int i=1;i<=tot;i++)
		sum[i]=sum[i-1]+b[i];	
} 

inline void mainwork()
{
	ll tmp1,tmp2,ned=0,res=0,mi;
	for(int i=1;i<=n;i++)
		res+=(h[i]-num[1]);
	tmp1=res*r;ans=tmp1;
	for(int i=2;i<=tot;i++)
	{
		res-=(sum[tot]-sum[i-1])*(num[i]-num[i-1]);
		ned+=sum[i-1]*(num[i]-num[i-1]);
		mi=min(res,ned);
		tmp1=(ned-mi)*a+(res-mi)*r+mi*m;
		tmp2=ned*a+res*r;
		ans=min(ans,min(tmp1,tmp2));
	}
}

inline void print()
{
	printf("%lld\n",ans);	
}

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

 

### Codeforces 887E Problem Solution and Discussion The problem **887E - The Great Game** on Codeforces involves a strategic game between two players who take turns to perform operations under specific rules. To tackle this challenge effectively, understanding both dynamic programming (DP) techniques and bitwise manipulation is crucial. #### Dynamic Programming Approach One effective method to approach this problem utilizes DP with memoization. By defining `dp[i][j]` as the optimal result when starting from state `(i,j)` where `i` represents current position and `j` indicates some status flag related to previous moves: ```cpp #include <bits/stdc++.h> using namespace std; const int MAXN = ...; // Define based on constraints int dp[MAXN][2]; // Function to calculate minimum steps using top-down DP int minSteps(int pos, bool prevMoveType) { if (pos >= N) return 0; if (dp[pos][prevMoveType] != -1) return dp[pos][prevMoveType]; int res = INT_MAX; // Try all possible next positions and update 'res' for (...) { /* Logic here */ } dp[pos][prevMoveType] = res; return res; } ``` This code snippet outlines how one might structure a solution involving recursive calls combined with caching results through an array named `dp`. #### Bitwise Operations Insight Another critical aspect lies within efficiently handling large integers via bitwise operators instead of arithmetic ones whenever applicable. This optimization can significantly reduce computation time especially given tight limits often found in competitive coding challenges like those hosted by platforms such as Codeforces[^1]. For detailed discussions about similar problems or more insights into solving strategies specifically tailored towards contest preparation, visiting forums dedicated to algorithmic contests would be beneficial. Websites associated directly with Codeforces offer rich resources including editorials written after each round which provide comprehensive explanations alongside alternative approaches taken by successful contestants during live events. --related questions-- 1. What are common pitfalls encountered while implementing dynamic programming solutions? 2. How does bit manipulation improve performance in algorithms dealing with integer values? 3. Can you recommend any online communities focused on discussing competitive programming tactics? 4. Are there particular patterns that frequently appear across different levels of difficulty within Codeforces contests?
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值