博客
关于我
NC14661 简单的数据结构
阅读量:648 次
发布时间:2019-03-15

本文共 2091 字,大约阅读时间需要 6 分钟。

栗酱的数据结构题解

题目链接

数据结构题解

题意

栗酱在网上发现了一道有趣的数据结构题。题目描述了一个容器的操作,涉及七种不同的操作,需要模拟这些操作并输出结果。容器初始为空,操作包括从前面或后面插入、删除元素,以及头尾翻转、排序等功能。

操作说明

  • 从前面插入元素a
  • 从前面删除一个元素
  • 从后面插入一个元素a
  • 从后面删除一个元素
  • 将整个容器头尾翻转
  • 输出容器的个数和所有元素
  • 对所有元素进行从小到大排序
  • 解题思路

    该问题可以通过模拟容器的操作来实现。容器的操作涉及到头尾插入、删除以及整体翻转等操作。由于容器需要支持快速的插入、删除和翻转操作,可以选择使用双端队列(Deque)来模拟容器,因为双端队列支持O(1)时间复杂度的插入和删除操作。

    具体步骤如下:

  • 初始化双端队列。
  • 根据操作类型执行相应的操作。
  • 对于翻转操作,将双端队列中的元素进行交换。
  • 对于排序操作,将双端队列中的元素进行排序。
  • 对于输出操作,输出容器的大小和所有元素。
  • 参考实现

    #include 
    #include
    using namespace std;int main() { int n, m; long long sum = 0; cin >> n >> m; int index; deque
    dq; for(int i = 0; i < m; i++) { cin >> index; if(index == 1) { int tmp; cin >> tmp; dq.push_front(tmp); } else if(index == 2) { if(!dq.empty()) { dq.pop_front(); } } else if(index == 3) { int tmp; cin >> tmp; dq.push_back(tmp); } else if(index == 4) { if(!dq.empty()) { dq.pop_back(); } } else if(index == 5) { if(dq.size() <= 1) continue; int left = dq.begin(); int right = dq.end(); int mid = dq.end() - (dq.size() / 2); auto it = dq.begin(); while(it != mid) { auto temp = *it; *it = *mid; mid--; *mid = temp; it++; } } else if(index == 6) { cout << "Size: " << dq.size() << endl; if(!dq.empty()) { cout << "Elements: "; for(auto num : dq) { cout << num << " "; } cout << endl; } } else if(index == 7) { vector
    vec(dq.begin(), dq.end()); sort(vec.begin(), vec.end()); dq = deque(vec); } } return 0;}

    代码说明

    • 使用双端队列(deque)来模拟容器,支持O(1)时间复杂度的插入和删除操作。
    • 操作1和操作3分别从前面和后面插入元素。
    • 操作2和操作4分别从前面和后面删除元素。
    • 操作5实现头尾翻转,通过交换双端队列中相应位置的元素来实现。
    • 操作6输出容器的大小和所有元素。
    • 操作7将容器中的元素进行排序,使用内置的排序函数来实现。

    转载地址:http://pyhlz.baihongyu.com/

    你可能感兴趣的文章
    nova基于ubs机制扩展scheduler-filter
    查看>>
    Now trying to drop the old temporary tablespace, the session hangs.
    查看>>
    nowcoder—Beauty of Trees
    查看>>
    np.arange()和np.linspace()绘制logistic回归图像时得到不同的结果?
    查看>>
    np.power的使用
    查看>>
    NPM 2FA双重认证的设置方法
    查看>>
    npm build报错Cannot find module ‘webpack/lib/rules/BasicEffectRulePlugin‘解决方法
    查看>>
    npm build报错Cannot find module ‘webpack‘解决方法
    查看>>
    npm ERR! ERESOLVE could not resolve报错
    查看>>
    npm ERR! fatal: unable to connect to github.com:
    查看>>
    npm ERR! Unexpected end of JSON input while parsing near '...on":"0.10.3","direc to'
    查看>>
    npm ERR! Unexpected end of JSON input while parsing near ‘...“:“^1.2.0“,“vue-html-‘ npm ERR! A comp
    查看>>
    npm error Missing script: “server“npm errornpm error Did you mean this?npm error npm run serve
    查看>>
    npm error MSB3428: 未能加载 Visual C++ 组件“VCBuild.exe”。要解决此问题,1) 安装
    查看>>
    npm install CERT_HAS_EXPIRED解决方法
    查看>>
    npm install digital envelope routines::unsupported解决方法
    查看>>
    npm install 卡着不动的解决方法
    查看>>
    npm install 报错 EEXIST File exists 的解决方法
    查看>>
    npm install 报错 ERR_SOCKET_TIMEOUT 的解决方法
    查看>>
    npm install 报错 Failed to connect to github.com port 443 的解决方法
    查看>>