codeforce 899E(线段树/链表+优先队列)

本文介绍了两种解决最大重复序列删除问题的方法。一种是利用线段树进行高效维护和更新,另一种则是采用优先队列实现。文章详细解释了两种方法的具体实现步骤,并提供了完整的代码示例。

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

这题做完之后整场比赛就AK了(虽然是赛后),感觉赛后得把题全做了比赛才会更有意义撒。。

题意为找最大重复序列并依次删除。。

解法一:在汪聚聚的提醒下才发现其实用线段树就能实现啊。。orz。。维护的东西有点多,实现起来的难度比想象的要小很多,连pushdown都没有,果然CF是注重想法的。。

update时考虑维护最长区间,能够产生最长区间的有三种情况 ,做子树的最长区间,右子树的最长区间以及左子树右端重复序列和右子树左端重复序列合并起来的序列,因此需要维护左右端的重复序列。。然后考虑到序列中间有空缺。。所以需要记录长度去比较(我竟然用傻叉到用区间长度去比较。。。)然后注意一下最长区间要从左到右取就可以了。。

解法二:然后不小心看到了别人用STL的做法。。然后发现自己真的是智障。。有优先队列不用却去手写数据结构。。有个现成的堆用多好啊。。。先附上链接:

http://blog.youkuaiyun.com/yxm980918/article/details/78887737

其实有想过用平衡树实现。。不过说到平衡树我就不想写了。。所以思路是能避开就避开。。这个模拟链表之后其实就只剩找最大区间了。。用优先队列去找就可以。。

