首页 > 其他分享 >8.22集训笔记

8.22集训笔记

时间:2023-08-23 12:11:08浏览次数:54  
标签:Node nxt int 笔记 链表 pa 8.22 NULL 集训

上午

点击查看代码
#include<bits/stdc++.h>
using namespace std;
const int N=2e5+10;
int n,m,c,a[N],st[N],ans;

void sol1_70(){ // O(n*2)  TLE  time limit E
    for(int i=1; i<=n; i++){
        for(int j=1; j<=n; j++){
            if(a[i] - a[j] == c) ans ++;
        }
    }
}
void sol2_70(){ // o(n) RE  runtime error
    // st[i] 表示 i 的个数 
    for(int i=1; i<=n; i++) st[ a[i] ] ++;
    for(int i=1; i<=n; i++) {
        int b = a[i]-c;
        ans += st[ b ];
    }
}
// map<int,int> mp;  mp[x] = y;
void sol3_100(){
    map<int,int> mp;
    for(int i=1; i<=n; i++) mp[ a[i] ] ++;
    for(int i=1; i<=n; i++) {
        int b = a[i]-c;
        ans += mp[ b ];
    }
} 
void sol4_100(){
    sort(a+1, a+1+n);
    for(int i=1; i<=n; i++){
        int b = a[i] - c;
        int l = lower_bound(a+1, a+1+n, b) - a; // >= b 的第一个元素位置 
        int r = upper_bound(a+1, a+1+n, b) - a; // > b  的第一个元素位置 
//        bool f = binary_search(a+1, a+1+n, b);  // 返回 b 是否存在 
        ans += r-l; 
    }
}
int main(){
    cin>>n>>c;
    for(int i=1; i<=n; i++) cin>>a[i];
    sol4_100();
    cout<<ans;
    return 0;
}
  • 指针
点击查看代码
#include<bits/stdc++.h>
using namespace std;
const int N=110;

int a = 10;
int* point_a = &a; // 指针 
double b = 123.4;
double *pb = &b;
int * pc = NULL;  // 空指针 

int main(){
    cout<<a<<endl;    // 10
    cout<<&a<<endl;   // 0x3f3f3f3f
    cout<<point_a<<endl; // 0x3f3f3f3f
    cout<<*point_a<<endl; // 0x3f3f3f3f

    cout<<b<<endl;    // 10
    cout<<&b<<endl;   // 0x3f3f3f3f
    cout<<pb<<endl;   // 0x3f3f3f3f
    cout<<*pb<<endl;   // 0x3f3f3f3f

    cout<<pc<<endl; 
    return 0;
}
  • 单向链表
点击查看代码
#include<bits/stdc++.h>
using namespace std;
const int N=1e5+10;

struct Node{
    int data;
    Node *nxt;
}a[N];
int main(){
    a[1] = {11, NULL};
    a[2] = {12, NULL};
    a[3] = {13, NULL};
//  需求:建立链表:11 -> 13 -> 12 -> NULL; // NULL 表示 0,空,没有 
//  结构体元素使用 . 访问
//  指针元素使用  -> 访问  
    Node *pa = &a[1];  // pa 等效于 &a[1] 
    a[1].nxt = &a[3];
    a[3].nxt = &a[2];
//    a[2].nxt = &a[1]; 加上后变为循环链表 
    
    while(pa != NULL){
//        cout<<(*pa).data<<endl; // (*pa) 等效于 a[1] 
        cout<<pa->data<<endl;
        pa = pa->nxt;
    }
    return 0;
}


  • 双向链表
点击查看代码
#include<bits/stdc++.h>
using namespace std;
const int N=1e5+10;

struct Node{
    int data;
    Node *pre, *nxt;
}a[N];
int main(){
    a[1] = {11, NULL, NULL};
    a[2] = {12, NULL, NULL};
    a[3] = {13, NULL, NULL};
//    需求:建立链表:11 - 13 - 12 - NULL; // NULL 表示 0,空,没有 
//  结构体元素使用 . 访问
//  指针元素使用  -> 访问  
    Node *pa = &a[1];  // pa 等效于 &a[1]
    Node *pb = &a[2];  // pa 等效于 &a[1]
    a[1].nxt = &a[3];
    a[3].nxt = &a[2];

    a[2].pre = &a[3];
    a[3].pre = &a[1];
    while(pa != NULL){
//        cout<<(*pa).data<<endl; // (*pa) 等效于 a[1]
        cout<<pa->data<<endl;
        pa = pa->nxt;
    }
    while(pb != NULL){
        cout<<pb->data<<endl;
        pb = pb->pre;
    }
    return 0;
}
  • 队列
