A9087 | Bubble Sort Graph
时间限制1s
内存限制256MB
通过 / 提交0/0
题目描述
Iahub recently has learned Bubble Sort, an algorithm that is used to sort a permutation with $n$ elements $a_{1}$ , $a_{2}$ , ..., $a_{n}$ in ascending order. He is bored of this so simple algorithm, so he invents his own graph. The graph (let's call it $G$ ) initially has $n$ vertices and 0 edges. During Bubble Sort execution, edges appear as described in the following algorithm (pseudocode).
<br></br>procedure bubbleSortGraph()<br></br> build a graph G with n vertices and 0 edges<br></br> repeat<br></br> swapped = false<br></br> for i = 1 to n - 1 inclusive do:<br></br> if a[i] > a[i + 1] then<br></br> add an undirected edge in G between a[i] and a[i + 1]<br></br> swap( a[i], a[i + 1] )<br></br> swapped = true<br></br> end if<br></br> end for<br></br> until not swapped <br></br> /* repeat the algorithm as long as swapped value is true. */ <br></br>end procedure<br></br>For a graph, an independent set is a set of vertices in a graph, no two of which are adjacent (so there are no edges between vertices of an independent set). A maximum independent set is an independent set which has maximum cardinality. Given the permutation, find the size of the maximum independent set of graph $G$ , if we use such permutation as the premutation $a$ in procedure bubbleSortGraph.输入格式
The first line of the input contains an integer $n$ ( $2<=n<=10^{5}$ ). The next line contains $n$ distinct integers $a_{1}$ , $a_{2}$ , ..., $a_{n}$ $(1<=a_{i}<=n)$ .
输出格式
Output a single integer — the answer to the problem.
输入输出样例
输入 #1
3 3 1 2
输出 #1
2
Consider the first example. Bubble sort swaps elements 3 and 1. We add edge (1, 3). Permutation is now \[1, 3, 2\]. Then bubble sort swaps elements 3 and 2. We add edge (2, 3). Permutation is now sorted. We have a graph with 3 vertices and 2 edges (1, 3) and (2, 3). Its maximal independent set is \[1, 2\].
C++ 编辑器
输入
输出
可保存默认模板;新题优先使用已保存模板。
当前快捷键仅展示,暂不支持修改。
- 撤销
Ctrl / ⌘ + Z - 重做
Ctrl / ⌘ + Y - 查找
Ctrl / ⌘ + F - 全选
Ctrl / ⌘ + A - 复制
Ctrl / ⌘ + C - 剪切
Ctrl / ⌘ + X - 粘贴
Ctrl / ⌘ + V - 自动排版
工具栏排版按钮 - 草稿保存
编辑时自动保存到本机
历史
提交记录
状态说明时间源码
AI
作答助手
你好,我是作答助手。可以问思路、复杂度、样例含义或代码报错原因;不会直接给出完整 AC 代码。
确定要清空代码吗?
提交通过
评测结果:Accepted