关于我的码风

关于我的码风

  • \(8\) 字符 tab 喵, 不用空格缩进, respect to linux.
  • 运算符左右不加空格.
  • 大括号风格非常离谱, 参考上世纪的码风, 上世纪的屏幕较小所以省略行数喵.
  • 因为对优先级非常了解, 所以能省略括号就会省略. // 这个可以多看 man operator 来练习, 但 man 命令仅限 linux.
  • long live 万能头, #include<bits/stdc++.h>.
  • using namespace std::string_literals 用于使用 "i ak wf"s 表示 std::string 常量.
  • lf 是浮点, ll 是整数, ull 是无符号整数, us 是无符号短整数.
  • 无用的快读 nxt_lf() nxt_ll() nxt_ull() nxt_us() 都是给 std::cin 的打包.
  • inf 是著名极大值 \(3f3f~3f3f~3f3f~3f3f\).
  • mod 是著名模数 \(998~244~353\) 的 hex \(3b800001\) 喵.
  • 记录题目链接和思考时间, 编码时间.
  • class 记录数据结构喵.
  • ld rd 记录左右子顶点.
  • 线段树当前顶点的区间是 \([f,g]\).
  • void 函数使用 auto func()->type 后置返回类型, 便于一眼看到.
  • 全局数组较少, 更多使用 new 数组.
  • 平时写代码会 delete, 赛时为运行速度而不手动释放, 避免卡常.
  • for 使用 x y z 作为循环变量.
  • main() 用于多测, 主函数是 solve(), 写特殊性质时会调用 solve_testcase777() 而不是 solve(777).
  • yy 喵用于累计和或记录前缀和 / 差分 (前缀 \(\operatorname{xor}\) 也是前缀和), ct 喵用于记录临时答案. // 这个是一个隐藏的信息, 如果你懂, 当然能看出来代表的含义 (非键政).
  • f 用于记录 dp 数组.
  • jv 用于记录答案, jv 是我嫂子 \(\texttt{\color{black}{j}\color{red}{jhvic250}}\) 喵. // 我哥 \(\texttt{\color{grey}{3f}}\) 连绿题都切不了, 我嫂子能切黑题, 所以用我嫂子的 id 保佑代码正常运行得到 ac.

以下是 problem:洛谷-P2418 的代码.

#include<bits/stdc++.h>	// 万能头喵
using namespace std::string_literals;
using lf=double;
using ll=std::ptrdiff_t;
using ull=std::size_t;
using us=unsigned;
auto	nxt_lf(void)->lf{lf x;std::cin>>x;return x;}	// Orz 无用的快读
auto	nxt_ll(void)->ll{ll x;std::cin>>x;return x;}
auto	nxt_ull(void)->ull{ull x;std::cin>>x;return x;}
auto	nxt_us(void)->us{us x;std::cin>>x;return x;}
constexpr ll inf=0x3f3f3f3f3f3f3f3fl;
constexpr ll mod=0x3b800001l;
/*	// 记录题目链接和思考时间, 编码时间.
prob link:	https://vjudge.net/problem/%E6%B4%9B%E8%B0%B7-P2418
start think:	dec 14th 2025, 6:30
term think:	dec 14th 2025, 6:48
start code:	dec 14th 2025, 6:48
term code:	dec 14th 2025, 7:19
*/
class	tree{	// class 记录 ds 喵
	tree *ld,*rd;
	ll f,g,v;
public:
	tree(ll,ll);
	~tree(void);
	void push_up(void);
	void push_down(void);
	void update(ll,ll);
	auto query(ll,ll)->ll;
};
tree::tree(ll p,ll q){
	ld=rd=nullptr;
	f=p,g=q,v=inf;
}
tree::~tree(void){
	if(ld!=nullptr)
		delete ld,
		ld=nullptr;
	if(rd!=nullptr)
		delete rd,
		rd=nullptr;
}
void	tree::push_up(void){
	v=std::min(ld->v,rd->v);
}
void	tree::push_down(void){
	if(ld==nullptr)
		ld=new tree(f,f+g>>1);	// 根据 man operator, 先 + 后 >> 喵
	if(rd==nullptr)
		rd=new tree((f+g>>1)+1,g);
}
void	tree::update(ll x,ll k){
	if(f==g){
		v=std::min(v,k);
		return;
	}	push_down();
	if(x<=ld->g)
		ld->update(x,k);
	else	rd->update(x,k);
	push_up();
}
auto	tree::query(ll p,ll q)->ll{	// auto 后置返回值
	if(p<=f&&g<=q)
		return v;
	push_down();
	ll ct=inf;
	if(p<=ld->g)
		ct=std::min(ct,ld->query(p,q));
	if(rd->f<=q)
		ct=std::min(ct,rd->query(p,q));
	return ct;
}
void	solve(void){
	ll n=nxt_ll(),m=nxt_ll();	// 依旧无用快读
	ll *a=new ll[n+1]();	// 申请数组
	std::generate(a+1,a+1+n,nxt_ll);	// 这个函数可以按顺序给 [begin,end) 赋值为调用传入的函数
	ll *yy[2];	// 记录前缀和喵
	yy[0]=new ll[n+1]();
	yy[1]=new ll[n+1]();
	ll *ct=new ll[n+1]();	// 记录临时答案喵
	ll *f=new ll[n+1]();
	for(ll x=1;x<=n;x++){	// 使用 x 作为循环变量
		yy[0][x]=yy[0][x-1]+(a[x]==1);
		yy[1][x]=yy[1][x-1]+(a[x]==2);
		ct[x]=yy[1][x]-yy[0][x];
		f[x]=inf;
	}	ll jv=inf;	// 记录重要信息
	tree *rt=new tree(-n,n);
	rt->update(0,0);
	for(ll x=1;x<=n;x++){
		if(a[x]!=a[x-1])
			jv=f[x-1];
		ll p=ct[x]-m,q=ct[x]+m;
		f[x]=std::min(jv,rt->query(p,q))+1;
		rt->update(ct[x],f[x]);
		jv=std::min(jv,f[x]);
	}	std::cout<<f[n]<<"\n";
	delete[] a;	// 要记得释放
	delete[] yy[0];
	delete[] yy[1];
	delete[] ct;
	delete[] f;
	delete rt;
}
auto	main(void)->signed{
//	freopen(".in","r",stdin);
//	freopen(".out","w",stdout);
	std::ios::sync_with_stdio(0);	// 关同步
	std::cin.tie(nullptr);
	std::cout.tie(nullptr);
	ll _=1;	// 处理多测
//	_=nxt_ll();
	while(_--)
		solve();
	return 0;
}
posted @ 2025-12-14 08:22  young_tea  阅读(18)  评论(0)    收藏  举报