codeforces 340D Bubble Sort Graph(dp,LIS)
转载请注明出处: http://www.cnblogs.com/fraud/ ——by fraud
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.
3
3 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].
考虑到可以转化成最长上升子序列的问题。。。然后就O(nlogn)搞一下就好
//#####################
//Author:fraud
//Blog: http://www.cnblogs.com/fraud/
//#####################
#include <iostream>
#include <sstream>
#include <ios>
#include <iomanip>
#include <functional>
#include <algorithm>
#include <vector>
#include <string>
#include <list>
#include <queue>
#include <deque>
#include <stack>
#include <set>
#include <map>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <climits>
#include <cctype>
using namespace std;
#define XINF INT_MAX
#define INF 0x3FFFFFFF
#define MP(X,Y) make_pair(X,Y)
#define PB(X) push_back(X)
#define REP(X,N) for(int X=0;X<N;X++)
#define REP2(X,L,R) for(int X=L;X<=R;X++)
#define DEP(X,R,L) for(int X=R;X>=L;X--)
#define CLR(A,X) memset(A,X,sizeof(A))
#define IT iterator
typedef long long ll;
typedef pair<int,int> PII;
typedef vector<PII> VII;
typedef vector<int> VI;
#define MAXN 100010
int a[MAXN];
int dp[MAXN];
int main()
{
ios::sync_with_stdio(false);
int n;
cin>>n;
for(int i=;i<n;i++)
cin>>a[i];
fill(dp,dp+MAXN,INF);
for(int i=;i<n;i++)
*lower_bound(dp,dp+n,a[i])=a[i];
cout<<lower_bound(dp,dp+n,INF)-dp<<endl; return ;
}
代码君
codeforces 340D Bubble Sort Graph(dp,LIS)的更多相关文章
- Codeforces Round #198 (Div. 2) D. Bubble Sort Graph (转化为最长非降子序列)
D. Bubble Sort Graph time limit per test 1 second memory limit per test 256 megabytes input standard ...
- Codeforces 840C - On the Bench(dp/容斥原理)
Codeforces 题目传送门 & 洛谷题目传送门 这是一道 *2500 的 D1C,可个人认为难度堪比某些 *2700 *2800. 不过嘛,*2500 终究还是 *2500,还是被我自己 ...
- hdu----(1257)最少拦截系统(dp/LIS)
最少拦截系统 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Subm ...
- HDU 1087 Super Jumping! Jumping! Jumping! (DP+LIS)
题意:给定一个长度为n的序列,让你求一个和最大递增序列. 析:一看,是不是很像LIS啊,这基本就是一样的,只不过改一下而已,d(i)表示前i个数中,最大的和并且是递增的, 如果 d(j) + a[i] ...
- codeforces 985E Pencils and Boxes(dp+思维)
E. Pencils and Boxes time limit per test 2 seconds memory limit per test 256 megabytes input standar ...
- HDU 5078 Revenge of LIS II(dp LIS)
Problem Description In computer science, the longest increasing subsequence problem is to find a sub ...
- POJ 2533 Longest Ordered Subsequence(dp LIS)
Language: Default Longest Ordered Subsequence Time Limit: 2000MS Memory Limit: 65536K Total Submis ...
- Bubble Sort Graph CodeForces - 340D || 最长不下降/上升子序列
Bubble Sort Graph CodeForces - 340D 题意: 给出一个n个数的数列,建一个只有n个结点没有边的无向图,对数列进行冒泡排序,每交换一对位置在(i,j)的数在点i和点j间 ...
- 洛谷P2507 [SCOI2008]配对 题解(dp+贪心)
洛谷P2507 [SCOI2008]配对 题解(dp+贪心) 标签:题解 阅读体验:https://zybuluo.com/Junlier/note/1299251 链接题目地址:洛谷P2507 [S ...
随机推荐
- OpenGL ES 2.0 摄像机与投影
1.摄像机的设置 摄像机的位置坐标 摄像机的位置 摄像机up方向 Matrix.setLookAtM( mVMatrix, //存储生成矩阵元素的float[]类型数组 0, //填充起始偏移量 cx ...
- hdu5672 尺取法
StringTime Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) Total Subm ...
- C#拖动自己的定义标题栏(panel)以及实现窗体拖动关闭和最小化
//没有标题 this.FormBorderStyle = FormBorderStyle.None; //任务栏不显示 this.ShowInTaskbar = false; //实现拖动 1.在窗 ...
- c# 大量拼接xml时内存溢出解决方法
public static string SelectUNnormalPriceSTrans(EUNnormalPriceS rqInfo) { string guidStrJianJclFirst ...
- N年之后,只记得三井寿!而我们程序猿们也要加油珍惜时间!
[感觉程序员看一篇励志文章效果大于6篇技术文章,3份源码下载.....所以上此文] [说明:本文不少段落是摘自别人文章,因为本人写程序的文笔有限,怕感动不了大家,所以摘取了不错的部分] 前段时间重新看 ...
- 使用PULL方式解析XML资源文件下面的xml文件
public class MainActivity extends Activity { private Button btn = null; private List<Map<Strin ...
- [Powershell] FTP Download File
# Config $today = Get-Date -UFormat "%Y%m%d" $LogFilePath = "d:\ftpLog_$today.txt&quo ...
- grep详解
一.简介 Global Regular Expression Print,是一种强大的文本搜索工具,能使用正则表达式. 二.语法 grep [OPTIONS] PATTERN [FILE...]gre ...
- myeclipse中控制台日志比实际晚8小时解决方法及java日志处理
今天终于忍不住要解决myeclipse控制台中日志显示比实际晚8小时的问题,开始以为myeclipse编辑器时间问题,后来想想不对,myeclipse控制台打印的是tomcat的日志,随后以为是log ...
- 用户登陆,退出等基本Action
用户登陆页面user_login.jsp对应action为login.do: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transiti ...