BNU 34981 a matrix (constructed)

Source: Internet
Author: User

Link: BNU 34981 a Matrix

Suppose there is a sequence, and a sequence table is constructed according to the algorithm given in the question. Now the question is given a sequence table, which requires that the sequence be obtained and that the inverted Lexicographic Order of the sequence be the largest.

Solution: construction. For each layer, it must be incremental and can be obtained according to the algorithm. And a number is switched to the next line because there must be a sequence with a number smaller than its own, therefore, each layer starts matching from the last number and finds the largest number smaller than itself in the previous layer. It is assumed that this number causes the current number to be switched to the next row, note that a single number can only be changed to the next row. Therefore, the corresponding sequence cannot be found, and-1 is output.

  • The number in a row is not incremental.
  • The number of the current row cannot correspond to the number of the previous row one by one

#include <cstdio>#include <cstring>#include <vector>#include <algorithm>using namespace std;const int N = 1e5+5;bool flag;int n, m, c, mv, f[N], r[N], ans[N];vector<int> g[N];int getfar(int x) {    return x == f[x] ? x : f[x] = getfar(f[x]);}void init () {    scanf("%d%d", &n, &m);    flag = false;    for (int i = 0; i <= n; i++)        r[i] = f[i] = i;    for (int i = 0; i < m; i++)        g[i].clear();    int t;    for (int i = 0; i < m; i++) {        scanf("%d", &t);        int a, pre = 0;        for (int j = 0; j < t; j++) {            scanf("%d", &a);            g[i].push_back(a);            if (a < pre)                flag = true;            pre = a;        }    }}bool insert (int x, int d) {    for (int j = mv-1; j >= 0; j--) {        if (g[d][j] < x) {            int p = getfar(g[d][j]);            f[p] = x;            r[p] = x;            mv = j;            return true;        }    }    return false;}void put(int x) {    ans[c--] = x;    if (r[x] != x)        put(r[x]);}void solve () {    for (int i = m-1; i; i--) {        int t = g[i].size();        mv = g[i-1].size();        for (int j = t-1; j >= 0; j--)            if (!insert(g[i][j], i-1)) {                flag = true;                return;            }    }    c = n;    int t = g[0].size();    for (int i = t-1; i >= 0; i--)        put(g[0][i]);}int main () {    int cas;     scanf("%d", &cas);    for (int i = 1; i <= cas; i++) {        init ();        printf("Case #%d: ", i);        solve();        if (flag) {            printf("No solution\n");        } else {            for (int j = 1; j < n; j++)                printf("%d ", ans[j]);            printf("%d\n", ans[n]);        }    }    return 0;}

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.