codeforces1398E Two Types of Spells

本文详细解析了Codeforces竞赛1398题E的算法思路,通过维护三个集合实现最优解,探讨了如何在比赛中高效解决问题的方法。

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

设当前有cnt个光之符卡

维护3个set,c表示最大的cnt个符卡,f1表示不是c中的剩下的火卡,l1表示不是c中的剩下的光卡

那么答案就是c中的所有卡的值*2+剩下的卡的值,sum为所有卡的值,sum1位c中卡的值,ans=sum+sum1

接下来我们就只需要维护c中总是所有卡中最大的cnt个就行了,当前szc!=cnt都判断一下,然后如果剩下的卡中有比c最小的卡大的就替换一下

注意特判特殊情况,维护一个cntc,表示c中有多少个光之符卡,如果cntc==cnt的话,也就是c中全是光之符卡,那必有一个无法*2,把最小的去掉然后加上剩下的火卡中最大的就行了。

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

const int maxl=2e5+10;

int n,cnt;ll ans,sum,sum1;
struct spell
{
	int val,tp,id;
	bool operator < (const spell &b)const
	{
		if(val==b.val)
		{
			if(tp==b.tp)
				return id<b.id;
			return tp<b.tp;
		}
		return val<b.val;
	}
	bool operator == (const spell &b)const
	{
		return id==b.id;
	}
	bool operator > (const spell &b)const
	{
		if(val==b.val)
		{
			if(tp==b.tp)
				return id>b.id;
			return tp>b.tp;
		}
		return val>b.val;
	}
}a[maxl];
set<spell> f1,l1,c;
set<spell> :: iterator it; 

inline void prework()
{
	scanf("%d",&n);
}

inline void mainwork()
{
	sum=0;ans=0;sum1=0;cnt=0;
	int szc=0,cntc=0,fmx,lmx;
	spell d;
	for(int i=1;i<=n;i++)
	{
		scanf("%d%d",&a[i].tp,&a[i].val);
		a[i].id=i;
		if(a[i].val>0)
		{
			sum+=a[i].val;
			if(a[i].tp==0)
				f1.insert(a[i]);
			else
			{
				l1.insert(a[i]);
				++cnt;
			}
		}
		else
		{
			if(a[i].tp==0)
			{
				sum+=a[i].val;
				it=c.lower_bound({-a[i].val,0,0});
				if(it!=c.end() && (*it).val==-a[i].val && (*it).tp==0)
					sum1+=a[i].val,c.erase(it),szc--;
				else
					f1.erase(f1.lower_bound({-a[i].val,0,0}));
			}
			else
			{
				--cnt;sum+=a[i].val;
				it=c.lower_bound({-a[i].val,1,0});
				if(it!=c.end() && (*it).val==-a[i].val && (*it).tp==1)
					sum1+=a[i].val,c.erase(it),szc--,cntc--;
				else
					l1.erase(l1.lower_bound({-a[i].val,1,0}));
			}
		}
		while(szc<cnt)
		{
			if(f1.begin()==f1.end())
			{
				it=l1.end();--it;c.insert(*it);
				sum1+=(*it).val;szc++;cntc++;		
				l1.erase(it);
			}
			else if(l1.begin()==l1.end())
			{
				it=f1.end();--it;c.insert(*it);
				sum1+=(*it).val;szc++;
				f1.erase(it);
			}
			else
			{
				if((*f1.rbegin()).val>=(*l1.rbegin()).val)
				{
					it=f1.end();--it;c.insert(*it);
					sum1+=(*it).val;szc++;
					f1.erase(it);
				}
				else
				{
					it=l1.end();--it;c.insert(*it);
					sum1+=(*it).val;szc++;cntc++;		
					l1.erase(it);
				}
			}
		}
		while(szc>cnt)
		{
			d=(*c.begin());sum1-=d.val;
			if(d.tp==0) 
				f1.insert(d);
			else 
				l1.insert(d),--cntc;
			szc--;c.erase(c.begin());
		}
		while(cnt>0)
		{
			fmx=0;lmx=0;
			if(f1.begin()!=f1.end())
				fmx=(*f1.rbegin()).val;
			if(l1.begin()!=l1.end())
				lmx=(*l1.rbegin()).val;
			d=*c.begin();
			if(max(fmx,lmx)<=d.val)
				break;
			sum1-=d.val;c.erase(c.begin());
			if(d.tp==1) cntc--;
			if(fmx>lmx)
			{
				sum1+=fmx;it=f1.end();--it;
				c.insert(*it);f1.erase(it);
			}
			else
			{
				sum1+=lmx;it=l1.end();--it;++cntc;
				c.insert(*it);l1.erase(it);
			}
			if(d.tp==0)
				f1.insert(d);
			else
				l1.insert(d);
		}
		if(cnt>0 && cnt==cntc)
		{
			ans=sum1+sum-(*c.begin()).val;
			if(f1.begin()!=f1.end())
				ans+=(*f1.rbegin()).val;
		}
		else
			ans=sum1+sum;
		printf("%lld\n",ans);
	}
}

int main()
{
	prework();
	mainwork();
}

 

【直流微电网】径向直流微电网的状态空间建模与线性化:一种耦合DC-DC变换器状态空间平均模型的方法 (Matlab代码实现)内容概要:本文介绍了径向直流微电网的状态空间建模与线性化方法,重点提出了一种基于耦合DC-DC变换器状态空间平均模型的建模策略。该方法通过对系统中多个相互耦合的DC-DC变换器进行统一建模,构建出整个微电网的集中状态空间模型,并在此基础上实施线性化处理,便于后续的小信号分析与稳定性研究。文中详细阐述了建模过程中的关键步骤,包括电路拓扑分析、状态变量选取、平均化处理以及雅可比矩阵的推导,最终通过Matlab代码实现模型仿真验证,展示了该方法在动态响应分析和控制器设计中的有效性。; 适合人群:具备电力电子、自动控制理论基础,熟悉Matlab/Simulink仿真工具,从事微电网、新能源系统建模与控制研究的研究生、科研人员及工程技术人员。; 使用场景及目标:①掌握直流微电网中多变换器系统的统一建模方法;②理解状态空间平均法在非线性电力电子系统中的应用;③实现系统线性化并用于稳定性分析与控制器设计;④通过Matlab代码复现和扩展模型,服务于科研仿真与教学实践。; 阅读建议:建议读者结合Matlab代码逐步理解建模流程,重点关注状态变量的选择与平均化处理的数学推导,同时可尝试修改系统参数或拓扑结构以加深对模型通用性和适应性的理解。
### 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、付费专栏及课程。

余额充值