UVa 12174:Shuffle(滑動視窗)

來源:互聯網
上載者:User

題目連結:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=844&page=show_problem&problem=3326

題意:你正在使用的音樂播放器有一個所謂的亂序播放功能,即隨機打亂歌曲的播放順序。假設一共有s首歌,則一開始會給這s首歌隨機排序,全部播放完畢後再重新隨機排序、繼續播放,依次類推。注意,當s首歌播放完畢之前不會重新排序。這樣,播放記錄裡的每s首歌都是1~s的一個排列。給出一個長度為n (1≤s,n≤100000) (1 \le s, n \le 100000)的播放記錄(不一定是從最開始記錄的) xi(1≤xi≤s) x_i(1 \le x_i \le s),你的任務是統計下次隨機排序所發生的時間有多少種有多少種可能性。例如,s=4,播放記錄是3,4,4,1,3,2,1,2,3,4,不難發現只有一種可能性:前兩首是一個段的最後兩首歌,後面是兩個完整的段,因此答案是1;當s=3時,播放記錄1,2,1有兩種可能:第一首是一個段,後兩首是另一段;前兩首是一段,最後一首是另一段。答案為2。(本段摘自《演算法競賽入門經典(第2版)》)

分析:
       滑動視窗的思想,維護長度為s的視窗看是否合法,最後枚舉可能答案進行判斷即可。

代碼:

這#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 std;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;}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.