HDU 2158 simulation questions, hdu2158 simulation questions

Source: Internet
Author: User

HDU 2158 simulation questions, hdu2158 simulation questions

Question:

Given a sequence, there are N integers in the range of [0, N ).
There are M inquiries. Each time you ask for a given Q integer, duplicate values may appear.
You must find a shortest interval that contains the Q integer values.

Question:

First, a whole L and R are convenient, then L is enumerated, and R is maintained at the same time, so that the range meets the question requirements, the minimum interval is updated, and the direct path does not meet the requirements.

Code:

#include<stdio.h>#include<string.h>#define N 100005int a[N], find[N], mark[N];int l, r, n, m, count, num;void Find(){   int kk = 0;   for(int i = 1; i <= n; i++)   {     if(mark[a[i]])     {       if(!find[a[i]])   kk ++;       find[a[i]] ++;       if(kk == num)         {          r = i;          break ;            }     }        }     }void slove(){   for(int i = 1; i <= n; i++)   {      if(mark[a[i]])      {        if(!(--find[a[i]]))        {           int flag = 0;           for(int j = r+1; j <= n; j++)           {              if(mark[a[j]])               {                find[a[j]] ++;               if(a[j] == a[i])               {                flag = 1;                 r = j;                break;                       }              }                  }                       if(!flag)  break;            }                            }          if(count > r - i)   count = r - i;   }     }int main(){    int q, p;    while(scanf("%d %d", &n, &m)!=EOF)    {      if( n == 0 && m == 0 )  break;      for(int i = 1; i <= n; i++)      {         scanf("%d", &a[i]);              }         while(m --)      {             memset(mark,0,sizeof(mark));       memset(find,0,sizeof(find));       scanf("%d", &q);       num = 0;       while(q--)       {         scanf("%d", &p);         if(!mark[p])  num ++;         mark[p] = 1;            }             l = 1, count = n;       Find();           count = r - l + 1;        slove();          printf("%d\n", count);      }    }        }

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.