UVA - 12174 - Shuffle(思路:预处理 + 暴力)

题意:有个音乐播放器有乱序功能,可以随机播放1 ~ s首歌曲,每次随机播放完后再次打乱顺序随机播放(在播放完这一组的歌曲之前,不会再次乱序,直到全部播完)。现截取一段记录,给定播放的歌曲数 s 以及这段记录的长度 n ,然后输入这段记录,根据这段记录,求随机排序发生的时刻有几种可能。(1 <= s, n <= 100000)

对于符合要求的某一段来说,每首歌最多出现一次,可以出现零次。

先预处理,求出对于每个元素往前最多延伸至 s 长度时,能否符合要求,然后对每 s 的长度暴力求解即可。

这里多处理了结束后的 s 长度,目的是最后多出来的,长度不足 s 的那一段也方便求解是否符合要求。

代码如下:

 

#include<cstdio>
#include<cstring>
#include<cctype>
#include<cstdlib>
#include<cmath>
#include<iostream>
#include<sstream>
#include<iterator>
#include<algorithm>
#include<string>
#include<vector>
#include<set>
#include<map>
#include<deque>
#include<queue>
#include<stack>
#include<list>
#define fin freopen("in.txt", "r", stdin)
#define fout freopen("out.txt", "w", stdout)
#define pr(x) cout << #x << " : " << x << "   "
#define prln(x) cout << #x << " : " << x << endl
#define Min(a, b) a < b ? a : b
#define Max(a, b) a < b ? b : a
typedef long long ll;
typedef unsigned long long llu;
const int INT_INF = 0x3f3f3f3f;
const int INT_M_INF = 0x7f7f7f7f;
const ll LL_INF = 0x3f3f3f3f3f3f3f3f;
const ll LL_M_INF = 0x7f7f7f7f7f7f7f7f;
const double pi = acos(-1.0);
const double EPS = 1e-6;
const int dr[] = {0, 0, -1, 1, -1, -1, 1, 1};
const int dc[] = {-1, 1, 0, 0, -1, 1, -1, 1};
const ll MOD = 1e9 + 7;
using namespace std;

#define NDEBUG
#include<cassert>
const int MAXN = 100 + 10;
const int MAXT = 200000 + 10;

int T, s, n, a[MAXT], num[MAXT];
bool vis[MAXT];
set<int> st;

bool solve(int lur){
    for(int i = lur; i < n + s; i += s)
        if(!vis[i])  return false;
    return true;
}

int main(){
    scanf("%d", &T);
    while(T--){
        memset(vis, false, sizeof vis);
        memset(num, 0, sizeof num);
        memset(a, -1, sizeof a);
        st.clear();
        scanf("%d%d", &s, &n);
        for(int i = 0; i < n; ++i)  scanf("%d", a + i);
        int head = 0;
        for(int tail = 0; tail < n + s; ++tail){
            if(a[tail] != -1 && ++num[a[tail]] > 1)  st.insert(a[tail]);
            if(tail - head + 1 > s){
                if(--num[a[head]] == 1)  st.erase(a[head]);
                ++head;
            }
            if(st.empty())  vis[tail] = true;
        }
        int len = Min(n, s), ans = 0;
        for(int i = 0; i < len; ++i)
            if(solve(i))  ++ans;
        if(ans == len)  ans = Max(ans, s);
        printf("%d\n", ans);
    }
    return 0;
}

 

转载于:https://www.cnblogs.com/tyty-TianTengtt/p/6028055.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值