UVa 12174:shuffle (sliding window)

Source: Internet
Author: User
Tags shuffle

Title Link: Https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=844&page=show_ problem&problem=3326

Test instructions: The music player you are using has a so-called shuffle function that randomly disrupts the order of the songs to play. Assuming that there is a song of S, then the first song will be randomly sorted, all played and then re-randomly sorted, continue to play, and so on. Note that the S song will not be reordered until it finishes playing. In this way, every s song in the playback record is an arrangement of 1~s. Given a playback record of n (1≤s,n≤100000) (1 \le s, n \le 100000) length (not necessarily from the very beginning) of Xi (1≤xi≤s) x_i (1 \le x_i \le s), your task is to count the number of times the next random order has occurred Number of possibilities. For example, s=4, the playback record is 3,4,4,1,3,2,1,2,3,4, it is not difficult to find that there is only one possibility: the first two is a paragraph of the last two songs, followed by two complete segments, so the answer is 1; When s=3, there are two possible ways to play records 1,2,1: The first is a segment, The last two is another paragraph; the first two is a paragraph and the final one is another. The answer is 2. (This paragraph is excerpted from the "Algorithmic Competition Primer (2nd edition)")

Analysis:
The idea of sliding the window, maintaining the length of the window to see if it is legal, the last enumeration may be the answer to judge.

Code:

This # include <fstream> #include <iostream> #include <cstring> #include <algorithm> #include < stack> #include <sstream> #include <string> #include <map> #include <cmath> #include < queue> #include <vector> #include <set> #include <string> #include <vector> using namespace St

D

const int MAXN = 100000 + 5;
int T, ans, CNT, S, N;
int A[MAXN * 3], NUM[MAXN], X[MAXN * 2];

BOOL Flag;
    int main () {scanf ("%d", &t); for (int C = 0; C < T;
        ++c) {ans = 0;
        Memset (A,-1, sizeof (a));
        memset (x, 0, sizeof (x));
        memset (num, 0, sizeof (num));
        scanf ("%d%d", &s, &n);
        CNT = s;
        for (int i = s; i < S + N; ++i) scanf ("%d", &a[i]);  for (int i = s; i < S + S + N; ++i) {if (a[i-s]! =-1) {--num[a[i-
                S]]; if (num[a[i-s]] = = 0)--cnT
            } else--cnt;
                if (a[i]! =-1) {++num[a[i]];
            if (num[a[i]] = = 1) ++cnt;
            } else ++cnt;
        if (cnt = = s) x[i-s] = 1;
            } for (int i = 0; i < s; ++i) {flag = true;
                    for (int j = i; J < S + N; j + = s) if (!x[j]) {flag = false;
                Break
        } if (flag) ++ans;
    } printf ("%d\n", ans);
} 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.