AtCoder:pushpush(STL & 思维)

本文探讨了一个有趣的算法问题——逆序追加序列。通过给定整数序列,执行特定的追加和反转操作,最终得出序列结果。利用C++中的list数据结构,实现了简洁高效的解决方案。

C - pushpush


Time limit : 2sec / Memory limit : 256MB

Score : 300 points

Problem Statement

You are given an integer sequence of length na1,…,an. Let us consider performing the following n operations on an empty sequence b.

The i-th operation is as follows:

  1. Append ai to the end of b.
  2. Reverse the order of the elements in b.

Find the sequence b obtained after these n operations.

Constraints

  • 1n2×105
  • 0ai109
  • n and ai are integers.

Input

Input is given from Standard Input in the following format:

n
a1 a2  an

Output

Print n integers in a line with spaces in between. The i-th integer should be bi.


Sample Input 1

Copy
4
1 2 3 4

Sample Output 1

Copy
4 2 1 3
  • After step 1 of the first operation, b becomes: 1.
  • After step 2 of the first operation, b becomes: 1.
  • After step 1 of the second operation, b becomes: 1,2.
  • After step 2 of the second operation, b becomes: 2,1.
  • After step 1 of the third operation, b becomes: 2,1,3.
  • After step 2 of the third operation, b becomes: 3,1,2.
  • After step 1 of the fourth operation, b becomes: 3,1,2,4.
  • After step 2 of the fourth operation, b becomes: 4,2,1,3.

Thus, the answer is 4 2 1 3.


Sample Input 2

Copy
3
1 2 3

Sample Output 2

Copy
3 1 2

As shown above in Sample Output 1, b becomes 3,1,2 after step 2 of the third operation. Thus, the answer is 3 1 2.


Sample Input 3

Copy
1
1000000000

Sample Output 3

Copy
1000000000

Sample Input 4

Copy
6
0 6 7 6 7 0

Sample Output 4

Copy
0 6 6 0 7 7
题意:给n个数a和一个空序列b,每次执行两个操作,①将a[i]放到b的末尾,②翻转b序列,输出执行完n个数后的b序列。

思路:容易想到是将a[i]前后前后地放到b里面,那么用queue或者list都能方便实现。

# include <bits/stdc++.h>
using namespace std;
list<int>l;
int main()
{
    int n, m, t=1;
    scanf("%d",&n);
    for(int i=0; i<n; ++i)
    {
        scanf("%d",&m);
        t = !t;
        if(t) l.push_back(m);
        else l.push_front(m);
    }
    if(!(n&1)) l.reverse();
    for(auto i=l.begin();i!=l.end();++i)
        printf("%d ",*i);
    return 0;
}



### C++机试学习资料与练习题推荐 #### 资料推荐 对于准备C++机试的学习者而言,可以从以下几个方面入手: 1. **基础语法掌握** 掌握基本的数据类型、控制结构(如 `if` 和 `for`)、函数定义以及输入输出操作是必要的。例如,在引用中的代码片段展示了如何通过条件判断实现简单的逻辑处理[^1]。 2. **算法与数据结构** 熟悉常见的算法和数据结构是非常重要的。比如排序算法的应用可以在实际编程中体现出来,如下所示的代码实现了基于自定义规则的排序功能[^2]: ```cpp #include &lt;iostream&gt; #include &lt;vector&gt; #include &lt;algorithm&gt; using namespace std; const int N = 110; pair&lt;int, int&gt; ans[N]; int n; int main() { cin &gt;&gt; n; int k = 0; while (n--) { int p, q; cin &gt;&gt; p &gt;&gt; q; ans[k++] = {q, p}; } sort(ans, ans + k); for (int i = 0; i &lt; k; i++) { cout &lt;&lt; ans[i].second &lt;&lt; &quot; &quot; &lt;&lt; ans[i].first &lt;&lt; endl; } return 0; } ``` 3. **STL库应用** STL 是 C++ 中非常强大的工具集,熟悉其常用容器(如 `vector`, `stack`, `queue`)及其方法能够显著提高编码效率。下面是一个利用栈反转字符串的例子[^3]: ```cpp #include &lt;iostream&gt; #include &lt;string&gt; #include &lt;stack&gt; using namespace std; int main() { string s; stack&lt;string&gt; ss; while (cin &gt;&gt; s) { ss.push(s); } while (!ss.empty()) { cout &lt;&lt; ss.top(); cout &lt;&lt; &#39; &#39;; ss.pop(); } return 0; } ``` 4. **综合实践题目** 可以尝试解决一些综合性较强的题目,这些题目通常涉及多个知识点的融合运用。例如,设计一个程序来模拟工作环境下的绩效评估机制[^4]。 --- #### 练习平台建议 以下是几个适合进行 C++ 编程练习的在线平台: - LeetCode: 提供丰富的算法题库,支持多种语言提交。 - Codeforces: 定期举办比赛并提供大量高质量训练题。 - AtCoder: 日本知名竞赛网站,难度适中且注重思维能力培养。 - HackerRank: 面向不同技能水平用户设置专项挑战赛。 --- ###
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值