数据结构笔记

一、链表

1、引入

考虑一个问题:一个排好序的数组,你要如何动态地插入和删除数字?
正常数组的问题:拿一个total变量存数据个数(数组内有效长度)每次插入要O(n)地挪动,删除也是O(n)地挪动,还有可能数据太多塞不下了
现在引入单链表:用的是结构体o
但是链表访问必须从头开始,查找也不能二分查找
O(1)删除结点a,现在只知道结点a的位置不知道a之前的:
把a后面的数据拷贝过来,然后删除a!!!!救救

另有:
双链表:知道head,tail
循环链表:最后一个的tail是第一个的地址

2、例题

luoguP1996约瑟夫

在这里插入图片描述

#include<iostream> 
#include<cstring>
using namespace std;
int n,m,cnt = 1;
struct Node{
	int data;
	Node *next;
}node[105];

int main(){
	cin>>n>>m;
	for(int i=1;i<n;i++){
		node[i].data = i;
		node[i].next = &node[i+1];
	}
	node[n].data = n;
	node[n].next = &node[1];
	Node *now = &node[1];//为什么不初始化!!!!!!
	while(now){
		while(cnt<m-1){//这里用m-1是为了删除结点方便一点
			now = now->next ;//指针用->
			cnt++;
		}
		cnt = 1;
		Node *tmp = now->next;
		now->next = tmp->next;
		cout<<tmp->data <<" ";
		tmp->next = NULL;
		now = now->next ;
	}//这里不需要再加最后一个结点的输出,因为最后一步的时候是最后一个人自己指向自己,弄完now=NULL
	return 0;
}
luoguP1160队列安排

在这里插入图片描述

AC代码:

#include<iostream>
#include<algorithm> 
using namespace std;
struct Node{
	int data;
	Node *last,*next;
}node[100005];
int n,m,x;
int a[100005] = {0};

int main(){
	cin>>n;
	int k,p;
	Node *head;
	head = &node[1];
	node[1].data = 1;
	node[1].last = NULL;
	node[1].next = NULL;
	for(int i=2;i<=n;i++){
		cin>>k>>p;
		node[i].data = i;
		node[i].last = NULL;
		node[i].next = NULL;
		if(p==0){//左边 
			if(head == &node[k]) head = &node[i];
			node[i].next = &node[k];
			if(node[k].last){
				(node[k].last)->next = &node[i];
				node[i].last = node[k].last ;
			}
			node[k].last = &node[i];
		}
		if(p==1){//右边 
			node[i].last = &node[k];
			if(node[k].next){
				node[i].next = node[k].next ;
				(node[k].next)->last = &node[i];
			}
			node[k].next  = &node[i];
		}
	}
	cin>>m;

	for(int i=1;i<=m;i++){
		cin>>x;
		if(node[x].data==0) continue;
		else node[x].data = 0;
		if(node[x].last)
			(node[x].last)->next = node[x].next;
		if(node[x].next)
			(node[x].next)->last = node[x].last;
	} 
	cout<<head->data;
	head = head->next;
	while(head){
		cout<<" "<<head->data;
		head = head->next ;
	}
	cout<<endl;
	return 0;
} 

又是RE又是WA又是TLE的,搞了好久才过

  • RE:删除时,if(node[x].last) (node[x].last)->next = node[x].next; if(node[x].next) (node[x].next)->last = node[x].last;两句忘记写 if 判断,导致runtime error,没有给指针赋值就直接用了,出了问题(其实我不知道不赋值会不会自动设定为NULL,所以全部给初始化了一遍为NULL)
  • WA:删除时去重写错了,continue和break用错了
  • TLE:没有意识到这里可以用node[x]表示data为x的结点,导致每次都从链表头开始查,浪费了很多时间,再加上一开始没有去重,就更麻烦了
  • 注意:struct里面可以不必要写Node *last,*next;因为可以写Node L,R;然后用node[L],node[R]找到前后的结点——注意这题的特殊性,可以用node数组的下标代替当前的这个人。
