zoukankan      html  css  js  c++  java
  • WUST Online Judge

    2174: 不同的数

    Time Limit: 1 Sec  Memory Limit: 128 MB   64bit IO Format: %lld
    Submitted: 459  Accepted: 71
    [Submit][Status][Web Board]

    Description

    n个数a1 a2 ... ai... an,m次询问,每次询问:从第x个数开始后面有多少不同的数。

    Input

    一组数据:

    第一行2个整数 n, m。

    接下来m行每行1个整数x。

    数据范围:1 <= n,m,x <= 100000, 0 <= ai <= 100000;

    Output

    对于每次查询输出一行,一个整数,表示从第x个数开始后面不同的数的个数。

    Sample Input

    8 3
    8 6 4 3 4 2 4 8
    6
    4
    2
    

    Sample Output

    3
    4
    5
    

    Author

    刘银虎

    代码如下:

    #include <stdio.h>
    
    int main() {
    	int i, n, m, x;
    	int a[100005], b[100005], c[100005];
    	while (scanf("%d%d", &n, &m) != EOF) {
    	    memset(b, 0, sizeof(b));
    	    memset(c, 0, sizeof(c));
    		for (i = 0; i < n; i++)
                scanf("%d", &a[i]);
            for (i = n - 1; i >= 0; i--) {
                if (!b[a[i]]) {
                    c[i] = c[i + 1] + 1;
                    b[a[i]]++;
                }
                else
                    c[i] = c[i + 1];
            }
    		while (m--) {
    			scanf("%d", &x);
    			printf("%d
    ", c[x - 1]);
    		}
    	}
    	return 0;
    }
    
    作者:McR
    本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
  • 相关阅读:
    vue-实践1
    node 基本使用
    vue通信
    初始AMD和Common.js
    vue正确引入第三方包
    常见的java设计模式
    springboot加ES实现全局检索
    Cookie丢失的原因
    动态SQL
    用Java实现给图片添加文字
  • 原文地址:https://www.cnblogs.com/mcr-tcp/p/9170749.html
Copyright © 2011-2022 走看看