这周看了分块、单调队列、哈希专题、树状数组看了一部分。
分块类的题有:常用于给定区间内进行的操作,或询问区间内满足给定条件的元素。
分块的思想:就是将一个集合划分成若干个规模较小的子集,对子集整体进行操作。
单调队列:也是堆或栈的操作,堆是先进先出,就像链表中的尾插法。栈是后进先出,就像链表中的头插法。
#define LENG sizeof(struct node)//节点所占的单元数
struct node//定义节点类型
{
int data;//data为整型
struct node *next;//next为指针类型
};
先进先出,问题:为什么要尾结点指向头节点?
truct node *creat1()
{
struct node *head,*tail,*p;//变量说明
int e;
head=(struct node *)malloc(LENG);//生成表头结点
tail=head;//尾指针指向表头
scanf("%d",&e);//输入一个数
//重要部分 (下)
while(e!=0)//不为零
{
p=(struct node *)malloc(LENG);//生成新结点
p->data=e;//装入输入的元素e
tail->next=p;//新节点连接到表尾
tail=p;//尾指针指向新结点
scanf("%d",&e);//再输入一个数
}
//重要部分(上)
tail->next=NULL;//尾节点的next置为空指针
return head;//返回头指针
};
后进先出
struct node *creat2()//生成“后进先出”单链表
{
struct node *head,*p;
int e;
head=(struct node *)malloc(LENG);//生成表头结点
head->next=NULL; //置为空表
scanf("%d",&e);//输入第一个数
//重要部分 (下)
while(e!=0)//不为零
{
p=(struct node *)malloc(LENG);//生成新结点
p->data=e;//输入数送新结点的data
p->next=head->next;//新结点指针指向原首结点
head->next=p;//表头结点的指针指向新结点
scanf("%d",&e);//再输入一个数
}
return head;//返回头指针
};
哈希可以用来判重。