CF 355 div2 C, D "very good two-brain-hole problem"

Source: Internet
Author: User
Tags bool

Transmission Door
C: Given an arrangement of N of N, each operation can place a number in the first or last of the sequence, and ask to turn the sequence into such an arrangement of 1 2 3....n.

Idea: We must first see a fact, that is, each time we choose some not in the corresponding position of these numbers in order to put in front or put behind so that the final sequence is we want this sequence, then at least, we will find a longest rise plus a sub-sequence, then we want these not moving, All that is left in order can be obtained, and this is the least of the steps.
"Brain hole good problem"
AC Code

const int MAXN = 1e5+5;
int A[MAXN], DP[MAXN];
void Solve ()
{
    int n;
    while (CIN >> N) {
        Fill (DP, 0);
        for (int i = 1; I <= n; i + +) {
            cin >> a[i];
            Dp[a[i]] = dp[a[i]-1] + 1;
        }
        int mx = 0;
        for (int i = 1; I <= n; i + +)
            mx = max (mx, dp[i]);
        cout << n-mx << endl;
    }
}

D: "Construct a good question!"
Test instructions: Given some edges and flags whether these edges are in the smallest spanning tree of the graph, ask if you can construct a picture that satisfies the edge of the topic, and give these edges according to the input order of the edges.

Idea: The first obvious idea is that we want this n-1 to be constructed into a chain tree, the rest of the add up, so we first in accordance with the weight of the size of a sequence, and then the first 1 of the edge of processing, and then go to the edge of 0, then when we meet the weight of this edge than the corresponding in the chain tree above the Benquan is smaller, Then it is impossible, this time the key point of judgment, that is the most difficult to write the place, the specific implementation please look at the code .....

AC Code

const int MAXN = 1E5+5; struct node {int u, V, W, id, F;}
E[MAXN];
    BOOL Cmp1 (node x, node Y) {if (x.f = = Y.F) return X.W < Y.W;
return X.F > Y.f;
    } bool Cmp2 (node x, node Y) {return x.id < y.id;} void Solve () {int n, m;
        while (CIN >> n >> m) {int cnt = 0;
            for (int i = 1; I <= m; i + +) {cin >> e[i].w >> e[i].f; E[i].id = i;
        CNT + = E[I].F;
            } if (cnt! = n-1) {printf (" -1\n");
        Continue
        } sort (e+1, e+1+m, CMP1);
        for (int i = 1; I <= n-1; i + +) {e[i].u = i; e[i].v = i+1;
        } int flag = 1;
        int id1 = 0, id2 = 2;
            for (int i = n; i <=m; i + +) {if (id1 + 2 = = Id2) id2++, id1 = 1;
            else id1++;
                if (E[I].W < E[ID2-1].W) {//This is the most critical place.
                Flag = 0;
            Break } e[i].u = id1, e[i].v = Id2;
        } if (!flag) {printf (" -1\n");
        Continue
        } sort (e+1, e+1+m, CMP2);
        for (int i = 1; I <= m; i + +) {printf ("%d%d\n", e[i].u, E[I].V); }
    }
}

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.