Shuffle-12174 Method

Source: Internet
Author: User
Tags shuffle

Question: Question Link

Idea: see zishu. I still don't understand the specific operation method. The code is from a question. I will review it later and need to understand it.

Code:

1 # include <iostream> 2 # include <cstring> 3 # include <set> 4 using namespace STD; 5 6 const int n = 1e5 + 5; 7 8 int S, N, A [n], vis [N]; 9 bool flag [N]; 10 int ans; 11 12 Void Init () 13 {14 CIN> S> N; 15 int num = 0; 16 for (INT I = 0; I <n; I ++) 17 {18 CIN> A [I]; 19 if (I <s) 20 {// analyze the previous s. 21 if (vis [A [I]) 22 num ++; // count the repeated number 23 vis [A [I] ++; 24} 25} 26 27 for (INT I = 0; I <N; I ++) 28 {29/ /If num = 0, it indicates that there are no repeated numbers in the first S, then the first number can be used as the start of the loop 30 if (num = 0) 31 flag [I] = true; 32 33 // The window starts to slide 34 if (vis [A [I] = 2) 35 num --; // if at this time the leftmost number is repeated, num needs to be reduced by 136 vis [A [I] --; 37 38 int K = I + S; // new number enters the Sliding Window 39 if (k> = N) 40 continue; 41 if (vis [A [k]) 42 num ++; // If 43 vis [A [k] ++ already exists; 44} 45} 46 47 bool judge (int x) 48 {49 for (INT I = x; I <n; I + = s) 50 if (! Flag [I]) 51 return false; 52 return true; 53} 54 55 void solve () 56 {57 memset (VIS, 0, sizeof (VIS )); 58 59 ans = 0; 60 for (INT I = 0; I <s; I ++) 61 {62 if (Judge (I) 63 ans ++; 64 if (I> = N) 65 continue; 66 // traverse from left to right. If a [I] already exists before it, it must start, in this case, the loop 67 If (vis [A [I]) 68 break; 69 vis [A [I] ++; 70} 71} 72 73 int main () 74 {75 // freopen ("D: \ txt.txt", "r", stdin); 76 int t; 77 CIN> T; 78 while (t --) 79 {80 memset (flag, 0, sizeof (FLAG); 81 memset (VIS, 0, sizeof (VIS); 82 Init (); 83 solve (); 84 cout <ans <Endl; 85} 86 return 0; 87}

 

Shuffle-12174 Method

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.