CDOJ 1256 打表+数组 统计

昊昊爱运动

Time Limit: 3000/1000MS (Java/Others)     Memory Limit: 65535/65535KB (Java/Others)
 

昊昊喜欢运动

N天内会参加M种运动(每种运动用一个[1,m]的整数表示)

舍友有Q个问题

问昊昊第l天到第r天参加了多少种不同的运动

Input

输入两个数NM (1N20001M100);

输入N个数ai表示在第i天昊昊做了第ai类型的运动;

输入一个数Q(1Q106);

输入Q行 每行两个数 lr(1lrn);

Output

一共Q

每一行输出一个数 表示昊昊在第l天到第r天一共做了多少种活动

Sample input and output

Sample Input Sample Output
5 3
1 2 3 2 2
3
1 4
2 4
1 5
3
2
3

Source

第七届ACM趣味程序设计竞赛第二场(正式赛)

论打表的重要性,数组出现数字种类的统计

<span style="font-size:24px;color:#3333ff;">#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int a[2005],ans[2005][2005],f[105];
int main()
{
    int n,m,i,j;
    while(~scanf("%d %d",&n,&m))
    {
         for(int i=1;i<=n;i++)
            scanf("%d",&a[i]);
         memset(ans,0,sizeof(ans));
         for(i=1;i<=n;i++)
         {
            memset(f,0,sizeof(f));
            int x=0;
            </span><span style="font-size:24px;color:#3333ff;">for(j=i;j<=n;j++)
                {
                    if(!f[a[j]])
                   {
                    f[a[j]]=1;
                    x++;
                   }
                  ans[i][j]=x;
                }</span><span style="font-size:24px;color:#ff0000;">//统计一段数组中出现的数的个数,非常优美</span><span style="font-size:24px;color:#3333ff;">


         }
         int q,r,l;
         scanf("%d",&q);
         for(int i=1;i<=q;i++)
         {
             scanf("%d %d",&l,&r);
             printf("%d
",ans[l][r]);
         }
    }
    return 0;
}</span>


原文地址:https://www.cnblogs.com/smilesundream/p/6642549.html