Algorithm: UVa 11536

Source: Internet
Author: User
Tags cas count printf

The main effect of the topic:

Give a sequence

X1 = 1

X2 = 2

X3 = 3

Xi = (Xi-1 + Xi-2 + Xi-3)% M + 1 for i = 4 to N

To find a shortest sequence of successive sequences, so that this subsequence contains a positive integer "1,k"

Ideas:

Scan it again, using a queue to record the position of the number within the "1, k" interval, and then use a variable count to maintain the number of 1,k in "". When Count equals K, the current sequence already satisfies the requirement, and the number of queue headers is the starting position.

Algorithm complexity O (n)

Code:

/* UVa 11536-smallest sub-array * */#include <iostream> #include <cstdio> #include <cstr  
    
ing> #include <utility> #include <set> #include <queue> using namespace std;  
typedef long long Int64;  
    
typedef pair<int,int>pii;  
const int INF = 0X3F3F3F3F;  
    
const int MAXN = 1000010;  
int n, m, K;  
int ARR[MAXN];  
    
int CNT[MAXN];  
    void Init () {arr[1] = 1;  
    ARR[2] = 2;  
    ARR[3] = 3;  
    for (int i=4; i<=n; ++i) arr[i] = (arr[i-1]+arr[i-2]+arr[i-3))%m + 1;  
memset (CNT, 0, sizeof (CNT));  
    int main () {int ncase, Cas=1;  
    
    scanf ("%d", &ncase);  
    
        while (ncase--) {scanf ("%d%d%d", &n,&m,&k);  
        Init ();  
        int minx = INF;  
        int count=0;  
    
        queue<int>q; for (int i=1; i<=n; ++i) {if (arr[i]>=1 && arr[i]<=k) {Q.push (i);  
                if (cnt[arr[i]]++ = = 0) {++count;  
                    while (count = = k) {int tmp = I-q.front () + 1;  
                    Minx = MIN (tmp, Minx);  
                    int val = Arr[q.front ()];  
                    if (--cnt[val]==0) {--count;  
    
                } q.pop ();  
        } printf ("Case%d:", cas++);  
        if (minx = = INF) puts ("Sequence Nai");  
    else printf ("%d\n", Minx);   
return 0; }

See more highlights of this column: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/sjjg/

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.