11.12 Simulation T2 Bubble Sort Chart

Source: Internet
Author: User

"Problem description"

There is a piece of pseudo-code that uses a bubbling sort to produce a graph: function bubblesortgraph (n, a[]):

 Graph = Emptygraph ()
Repeat
Swapped = False
For i = 1 to n-1:
If a[i] > a[i + 1]:
Graph.addedge (A[i], a[i + 1])
Swap (A[i], a[i + 1])
swapped = True
Until not swapped
Return graph
The input of the function is the arrangement of the length n a[], and the output is a graph of n-nodes without direction. function, Emptygraph () creates an empty graph, Addedge (x, y) adds an independent edge between X and Y to the graph, and the graph that is returned is the non-direction graph produced. The point independent set of graphs is a subset of the node collections in the graph. If the collection is a point-independent set of graphs, then any two nodes in the graph are not connected directly to each other. Given an arrangement of 1~n, the size of the maximum point independent set of the free graph generated by pseudo-code is requested, and the nodes that are bound to exist at the maximum point are independent.

"Input Format"

Enter the first line containing an integer n. The next line contains n space-delimited integers, representing the order
Column a[].

"Output Format"

Outputs two lines. The first line contains an integer that represents the size of the maximum point independent set of the generated graph.
The second line outputs the maximum point in a separate set of nodes that must contain the corresponding subscript in the input sequence, according to the
Small to large sequential outputs, separated by spaces.

"Sample Input"

3
3 1 2

"Sample Output"

2
2 3

"Data size and conventions"

For 30% of data, n≤16
For 60% of data, n≤1000.
For 100% of data, 1≤n≤100,000.

Prompted

The number of nodes that must exist in the maximum point independent set does not necessarily equal the size of the maximum point independent sets.

Idea: First question:

According to the pseudo-code, the two points of the edge can form a descending sequence, so the non-practicing points form the ascending sequence, so the first question is the length of the longest ascending subsequence in a[].

Second question:

A[] The common point in all the longest ascending subsequence sequences.

Code:
#include <cstdio>#include<algorithm>#include<iostream>#defineMAXN 100001using namespacestd;intN,A[MAXN],TOT,B[MAXN],C[MAXN],D[MAXN],MAXX[MAXN];BOOLE[MAXN];intMain () {Freopen ("bubble.in","R", stdin); Freopen ("Bubble.out","W", stdout); inti,j; scanf ("%d",&N);  for(i=1; i<=n;i++) scanf ("%d",&A[i]);  for(i=1; i<=n;i++)    {        if(a[i]>C[tot]) c[++tot]=a[i],b[i]=tot; Else        {            intX=upper_bound (c+1, c+tot+1, A[i])-C; C[X]=A[i]; B[i]=x; }} printf ("%d\n", tot);  for(i=n;i>=1; i--)    {        if(b[i]==tot| | maxx[b[i]+1]>A[i]) {E[i]=1; D[b[i]]++; Maxx[b[i]]=Max (maxx[b[i]],a[i]); }    }     for(i=1; i<=n;i++)      if(e[i]&&d[b[i]]==1) printf ("%d", i);    Fclose (stdin);    Fclose (stdout); return 0;}

11.12 Simulation T2 Bubble Sort Chart

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.