点击查看代码
#include<bits/stdc++.h>
using namespace std;
const int N=1e6+10;
// 数组模拟队列 
int que[N], front=0, tail=-1; // if(front > tail) 为空
int main(){
// 1-7 入队
//    for(int i=1; i<=7; i++) que[ ++tail ] = i;
//    for(int i=front; i<=tail; i++){
//        cout<<que[i]<<" ";
//    }

    queue<int> q;  // STL 的队列
    for(int i=1; i<=7; i++) q.push(i);
//    while(!q.empty()){
    while(q.size()){
        cout<<q.front()<<" ";
        q.pop();
    }
    return 0;
}

下午

标签:Node,nxt,int,笔记,链表,pa,8.22,NULL,集训
From: https://www.cnblogs.com/hellohebin/p/17650865.html

相关文章

  • Python基础入门学习笔记 070 GUI的终极选择:Tkinter7
    实例1:添加Tags1fromtkinterimport*23root=Tk()4text=Text(root,width=30,height=5)5text.pack()67#INSERT索引表示插入光标当前的位置8text.insert(INSERT,"IloveFishC.com!")#光标当前的位置插入9#注意,行号从1开始,列号则从0开始10text.ta......
  • 8.22普及模拟三
    \(\large{T1\最大生成树}\\\tiny{30pts}\)题意:给定一个点权为\(a_i\)边权为\(|a_i-a_j|\)的完全图,求这个图的最大生成树部分分:30~50pts:Kruskal求最长路100pts:贪心,设权值最大点为\(a_{max}\),权值最小点为\(a_{min}\)则\(max(|a_{max}-a_i|,|a_{min}-a_i|)\)一定......
  • Python基础入门学习笔记 067 GUI的终极选择:Tkinter4
    实例1:1fromtkinterimport*23root=Tk()#创建主窗口4e=Entry(root)#在主窗口中插入输入框5e.pack(padx=20,pady=20)67e.delete(0,END)#清空输入框8e.insert(0,"默认文本...")#设置输入框内容910mainloop() 实例2:1fromtkinterimp......
  • Python基础入门学习笔记 068 GUI的终极选择:Tkinter5
    Listbox组件如果需要提供选项给用户选择,单选可以用Radiobutton组件,多选可以用Checkbutton,如果提供的选项非常多,可以考虑使用Listbox组件。Listbox是以列表的形式显示出来,并支持滚动条操作。实例1:1fromtkinterimport*23root=Tk()#创建主窗口45theLB=Listb......
  • Python基础入门学习笔记 069 GUI的终极选择:Tkinter6
    Text组件Text(文本)组件用于显示和处理多种任务。虽然该组件的主要目的是显示多行文本,但它常常也被用于作为简单的文本编辑器和网页浏览器使用。实例1:插入内容1fromtkinterimport*23root=Tk()4text=Text(root,width=30,height=2)5text.pack()6#INSERT......
  • [树状数组] 学习笔记
    原理intlowbit(intx){ returnx&(-x);}voidadd(intx,intk){ for(;x<=n;x+=lowbit(x))c[x]+=k;}intquery(intx){ intans=0; for(;x;x-=lowbit(x))ans+=c[x]; returnans;}单点修改+区间查询intlowbit(intx){ returnx&......
  • Python基础入门学习笔记 066 GUI的终极选择:Tkinter3
    实例1:Checkbutton组件1fromtkinterimport*23root=Tk()4#需要一个Tkinter变量,用于表示该按钮是否被选中5v=IntVar()6c=Checkbutton(root,text="测试一下",variable=v)78c.pack()9#如果被选中,那么变量v被赋值为1,否则为010#可以用个Label......
  • Vue学习笔记:Pinia Part02 Store
    在Pinia中有option和setup两种写法OptionStore与Vue的选项式API类似,我们也可以传入一个带有 state、actions 与 getters 属性的Option对象exportconstuseCounterStore=defineStore('counter',{state:()=>({count:0}),getters:{double:(state)......
  • Python基础入门学习笔记 064 GUI的终极选择:Tkinter
    >>>importtkinter #Tkinter是python默认的GUI库,导入Tkinter模块>>> 实例1:1importtkinterastk23root=tk.Tk()#创建一个主窗口,用于容纳整个GUI程序4root.title("FishCDemo")#设置主窗口对象的标题栏56#添加一个Label组件,可以显示文本、图标或者图......
  • Python基础入门学习笔记 065 GUI的终极选择:Tkinter2
    实例1:Label组件显示文字与gif图片1#导入tkinter模块的所有内容2fromtkinterimport*34#创建主窗口5root=Tk()6#创建一个文本Label对象,文字为左对齐,离左边边框距离为107textLabel=Label(root,8text="您下载的影片含有未成年人......