第 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;
}
相关推荐
学长毕设5 小时前
基于SpringBoot的实验室资产管理系统
java·spring boot·后端
zzu123zsw6 小时前
从臃肿的模板调用到声明式契约:深入 UniHttp 动态代理与多渠道生命周期扩展
java·spring boot·microsoft·unihttp
UIU1147 小时前
P1233 [ICPC 2001 Taejon R] 木棍加工
数据结构·c++·算法·动态规划
源码集结号7 小时前
云诊所门诊系统源码,基于SpringBoot+Vue.js架构,支持多租户设计与Docker容器化部署
java·spring boot·源码·医院信息系统·门诊系统·诊所系统
论文复现现场7 小时前
WRF、FEA 能用 L40S 加速吗?求解器检查、断点恢复与长期运行
java·数据库·算法·wrf·有限元分析·gpu计算·断点续算
ZhengEnCi7 小时前
用 AI 写代码为什么拼到最后还是拼人:算力瓶颈与领域专家提示词的本质差异
算法·架构
啃瓜子的松鼠7 小时前
泛微OAe9-后端二开异步调用
java
秋天的怀念_David8 小时前
第二十一夜 · 数字黑洞与西西弗斯的惩罚
java·程序人生·算法
外收内放8 小时前
Python基础语法练习题(73)
java·前端·python
Wang's Blog8 小时前
Java 项目实战: 交友项目 - 环信后台发送消息测试
java·交友