zoukankan      html  css  js  c++  java
  • codeforces 340D Bubble Sort Graph(dp,LIS)

    转载请注明出处: http://www.cnblogs.com/fraud/           ——by fraud

     Bubble Sort Graph

    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.

    Input

    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

    Output a single integer — the answer to the problem.

    Sample test(s)
    input
    3
    3 1 2
    output
    2
    Note

    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)搞一下就好

     1 //#####################
     2 //Author:fraud
     3 //Blog: http://www.cnblogs.com/fraud/
     4 //#####################
     5 #include <iostream>
     6 #include <sstream>
     7 #include <ios>
     8 #include <iomanip>
     9 #include <functional>
    10 #include <algorithm>
    11 #include <vector>
    12 #include <string>
    13 #include <list>
    14 #include <queue>
    15 #include <deque>
    16 #include <stack>
    17 #include <set>
    18 #include <map>
    19 #include <cstdio>
    20 #include <cstdlib>
    21 #include <cmath>
    22 #include <cstring>
    23 #include <climits>
    24 #include <cctype>
    25 using namespace std;
    26 #define XINF INT_MAX
    27 #define INF 0x3FFFFFFF
    28 #define MP(X,Y) make_pair(X,Y)
    29 #define PB(X) push_back(X)
    30 #define REP(X,N) for(int X=0;X<N;X++)
    31 #define REP2(X,L,R) for(int X=L;X<=R;X++)
    32 #define DEP(X,R,L) for(int X=R;X>=L;X--)
    33 #define CLR(A,X) memset(A,X,sizeof(A))
    34 #define IT iterator
    35 typedef long long ll;
    36 typedef pair<int,int> PII;
    37 typedef vector<PII> VII;
    38 typedef vector<int> VI;
    39 #define MAXN 100010
    40 int a[MAXN];
    41 int dp[MAXN];
    42 int main()
    43 {
    44     ios::sync_with_stdio(false);
    45     int n;
    46     cin>>n;
    47     for(int i=0;i<n;i++)
    48         cin>>a[i];
    49     fill(dp,dp+MAXN,INF);
    50     for(int i=0;i<n;i++)
    51         *lower_bound(dp,dp+n,a[i])=a[i];
    52     cout<<lower_bound(dp,dp+n,INF)-dp<<endl;
    53     
    54     
    55     return 0;
    56 }
    代码君
  • 相关阅读:
    pyspark 知识点
    pyspark 读取csv文件创建DataFrame的两种方法
    iOS开发改变字符串中指定字符颜色,大小等等
    iOS开发解决页面滑动返回跟scrollView左右划冲突
    iOS开发自定义试图切换
    iOS开发UIColor,CGColor,CIColor三者的区别和联系
    iOS开发热更新JSPatch
    Swift-创建UIButton(其他UI组件雷同)
    Swift-枚举enum理解
    Swift-闭包理解(二)
  • 原文地址:https://www.cnblogs.com/fraud/p/4376961.html
Copyright © 2011-2022 走看看