【例题 8-15 UVA - 12174】Shuffle

【链接】 我是链接,点我呀:)
【题意】

在这里输入题意

【题解】

枚举第一段的范围[0..i] (0<=i

【代码】

/*
  	1.Shoud it use long long ?
  	2.Have you ever test several sample(at least therr) yourself?
  	3.Can you promise that the solution is right? At least,the main ideal
  	4.use the puts("") or putchar() or printf and such things?
  	5.init the used array or any value?
  	6.use error MAX_VALUE?
  	7.use scanf instead of cin/cout?
  	8.whatch out the detail input require
*/
/*
    一定在这里写完思路再敲代码!!!
*/
#include <bits/stdc++.h>
using namespace std;

const int N = 1e5;

int T,s,n,bo[N+10],a[N+10],f[N+10],cnt[N+10],kai[N+10],jie[N+10];

int ff(int x){
    if (f[x]!=x) return ff(f[x]);
    else return x;
}

void init(){
    memset(jie,255,sizeof jie);
    memset(bo,0,sizeof bo);
    memset(kai,0,sizeof kai);

    int l = 1;
    for (int i = 1;i <= n;i++){
        while (bo[a[i]]){
            bo[a[l]] = 0;
            l++;
        }
        if (i-l+1==s){
            if (jie[l-1]!=-1){
                int r1 = ff(jie[l-1]),r2 = ff(l);
                f[r1] = r2;
                cnt[r2] += cnt[r1];
            }
            kai[l] = 1;
            jie[i] = l;
        }

        bo[a[i]] = 1;
    }
    if (n-l+1<s) kai[l] = 1;
    for (int i = l+1;i <= n;i++) kai[i] = 1;
}


int main(){
	#ifdef LOCAL_DEFINE
	    freopen("rush_in.txt", "r", stdin);
	#endif
	ios::sync_with_stdio(0),cin.tie(0);
    cin >> T;
    while (T--){
        cin >> s >> n;
        for (int i = 1;i <= n;i++) cin >> a[i];
        for (int i = 1;i <= n;i++) f[i] = i,cnt[i] = 1;

        init();

        memset(bo,0,sizeof bo);
        int ans = 0;
        for (int i = 0;i < s;i++){
            if (i<=n && i>=1 && bo[a[i]]) break;
            if (i>=n) {
                ans++;
                continue;
            }
            if (kai[i+1]){
                int r = ff(i+1);
                if (n-cnt[r]*s-i<s){
                    ans++;
                }
            }
            if (i>=1 && i <= n) bo[a[i]] = 1;
        }
        cout << ans << endl;
    }
	return 0;
}

posted @ 2018-01-04 21:24  AWCXV  阅读(132)  评论(0编辑  收藏  举报