动态规划——Bubble Sort Graph
来源:互联网 发布:死飞自行车多少钱淘宝 编辑:程序博客网 时间:2024/06/05 15:33
Iahub recently has learned Bubble Sort, an algorithm that is used to sort a permutation with n elements a1, a2, ..., an 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).
procedure bubbleSortGraph() build a graph G with n vertices and 0 edges repeat swapped = false for i = 1 to n - 1 inclusive do: if a[i] > a[i + 1] then add an undirected edge in G between a[i] and a[i + 1] swap( a[i], a[i + 1] ) swapped = true end if end for until not swapped /* repeat the algorithm as long as swapped value is true. */ end procedure
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 ≤ 105). The next line contains n distinct integers a1, a2, ..., an (1 ≤ ai ≤ n).
Output a single integer — the answer to the problem.
33 1 2
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].
题意:看了之后才知道是求最长非递减子序列。。。
思路:看数据范围知道是用dp+二分
#include <cstdio>#include <cstring>#include <algorithm>#include <iostream>using namespace std;int a[100010];int dp[100010];int n;int wtf(int l, int x) //二分搜索求dp数组中<=x的位置{ int left=1,right=l,mid=(left+right)>>1; while(left<=right) { if(dp[mid]>x) right=mid-1; else if(dp[mid]<x) left=mid+1; else return mid; mid=(left+right)>>1; } return left;}int DP(){ int l=1; dp[1]=a[0]; for(int i=1; i<n; i++) { int k=wtf(l, a[i]); dp[k]=a[i]; l=max(l, k); } return l;}int main(){ while(~scanf("%d", &n)) { memset(dp, 0, sizeof(dp)); memset(a, 0, sizeof(a)); for(int i=0; i<n; i++) scanf("%d", &a[i]); int ans=DP(); printf("%d\n", ans); } return 0;}
- 动态规划——Bubble Sort Graph
- Bubble Sort Graph
- 模板——Bubble Sort
- Algorithm——Bubble Sort
- CodeForces 340D - Bubble Sort Graph
- codeforces 340D D. Bubble Sort Graph(dp+线段树)
- Codeforces 340D Bubble Sort Graph【LIS变形】
- codeforces 340D Bubble Sort Graph(dp+二分)
- 经典排序算法——冒泡排序Bubble sort
- 排序算法——冒泡排序(Bubble Sort)
- 排序——冒泡排序(Bubble Sort)
- 冒泡排序(Bubble Sort)——排序算法
- 排序算法——冒泡排序(Bubble Sort)
- 交换排序—冒泡排序(Bubble Sort)
- 交换排序—冒泡排序(Bubble Sort)
- 【Data Structures】 7. Simple Sorting—Bubble Sort, Selection Sort, and Insertion Sort
- Bubble sort
- Bubble Sort
- MySQL连接数问题
- 64位Linux下安装mysql-5.7.13-linux-glibc2.5-x86_64
- C语言-可变参数列表
- Qt :图片与QByteArray互转
- java类型转换小工具
- 动态规划——Bubble Sort Graph
- 解题报告:SPOJ
- R语言-数据类型
- POJ 2115 C Looooops extend gcd +找点
- 查找:静态查找表
- Python之路-python(mysql介绍和安装、pymysql、ORM sqlachemy)
- 消息驱动与事件驱动比较
- 讲一讲楼主从工程监理到软件测试又走出软件测试的这一年
- JavaScript 常用正则大集合!