然后优先队列其实自己并不擅长。。在做题的过程中被卡了好多次均因为不熟悉STL所致。。优先队列里面最好是不能放指针的,一个是因为如果指针指向内容改变,那么优先队列是不会再排序的,这个参照堆的维护过程是不难想象的。。所以在指向内容改变的时候还要再入队。。然后问题又来了。。入队的过程中如果按照堆的路径不巧碰到了原来的指针,那么原指针的内存也相应改变,因此可能就会停在原指针后面。。(orz..膜拜造这组数据的神犇


解法一:

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<queue>
#include<cmath>
#define inc(i,l,r) for(int i=l;i<=r;i++)
#define dec(i,l,r) for(int i=l;i>=r;i--)
#define link(x) for(edge *j=h[x];j;j=j->next)
#define inf 1e9
#define eps 1e-8
#define mem(a) memset(a,0,sizeof(a))
#define ll long long
#define succ(x) (1<<x)
#define lowbit(x) (x&(-x))
#define sqr(x) ((x)*(x))
#define NM 200005
#define pi 3.141592653
using namespace std;

int read(){
	int x=0,f=1;char ch=getchar();
	while(!isdigit(ch)){if(ch=='-')f=-1;ch=getchar();}
	while(isdigit(ch))x=x*10+ch-'0',ch=getchar();
	return f*x;
}


int n,s,_x,_y;
struct info{
	int size,l,r,_l,_r,s,ls,rs,lt,rt;
	info operator+(const info&o){
		info f;f.size=size+o.size;
		if(size==0){
			if(o.size==0)return f;
			return o;
		}
		if(o.size==0)return *this;
		f.l=l;f.r=r;f.s=s;
		if(rt==o.lt&&f.s<o.ls+rs)f.l=_r,f.r=o._l,f.s=o.ls+rs;
		if(o.s>f.s)f.l=o.l,f.r=o.r,f.s=o.s;
		f._l=_l;f._r=o._r;f.lt=lt;f.rt=o.rt;f.ls=ls;f.rs=o.rs;
		if(_l>=_r&<==o.lt)f._l=o._l,f.ls+=o.ls;
		if(o._l>=o._r&&o.rt==rt)f._r=_r,f.rs+=rs;
		return f;
	}
}T[4*NM];




void build(int i,int x,int y){
	int t=x+y>>1;
	if(x==y){
		T[i].size=T[i].s=T[i].ls=T[i].rs=1;
		T[i].l=T[i].r=T[i]._l=T[i]._r=x;
		T[i].lt=T[i].rt=read();
		return;
	}
	build(i<<1,x,t);build(i<<1|1,t+1,y);
	T[i]=T[i<<1]+T[i<<1|1];
}

void mod(int i,int x,int y){
	int t=x+y>>1;
	if(y<_x||_y<x||!T[i].size)return;
	if(_x<=x&&y<=_y){T[i].size=0;return;}
	mod(i<<1,x,t);mod(i<<1|1,t+1,y);
	T[i]=T[i<<1]+T[i<<1|1];
}

int main(){
	//freopen("data.in","r",stdin);
	n=read();
	build(1,1,n);
	while(T[1].size){
		_x=T[1].l;_y=T[1].r;
		mod(1,1,n);
		s++;
	}
	printf("%d\n",s);
	return 0;
}


解法二:

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<queue>
#include<cmath>
#define inc(i,l,r) for(int i=l;i<=r;i++)
#define dec(i,l,r) for(int i=l;i>=r;i--)
#define link(x) for(edge *j=h[x];j;j=j->next)
#define inf 1000000007
#define eps 1e-8
#define mem(a) memset(a,0,sizeof(a))
#define ll long long
#define succ(x) (1<<x)
#define lowbit(x) (x&(-x))
#define sqr(x) ((x)*(x))
#define NM 200005
#define nm 800005
#define pi 3.141592653
using namespace std;
int read(){
    int x=0,f=1;char ch=getchar();
    while(!isdigit(ch)){if(ch=='-')f=-1;ch=getchar();}
    while(isdigit(ch))x=x*10+ch-'0',ch=getchar();
    return f*x;
}






int n,_x,cnt,pre[NM],next[NM],c[NM],a[NM],ans;
struct tmp{
	int s,id;
	tmp(int s=0,int id=0):s(s),id(id){}
	bool operator<(const tmp&o)const{return s<o.s||(s==o.s&&id>o.id);}
};
priority_queue<tmp>q;
bool v[NM];
int main(){
	n=read();
	inc(i,1,n){
		_x=read();
		if(a[cnt]==_x)c[cnt]++;
		else{a[++cnt]=_x;c[cnt]=1;}
	}
	inc(i,1,cnt)q.push(tmp(c[i],i));
	inc(i,1,cnt)pre[i]=i-1,next[i]=i+1;
	while(cnt>0){
		tmp t=q.top();q.pop();
		while(v[t.id])t=q.top(),q.pop();
		int i=t.id,x=pre[i],y=next[i];
		if(a[x]==a[y]){
			next[x]=next[y];pre[next[y]]=x;
			c[x]+=c[y];v[y]++;q.push(tmp(c[x],x));cnt--;
		}else{
			next[x]=y;pre[y]=x;
		}
		ans++;v[i]++;cnt--;
	}
	printf("%d\n",ans);
	return 0;
}




E. Segments Removal
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Vasya has an array of integers of length n.

Vasya performs the following operations on the array: on each step he finds the longest segment of consecutive equal integers (the leftmost, if there are several such segments) and removes it. For example, if Vasya's array is[13, 13, 7, 7, 7, 2, 2, 2], then after one operation it becomes[13, 13, 2, 2, 2].

Compute the number of operations Vasya should make until the array becomes empty, i.e. Vasya removes all elements from it.

Input

The first line contains a single integer n (1 ≤ n ≤ 200 000) — the length of the array.

The second line contains a sequence a1, a2, ..., an (1 ≤ ai ≤ 109) — Vasya's array.

Output

Print the number of operations Vasya should make to remove all elements from the array.

Examples
Input
4
2 5 5 2
Output
2
Input
5
6 3 4 1 5
Output
5
Input
8
4 4 4 2 2 100 100 100
Output
3
Input
6
10 10 50 10 50 50
Output
4
Note

In the first example, at first Vasya removes two fives at the second and third positions. The array becomes[2, 2]. In the second operation Vasya removes two twos at the first and second positions. After that the array becomes empty.

In the second example Vasya has to perform five operations to make the array empty. In each of them he removes the first element from the array.

In the third example Vasya needs three operations. In the first operation he removes all integers4, in the second — all integers100, in the third — all integers 2.

In the fourth example in the first operation Vasya removes the first two integers10. After that the array becomes[50, 10, 50, 50]. Then in the second operation Vasya removes the two rightmost integers50, so that the array becomes[50, 10]. In the third operation he removes the remaining50, and the array becomes [10] after that. In the last, fourth operation he removes the only remaining10. The array is empty after that.


### Codeforces 测试数据生成方法 Codeforces 是一个流行的在线编程竞赛平台,支持多种方式来生成或提供测试数据。以下是关于如何在 Codeforces 平台上生成或提供测试数据的相关说明: #### 使用 `codeforces-parser` 自动生成测试数据 可以通过第三方工具如 `codeforces-parser` 来解析 Codeforces 的样例测试数据并自动生成测试文件。此工具可以从比赛页面提取样例输入和输出,并将其转换为可运行的测试用例[^1]。 安装和使用该工具的方法如下: ```bash pip install git+https://gitcode.com/gh_mirrors/co/codeforces-parser.git ``` 执行命令后,可以根据指定的比赛 ID 和题目编号下载对应的样例测试数据。例如: ```bash cfp --contest-id 1728 --problem A ``` 这会生成与问题 A 对应的输入和输出文件,便于本地调试和验证程序逻辑。 #### 手动创建自定义测试数据 如果需要更灵活的方式,则可以选择手动编写测试数据。通常情况下,可以按照以下结构准备输入文件: - **输入格式**: 遵循题目描述中的输入规范。 - **输出格式**: 根据预期的结果设计相应的输出内容。 假设有一道题目的输入形式如下所示: ```plaintext t (number of test cases) n_1 a_{1,1} ... a_{1,n_1} ... n_t a_{t,1} ... a_{t,n_t} ``` 那么可以构建类似的测试案例集用于验证算法实现是否正确。比如对于引用[3]提到的情况,即判断若干位数字组成的序列前缀匹配 π 值的问题,我们可以构造一些简单的例子作为起点[^3]: ```plaintext Input: 3 314 2718 1618 Output: 3 0 0 ``` 这里分别对应三个不同的测试情况——第一个完全吻合圆周率开头部分;第二个代表 e 数字串并不满足条件;最后一个接近黄金比例但也不符合条件。 #### 数据项的概念应用到测试数据的设计上 当考虑具体某个字段或者参数设置时,应当注意每一个单独的数据项都可能影响最终结果判定标准。正如引用[2]所指出那样,这些最小单位携带特定意义并且相互组合形成完整的记录条目[^2]。因此,在实际操作过程中要特别留意各组成部分之间的关系及其边界状况处理。 ```python def check_pi_prefix(s): pi_str = "3141592653589793238462643383279" max_match_length = min(len(pi_str), len(s)) for i in range(max_match_length + 1): if s[:i] != pi_str[:i]: return i - 1 return max_match_length # Example usage with custom inputs defined above. test_cases = ["314", "2718", "1618"] results = [check_pi_prefix(case) for case in test_cases] for result in results: print(result) ``` 上述 Python 函数实现了比较任意字符串同π数值前缀一致性的功能演示版本。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值