第 2 章 顺序表和 vector

  1. P3156 【深基15.例1】询问学号
cpp 复制代码
#include<iostream>
#include<vector>
using namespace std;
const int N = 2e6+5;
vector<int> a(N);
int main(){
    int n,m; cin >> n >> m;
    for(int i = 1;i<=n;i++) cin >> a[i];
    int x;
    while(m--){
        cin >> x;
        cout << a[x]<<endl;
    }
    return 0;
}
  1. P3613 【深基15.例2】寄包柜
cpp 复制代码
#include<iostream>
#include<vector>
using namespace std;
const int N = 1e5+5;
vector<int> a[N];
int main(){
    int n,q; cin >> n >> q;
    while(q--){
        int op,i,j,k;cin >> op >> i >> j;
        if(op == 1){
            cin >> k;
            if(a[i].size()<=j){
                 a[i].resize(j+1);
            }
            a[i][j] = k;
        }
        else cout << a[i][j]<< endl;
    }
    return 0;
}
    1. 移动零
cpp 复制代码
class Solution
{
public:
void sortColors(vector<int>& nums)
{
int n = nums.size();
int left = -1, right = n, i = 0;
while(i < right)
{
if(nums[i] == 0) swap(nums[++left], nums[i++]);
else if(nums[i] == 1) i++;
else swap(nums[--right], nums[i]);
}
}
};
    1. 颜⾊分类
cpp 复制代码
class Solution
{
public:
void sortColors(vector<int>& nums)
{
int n = nums.size();
int left = -1, right = n, i = 0;
while(i < right)
{
if(nums[i] == 0) swap(nums[++left], nums[i++]);
else if(nums[i] == 1) i++;
else swap(nums[--right], nums[i]);
}
}
};
  1. 合并两个有序数组
cpp 复制代码
class Solution
{
public:
void merge(vector<int>& nums1, int m, vector<int>& nums2, int n)
{
int cur1 = m - 1, cur2 = n - 1, cur = m + n - 1;
while(cur1 >= 0 && cur2 >= 0)
{
if(nums1[cur1] >= nums2[cur2]) nums1[cur--] = nums1[cur1--];
else nums1[cur--] = nums2[cur2--];
}
while(cur2 >= 0) nums1[cur--] = nums2[cur2--];
}
};
  1. The Blocks Problem
cpp 复制代码
#include <iostream>
#include <vector>
using namespace std;
const int N = 30;
typedef pair<int, int> PII;
int n;
vector<int> p[N]; // 创建 n 个放⽊块的槽
PII find(int x)
{
for(int i = 0; i < n; i++)
{
for(int j = 0; j < p[i].size(); j++)
{
if(p[i][j] == x)
{
return {i, j};
}
}
}
}
void clean(int x, int y)
{
// 把 [x, y] 以上的⽊块归位
for(int j = y + 1; j < p[x].size(); j++)
{
int t = p[x][j];
p[t].push_back(t);
}
p[x].resize(y + 1);
}
void move(int x1, int y1, int x2)
{
// 把 [x1, y1] 及其以上的⽊块放在 x2 上⾯
for(int j = y1; j < p[x1].size(); j++)
{
p[x2].push_back(p[x1][j]);
}
p[x1].resize(y1);
}
int main()
{
cin >> n;
// 初始化
for(int i = 0; i < n; i++)
{
p[i].push_back(i);
}
string op1, op2;
int a, b;
while(cin >> op1 >> a >> op2 >> b)
{
// 查找 a 和 b 的位置
PII pa = find(a);
int x1 = pa.first, y1 = pa.second;
PII pb = find(b);
int x2 = pb.first, y2 = pb.second;
if(x1 == x2) continue; // 处理不合法的操作
if(op1 == "move") // 把 a 上⽅归位
{
clean(x1, y1);
}
if(op2 == "onto") // 把 b 上⽅归位
{
clean(x2, y2);
}
move(x1, y1, x2);
}
// 打印
for(int i = 0; i < n; i++)
{
cout << i << ":";
for(int j = 0; j < p[i].size(); j++)
{
cout << " " << p[i][j];
}
cout << endl;
}
相关推荐
疯狂打码的少年2 小时前
【数据结构】顺序表 vs 链表的对比与选择
数据结构·笔记·链表
坚持编程的菜鸟10 小时前
模拟实现memmove
c语言·算法·模拟实现memmove
码农颜11 小时前
5.4.1 锁分类
java·数据库·mysql
wabs66611 小时前
关于图论【最短路径之Dijkstra算法(堆优化版)|卡码网47.参加科学大会的思考】
数据结构·算法·图论·优先级队列·邻接表·小顶堆·卡码网
坚持编程的菜鸟12 小时前
编写判断大小端程序
c语言·算法·判断大小端
papaofdoudou12 小时前
判断排列逆序奇偶性的乘积判别法(范德蒙德符号法)
人工智能·算法
linux-hzh12 小时前
百日算法修炼 · Day 03
java·算法
问商十三载13 小时前
RAG 检索效果差怎么排查?2026 五层诊断法完整指南
开发语言·人工智能·windows·python·算法
不负岁月无痕13 小时前
简单理解操作系统结构
java·linux·c语言·开发语言·c++·面试
mister_guo13 小时前
Java线程池参数应该如何设置(ThreadPoolExecutor)
java