HDU 2158 類比題,hdu2158類比題

來源:互聯網
上載者:User

HDU 2158 類比題,hdu2158類比題

題目:

給定一個序列,有N個整數,數值範圍為[0,N)。
有M個詢問,每次詢問給定Q個整數,可能出現重複值。
要求找出一個最短區間,該區間要包含這Q個整數數值。

題解:

先便利一個整體的   L 和 R,   然後枚舉L,  同時維護R,使得區間滿足題目要求,更新最小區間, 直道不滿足要求為止。

代碼:

#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);      }    }        }

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.