How many different numbers

How many different numbers

http://acm.fzu.edu.cn/problem.php?pid=1656

 

 


Time Limit:1sMemory limit:32M
Accepted Submit:108Total Submit:649

 

Recently oaiei has encountered a problem. And he needs your help.
There are N numbers in an array and Q queries. For each query, there are two integers S and T. And the proplem is that there are how many different numbers in the index range [S,T] of the array (the array ‘s index is started from 1).

Input

 

There are multiple test cases. For each test case, the first line is an integer N (1<=N<=100000), indicating there are N numbers in the array. In the second line,there are N integers which are separated by a space. The third line is an integer Q(1<=Q<=1000), indicating there are Q queries. The following Q lines, for each line, there are two integers S and T(1<=S<=T<=N), indicating the index range of the array. The element value of the array will fit in a signed 32-bit integer.

 

Output

 

For each test, output an integer, indicating the number of different numbers in the index range [S,T] of the array.

 

Sample Input

10
1 2 3 3 3 3 4 -9 -9 10
2
1 5
5 10

Sample output

3
4 

有关结构体和排序的问题,定义一个结构体,用来存储输入的值和它所对应的位置。

然后对里面的值从小到大或从大到小进行排序,排序以后原来的数和它们所对应的位置并没有改变,

那么排序了每个数又会有新的位置,这时候的位置除去了重复的值,也就是说值一样的数他们所对

应的位置是一样的,可以用一个数组下标来存储,c[a.num]=t;a.num 是原来的位置,c[a.num]是现在

的位置,这样就总共有t+1个不相同的数,再定义一个数组b[t+1]={0},如果要求原来的数列中第j-

j+i个数有多少个不重复的数,就可以用b[c[j]]来求,因为c[j]是排列以后的位置,相同的数他们的位置

都是相同的,所以当这个位置的数数访问时b[c[j]]就标记为1

此外这里面还用到了调用头文件的排序方法,头文件#include<algorithm>里面的sort(a,a+n,down/up)函数,

其中down/up()是一个函数,这个函数用来决定是升还是降,

bool down(int x,int y) {return a>b;}(降序)  bool up(int x,int y) {return a<b;}(升序)

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值