Question Link
Http://uva.onlinejudge.org/index.php? Option = com_onlinejudge & Itemid = 8 & category = 513 & page = show_problem & problem = 3326
[Topic]
You are playing a music player, which uses a random playback mode. When playing a random video, a random value of 1 ~ is generated first ~ And then play the songs in the order of N. After playing all the songs in this sequence, a random 1 ~ N to continue playing.
Now I will give you a playback history. This history is incomplete. I thought it was the first record, and some songs were played but not recorded.
Now we will show you a history of playing music from a certain point in time and the total number of songs in the player.
Q: The first song in the history may be the first song in a random list. How many possibilities are there?
[Idea]
Perform preprocessing first, and use a bool array of OK records. OK [I] indicates whether the number of consecutive s starting from position I is different.
Then, the first song is listed as the first X. Check whether the first s-x song is different. This can also be determined by preprocessing.
Then, start from X to determine x, x + S, x + 2 * s ...... Whether all the conditions are met.
[Code]
/*** Ultraviolet A 12174-shuffle **/# include <iostream> # include <cstdio> # include <cstring> using namespace STD; typedef long int64; const int INF = 0x3f3f3f; const int maxn = 100010; int S, N; int arr [maxn]; int CNT [maxn]; bool first [maxn], OK [maxn]; // preprocessing inline void Init () {memset (first, 0, sizeof (first); memset (OK, 0, sizeof (OK); memset (CNT, 0, sizeof (CNT); first [0] = true; int I; for (I = 0; I <S & I <n & &! CNT [arr [I]; ++ I) {first [I + 1] = true; CNT [arr [I] = 1 ;} if (I = N & I <S & first [I]) {While (I ++ <s) first [I] = true;} memset (CNT, 0, sizeof (CNT); OK [N] = true; int COUNT = 0; For (INT I = n-1; I> = 0; -- I) {If (CNT [arr [I] ++ = 0) {++ count ;} if (COUNT = S | n-I <S & n-I = count) {OK [I] = true;} if (I + S-1 <n) {If (-- CNT [arr [I + s-1] = 0) {-- count ;}}} bool check (INT st) {While (ST <n) {If (! OK [st]) return false; ST + = s;} return true;} int main () {int ncase; scanf ("% d", & ncase ); while (ncase --) {scanf ("% d", & S, & N); For (INT I = 0; I <n; ++ I) scanf ("% d", & arr [I]); Init (); int ans = 0; For (INT I = 0; I <s; ++ I) if (first [I] & check (I) + + ans; printf ("% d \ n", ANS) ;}return 0 ;}