CF 220C Little Elephant and Shifts

本文介绍了一种解决CodeForces 220C问题的有效算法,通过使用优先队列和延迟标记的方法避免了O(n^2)的复杂度,实现了O(nlogn)的时间效率。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目链接:http://codeforces.com/problemset/problem/220/C


题目大意:

a为长度为n的不变的串,b为长度为n的可循环左移的串.

a,b均由1~n组成,每个数在每串中用一次.

a串与b串的值为min{ |i-j| },ai==bj

求所有bi,bi+1...bn,bn-1...b1,与a串的值.


题目思路:

1<=n<=100000,朴素算法O(n^2)肯定不行,所以想到了O(nlogn)的线段树,纠结了很久,搞出个维护绝对值的最小值,但是表示没有任何想法= =...

之后发现对于每次操作,要么是某些数+1,要么是某些数-1(首个数除外)...本来还想用线段树来写,写着写着发现怎么看怎么像优先队列+延迟标记...


代码:

#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <ctype.h>
#include <math.h>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <vector>
#include <string>
#include <iostream>
#include <algorithm>
using namespace std;

#define ll long long
#define ls rt<<1
#define rs ls|1
#define lson l,mid,ls
#define rson mid+1,r,rs
#define middle (l+r)>>1
#define eps (1e-9)
#define type int
#define clr_all(x,c) memset(x,c,sizeof(x))
#define clr(x,c,n) memset(x,c,sizeof(x[0])*(n+1))
#define MOD 1000000007
#define inf 0x3f3f3f3f
#define pi acos(-1.0)

template <class T> void _swap(T &x,T &y){T t=x;x=y;y=t;}
template <class T> T _max(T x,T y){return x>y? x:y;}
template <class T> T _min(T x,T y){return x<y? x:y;}
int test,cas;
const int M=100000 +5;

int n,m;
int ap[M],b[M],cur[M];

struct node{
	int id,cur,num;
	node(){}
	node(int _i,int _c,int _n){id=_i,cur=_c,num=_n;}
	bool operator < (const node& t) const{
		return num > t.num;
	}
};
priority_queue<node>rgt,lft;
int covr,covl;

void run(){
	int i,j;
	for(i=1;i<=n;i++){
		scanf("%d",&j);
		ap[j]=i;
	}
	while(!rgt.empty()) rgt.pop();
	while(!lft.empty()) lft.pop();
	clr(cur,0,n+1);
	for(i=1;i<=n;i++){
		scanf("%d",&b[i]);
		if(i>ap[b[i]]) rgt.push(node(b[i],0,i-ap[b[i]]));
		else lft.push(node(b[i],0,ap[b[i]]-i));
	}
	covr=covl=0;
	node r,l;
	for(i=1;;i++){
		while(!rgt.empty()){
			r=rgt.top();
			if(r.cur!=cur[r.id]) rgt.pop();
			else if(r.num+covr==0){
				rgt.pop();
				lft.push(node(r.id,r.cur,-covl));
			}else break;
		}
		while(!lft.empty()){
			l=lft.top();
			if(l.cur!=cur[l.id]) lft.pop();
			else break;
		}
		int ans=inf;
		if(!rgt.empty()) ans=_min(ans,rgt.top().num+covr);
		if(!lft.empty()) ans=_min(ans,lft.top().num+covl);
		printf("%d\n",ans);
		if(i==n) break;
		covr--,covl++;
		cur[b[i]]++;
		if(n>ap[b[i]]) rgt.push(node(b[i],cur[b[i]],n-ap[b[i]]-covr));
		else lft.push(node(b[i],cur[b[i]],-covl));
	}
}

void preSof(){
}

int main(){
	//freopen("input.txt","r",stdin);
	//freopen("output.txt","w",stdout);
	preSof();
	//run();
	while(~scanf("%d",&n)) run();
	//for(scanf("%d",&test),cas=1;cas<=test;cas++) run();
	return 0;
}


03-22
### TO-220C 封装概述 TO-220 是一种常见的半导体器件封装形式,广泛用于功率晶体管、稳压器和其他高功耗电子元件。TO-220C 表示该封装的一个变体,通常具有特定的设计改进或材料差异[^1]。 #### 外形尺寸 TO-220C 的外形设计保持了标准 TO-220 的基本特征,其典型参数如下: - 总高度:约 4.5 mm 至 6.7 mm。 - 器件主体宽度:约为 10.9 mm 至 11.8 mm。 - 引脚间距:一般为 2.54 mm 或其他定制化选项。 这些数据可能因制造商而略有不同,因此建议查阅具体厂商的数据手册以获取精确数值[^2]。 #### 热性能特性 此封装支持良好的散热能力,通过金属底板直接连接到外部散热片来增强热传导效率。典型的结至环境热阻 (RθJA) 范围大约在 40°C/W 到 60°C/W之间,这取决于实际应用条件以及附加的冷却措施是否存在[^3]。 #### 电气参数 对于采用 TO-220C 封装的产品而言,具体的电压、电流额定值会依据内部芯片的功能有所变化。例如,在某些 NPN/PNP 双极型晶体管中可以见到以下范围内的工作参数: - 集电极最大允许电流 IC(MAX): ±5A ~±8A; - 发射极集电极间击穿电压 V(BR)CEO: ≥40V~≥100V; 以上仅为示意性的例子,请参照目标组件的确切文档确认适用的标准[^4]。 ```python # 示例 Python 计算 RθJA 对温度影响的小程序 def calculate_junction_temperature(power_dissipation, r_theta_ja, ambient_temp=25): """ Calculate junction temperature based on power dissipated and thermal resistance. :param power_dissipation: Power dissipated by the device in watts. :param r_theta_ja: Junction-to-air thermal resistance in degrees Celsius per watt. :param ambient_temp: Ambient air temperature around the component in degrees Celsius. :return: Estimated junction temperature of the semiconductor die. """ return ambient_temp + (power_dissipation * r_theta_ja) example_power = 3 # Example value for demonstration purposes only thermal_resistance = 50 # Hypothetical RθJA value; check datasheet for actual figure. junction_temp_estimate = calculate_junction_temperature(example_power, thermal_resistance) print(f"Estimated Junction Temperature with {example_power}W at {thermal_resistance}°C/W is approximately {junction_temp_estimate:.1f}°C.") ``` 上述代码片段展示了一个简单的函数用来估算给定条件下半导体结点处的预期温升情况。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值