ACMOJ 1085简单链表(会卡时间

在这里插入图片描述

AC代码:

#include<iostream>
using namespace std;
const int N = 100005;
int n,m,x,y,opt;
int len = 0;

struct Node{
	int data;
	Node *next;
	Node(int k){
		data = k;
		next = NULL;
	}
};

Node *head,*tail;

void create(int k){//attention here!!!!减小常数,建立链表的时候不要一遍一遍搜,你明知它是反着建立的,为什么不直接搞个tail指针依次往后移呢,直接减小常数
	Node *tmp = new Node(k);
	if(!tail){
		head = tail = tmp;
	}
	else {
		tail->next = tmp;
		tail = tmp;
	}
}

Node *move(int k){
	Node *idx = head;
	while(k){
		idx = idx->next ;
		k--;
	}
	return idx;
}

void push(int x,int y){
	Node *tmp = new Node(y);
	if(x==0){
		tmp->next = head;
		head = tmp;
	}
	else {
		Node *find = move(x-1);
		tmp->next = find->next;
		find->next = tmp;
	}
}

void pop(int t){
	Node *tmp; 
	if(t == 1) {
		tmp = head;
		head = head->next;
	}
	else {
		Node *target = move(t-2);//head往后t-1个 
		tmp = target->next;
		target->next = tmp->next;
	}
	delete tmp;
}

void print(){
	Node *p = head;
	while(p){
		cout<<p->data<<" ";
		p = p->next;
	}
	cout<<endl;
}

void clear(){
	Node *p = head,*q;
	while(p){
		q = p;
		p = p->next;
		delete q;
	}
}

int main(){
	cin>>n>>m;
	for(int i=1;i<=n;i++){
		cin>>x;
		create(x);
	}
	for(int i=1;i<=m;i++){
		cin>>opt;
		if(opt == 1){
			cin>>x>>y;
			push(x,y);
		}
		if(opt == 2){
			cin>>x;
			pop(x); 
		}
	}
	print();
	clear();
	return 0;
}

二、栈、队列

单调栈

P1901发射站

解决:最近的大于该元素的元素
在这里插入图片描述

AC代码:

#include<iostream>
using namespace std;
const int N = 1000005;
int h[N] = {0},v[N] = {0};
int l[N] = {0},r[N] = {0};
int q[N] = {0};
int w[N] = {0};
int n,ans = 0;
int head = 0;

int main(){
	cin>>n;
	for(int i=1;i<=n;i++){
		cin>>h[i]>>v[i];
	}
	q[++head] = 1;
	l[1] = 0;
	//q单减栈
	for(int i=2;i<=n;i++){
		while(h[q[head]]<=h[i] && head!=0){
			w[i] += v[q[head]];
			head--;
		}
		q[++head] = i;
		w[q[head-1]] += v[i];
	} 
	
	for(int i=1;i<=n;i++){
		ans = max(ans,w[i]);
	}
	cout<<ans<<endl;
	return 0;
}

20009灌溉农田

在这里插入图片描述

这题没说清楚……其实就是会被高于所在位置的田地挡住,意思是可以等但不能高于
AC代码:

#include<iostream>
using namespace std;
const int N = 1000005;
int n;
int h[N] = {0},v[N] = {0};
int q[N] = {0};
int l[N] = {0},r[N] = {0};
int head = 0,ans = 0,idx = 0;

int main(){
	cin>>n;
	for(int i=1;i<=n;i++){
		cin>>h[i];
	}
	for(int i=1;i<=n;i++){
		cin>>v[i];
	}
	for(int i=1;i<=n;i++){//单调不增!栈  果然是单调不增的问题 
		while(head!=0 && h[q[head]]<h[i]){
			r[q[head]] = i;
			head--;
		}
		if(h[i] == h[q[head]])
			l[i] = l[q[head]];
		else l[i] = q[head];
		q[++head] = i;
	}
	while(head){
		r[q[head]] = n+1;
		head--;
	}
	for(int i=1;i<=n;i++){
		int tmp = v[i]*(r[i]-l[i]-1);
		if(ans<tmp) {
			idx = i;
			ans = tmp;
		}
	}
	cout<<idx-1<<" "<<ans<<endl;
	return 0;
}

单调队列

板子题滑动窗口

#include<iostream>
using namespace std;
const int N = 1000005;
int a[N],q[N] = {0};
int n,k;
int head = 1,tail = 0;//专门搞队列的 

int main(){
	cin>>n>>k;
	for(int i=1;i<=n;i++){
		cin>>a[i];
	}
	for(int i=1;i<=n;i++){
		if(i<=k) {//q存最小值的下标 
			while(tail!=0 && a[q[tail]]>=a[i]){
				tail--;
			}
			q[++tail] = i;
		}
		else {
			cout<<a[q[head]]<<" ";
			if(q[head]<=i-k) head++;//注意,如果队列里面只剩下一个元素,这个元素一定是i-1号元素 
			while(a[q[tail]]>=a[i] && tail>=head){
				tail--;
			}
			q[++tail] = i;
		}
	} 
	cout<<a[q[head]]<<endl;
	head = 1;
	tail = 0;
	for(int i=1;i<=n;i++){
		if(i<=k) {//q存最大值的下标 
			while(tail!=0 && a[q[tail]]<=a[i]){
				tail--;
			}
			q[++tail] = i;
		}
		else {
			cout<<a[q[head]]<<" ";
			if(q[head]<=i-k) head++;//注意,如果队列里面只剩下一个元素,这个元素一定是i-1号元素 
			while(a[q[tail]]<=a[i] && tail>=head){
				tail--;
			}
			q[++tail] = i;
		}
	} 
	cout<<a[q[head]]<<endl;
	return 0;
}

三、哈夫曼树

在这里插入图片描述

写了n叉哈夫曼树的代码(MLE了,因为其实不需要建树)

#include<iostream>
using namespace std;
const int N = 100005;
long long a,n,m;
long long sum = 0;

struct Node{//这个是建好的哈夫曼树 
	long long data;
	Node **son;
	Node(int x,int sonnum){ 
		data = x;
		son = new Node *[sonnum+1];//左闭右开啊啊啊啊啊啊! 
		for(int i=1;i<=sonnum;i++){
			son[i] = NULL;
		}
	}
	~Node(){
		delete []son;
	}
};
Node *root;
Node *heap[2*N];//这个是堆!!! 小顶堆 
int sizze = 0;//是堆的sizze!! 

void push(Node *x){
	int hole = ++sizze;
	for( ;hole>1 && heap[hole/2]->data > x->data;hole/=2)
		heap[hole] = heap[hole/2];
	heap[hole] = x;
}

Node *pop(){
	Node *ans = heap[1];
	Node *tmp = heap[sizze--];
	heap[1] = tmp;
	int hole = 1;
	int child;
	for( ;hole*2<=sizze;hole=child){
		child = hole*2;
		if(child+1<=sizze && heap[child]->data > heap[child+1]->data)
			child++;
		if(tmp->data > heap[child]->data) heap[hole] = heap[child];
		else break;
	}
	heap[hole] = tmp;
	return ans;
}

void traverse(Node *rt,int k){
	if(rt->son[1] == NULL) {
		sum += k*rt->data ;
		return;
	}
	for(int i=1;i<=m;i++){
		traverse(rt->son[i],k+1);
	}
	return ;
}

int main(){
	cin>>n>>m;
	int cnt = (1-n)%(m-1);
	while(cnt<0) cnt += m-1;
	for(int i=1;i<=cnt;i++){
		Node *tmp = new Node(0,m); 
		push(tmp);
	}
	for(int i=1;i<=n;i++){
		cin>>a;
		Node *tmp = new Node(a,m);
		push(tmp);
	}
		
	cnt += n-m;
	cnt = cnt/(m-1)+1;
	for(int i=1;i<=cnt;i++){
		Node *rt = new Node(0,m); 
		for(int j=1;j<=m;j++){
			Node *tmp = pop();
			rt->son[j] = tmp;
			rt->data += tmp->data ;
		}
		push(rt);
		root = rt;
	}
	traverse(root,0);
	cout<<sum<<endl;
	return 0;
}

AC代码:

#include<iostream>
using namespace std;
const int N = 100005;
long long a,n,m;
long long sum = 0;

int root;
long long heap[2*N+1];//这个是堆!!! 小顶堆 
int sizze = 0;//是堆的sizze!! 

void push(long long x){
	int hole = ++sizze;
	for( ;hole>1 && heap[hole/2] > x;hole/=2)
		heap[hole] = heap[hole/2];
	heap[hole] = x;
}

long long pop(){
	long long ans = heap[1];
	long long tmp = heap[sizze--];
	heap[1] = tmp;
	int hole = 1;
	int child;
	for( ;hole*2<=sizze;hole=child){
		child = hole*2;
		if(child+1<=sizze && heap[child] > heap[child+1])
			child++;
		if(tmp > heap[child]) heap[hole] = heap[child];
		else break;
	}
	heap[hole] = tmp;
	return ans;
}

int main(){
	cin>>n>>m;
	int cnt = m-2-(n-2)%(m-1);
	
	for(int i=1;i<=cnt;i++){ 
		push(0);
	}
	for(int i=1;i<=n;i++){
		cin>>a;
		push(a);
	}
		
	cnt += n-m;
	cnt = cnt/(m-1)+1;
	while(sizze>1){
		long long tmp = 0;
		for(int j=1;j<=m;j++){
			tmp += pop() ;
		}
		push(tmp);
		sum += tmp;
	}
	cout<<sum<<endl;
	return 0;
}

Tips:

  • 说了答案在long long范围内,所以要用long long存 (少量WA)
  • 小心开空间的时候是左闭右开,有时候需要多开一点
  • 写的时候RE了,发现是heap开小了,要开大点,也不知道是本来就要开大一点还是数据的问题 (RE)
  • heap里面存什么?如果你要建出来哈夫曼树,就在heap里面存结点类型,如果只是用个数据,可以不建树而达成目的,就可以直接存数据类型在里面!
  • 算补0的时候遇到了计算式的问题,发现如下两种写法竟然得到一样的结果……
#include<iostream>
using namespace std;
int n,m;

int main(){
	while(cin>>n){
		cin>>m;
		cout<<m-2-(n-2)%(m-1)<<endl;
		int cnt = (1-n)%(m-1);
		while(cnt<0) cnt += m-1;
		cout<<cnt<<endl;
	}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值