8.22集训笔记

发布时间 2023-08-23 12:09:39作者: HelloHeBin

上午

点击查看代码
#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;
}

下午