HDU 2158 模拟题

题目:

给定一个序列,有N个整数,数值范围为[0,N)。

有M个询问,每次询问给定Q个整数,可能出现重复值。

要求找出一个最短区间,该区间要包含这Q个整数数值。

题解:

先便利一个整体的   L 和 R,   然后枚举L,  同时维护R,使得区间满足题目要求,更新最小区间, 直道不满足要求为止。

代码:

#include<stdio.h>
#include<string.h>
#define N 100005
int 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);
      }
    }

}
时间: 2024-12-21 19:24:24

HDU 2158 模拟题的相关文章

hdu 4801模拟题

/* 模拟: 注意:实质上一次魔方的一半要变化 用c++超内存 用g++过了 */ #include<stdio.h> #include<string.h> #include<queue> #include<algorithm> using namespace std; struct node { int f0[4],f4[4],f6[4],f8[4],f16[4],f20[4]; int step; } s,next; int fnow[10000][6]

HDU 4028 The time of a day STL 模拟题

暴力出奇迹.. #include<stdio.h> #include<iostream> #include<algorithm> #include<vector> #include<cmath> #include<queue> #include<set> #include<map> using namespace std; #define ll __int64 #define N 42 ll n,m,ans;

hdu 5641 King&#39;s Phone(暴力模拟题)

Problem Description In a military parade, the King sees lots of new things, including an Andriod Phone. He becomes interested in the pattern lock screen. The pattern interface is a 3×3 square lattice, the three points in the first line are labeled as

HDU 4930 Fighting the Landlords(扯淡模拟题)

Fighting the Landlords 大意: 斗地主....   分别给出两把手牌,肯定都合法.每张牌大小顺序是Y (i.e. colored Joker) > X (i.e. Black & White Joker) > 2 > A (Ace) > K (King) > Q (Queen) > J (Jack) > T (10) > 9 > 8 > 7 > 6 > 5 > 4 > 3. 给你8种组合:1.

HDU 4925 Apple Tree(模拟题)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4925 解题报告:给你n*m的土地,现在对每一块土地有两种操作,最多只能在每块土地上进行两种操作,第一种是种苹果树操作,第二种是施肥操作,种苹果树操作可以使得该块地 长出一个苹果,施肥操作可以使得与这块土地相邻的土地的苹果产量变为原来的两倍,问可以得到的最多的苹果数量是多少? 例如一个4*4的土地,用1表示在该土地上做第一种操作,0表示在该土地上做第二种操作,可以得到最多苹果的操作如下: 0 1 0

HDU 4941 Magical Forest _(:зゝ∠)_ 模拟题

模拟大法保平安_(:зゝ∠)_ #include <cstdio> #include <map> #include <set> #include <algorithm> using namespace std; const int N = 1; struct node{ int x, y, val; node(int a=0,int b=0,int c=0):x(a),y(b),val(c){} bool operator<(const node&am

hdu 4119 Isabella&#39;s Message 模拟题

Isabella's Message Time Limit: 20 Sec  Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=4119 Description Isabella and Steve are very good friends, and they often write letters to each other. They exchange funny experiences, talk ab

HDU 4022 Bombing STL 模拟题

手动模拟.. #include<stdio.h> #include<iostream> #include<algorithm> #include<vector> #include<cmath> #include<queue> #include<set> #include<map> using namespace std; #define N 10100 #define inf 1000000010 map<

HDU多校联合赛(1007 Magical Forest)模拟题

题目: Problem Description There is a forest can be seen as N * M grid. In this forest, there is some magical fruits, These fruits can provide a lot of energy, Each fruit has its location(Xi, Yi) and the energy can be provided Ci. However, the forest wi