Sereja and Suffixes(思维)
Time Limit:1000MS Memory Limit:262144KB 64bit IO Format:%I64d & %I64u
Description
Sereja has an array a, consisting of n integers a1, a2, ..., an. The boy cannot sit and do nothing, he decided to study an array. Sereja took a piece of paper and wrote out m integers l1, l2, ..., lm(1 ≤ li ≤ n). For each number li he wants to know how many distinct numbers are staying on the positions li, li + 1, ..., n. Formally, he want to find the number of distinct numbers among ali, ali + 1, ..., an.?
Sereja wrote out the necessary array elements but the array was so large and the boy was so pressed for time. Help him, find the answer for the described question for each li.
Input
The first line contains two integers n and m(1 ≤ n, m ≤ 105). The second line contains n integers a1, a2, ..., an(1 ≤ ai ≤ 105) — the array elements.
Next m lines contain integers l1, l2, ..., lm. The i-th line contains integer li(1 ≤ li ≤ n).
Output
Print m lines — on the i-th line print the answer to the number li.
Sample Input
10 10 1 2 3 4 1 2 3 4 100000 99999 1 2 3 4 5 6 7 8 9 10
6 6 6 6 6 5 4 3 2 1
题解:让求x到n的不同数字的个数;打表,对于m次询问,如果每次都暴力肯定超了,由于n是固定的,所以打下表,从后往前;
代码:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<set>
#include<map>
using namespace std;
const int MAXN = 1e5+;
class Ary{
public:
int n,m;
int a[MAXN];
int dp[MAXN];
set<int>st;
Ary(int n,int m):n(n),m(m){
for(int i = ; i <= n; i++){
cin >> a[i];
}
}
int work(){
int cnt = ;
for(int i = n; i >= ; i--){
if(!st.count(a[i])){
cnt++;
st.insert(a[i]);
}
dp[i] = cnt;
}
//cout << endl;
}
~Ary(){
//cout << "对象毁灭" << endl;
}
};
int main(){
int n,m;
while(~scanf("%d%d", &n, &m)){
Ary boy(n,m);
int temp;
boy.work();
while(m--){
scanf("%d",&temp);
printf("%d\n",boy.dp[temp]);
}
}
return ;
}
Sereja and Suffixes(思维)的更多相关文章
- Codeforces Round #215 (Div. 2) B. Sereja and Suffixes map
B. Sereja and Suffixes Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/problemset ...
- B. Sereja and Suffixes
B. Sereja and Suffixes time limit per test 1 second memory limit per test 256 megabytes input standa ...
- B. Sereja and Suffixes(cf)
http://codeforces.com/problemset/problem/368/B B. Sereja and Suffixes time limit per test 1 second m ...
- Codeforces Round #215 (Div. 2) B. Sereja and Suffixes
#include <iostream> #include <vector> #include <algorithm> #include <set> us ...
- CodeForces 368B Sereja and Suffixes
题意:给你一个序列,问你从l位置到结尾有多少个不同的数字. 水题,设dp[i]表示从i位置到结尾不同数字的个数,那么dp[i] = dp[i+1] + (vis[a[i]] == 0),在O(n)时间 ...
- Sereja and Suffixes
Codeforces Round #215 (Div. 2) B:http://codeforces.com/problemset/problem/368/B 题意:给你一个序列,然后查询i--n中没 ...
- cf B. Sereja and Suffixes
http://codeforces.com/contest/368/problem/B 从后往前找一遍就可以. #include <cstdio> #include <cstring ...
- Codeforces Round #527 (Div. 3) C. Prefixes and Suffixes (思维,字符串)
题意:给你某个字符串的\(n-1\)个前缀和\(n-1\)个后缀,保证每个所给的前缀后缀长度从\([1,n-1]\)都有,问你所给的子串是前缀还是后缀. 题解:这题最关键的是那两个长度为\(n-1\) ...
- Codeforces 1092C Prefixes and Suffixes(思维)
题目链接:Prefixes and Suffixes 题意:给定未知字符串长度n,给出2n-2个字符串,其中n-1个为未知字符串的前缀(n-1个字符串长度从1到n-1),另外n-1个为未知字符串的后缀 ...
随机推荐
- Amdroid示例:利用Gson生成或解析json
转自:http://www.cnblogs.com/liqw/p/4266209.html 目前手机端和服务端数据交流格式一般是json,而谷歌提供了Gson来解析json.下载Gson:https: ...
- STL源码剖析读书笔记之vector
STL源码剖析读书笔记之vector 1.vector概述 vector是一种序列式容器,我的理解是vector就像数组.但是数组有一个很大的问题就是当我们分配 一个一定大小的数组的时候,起初也许我们 ...
- Ueditor文本编辑器(新浪SAE平台版本) - 下载频道 - CSDN.NET
Ueditor文本编辑器(新浪SAE平台版本) - 下载频道 - CSDN.NET Ueditor文本编辑器(新浪SAE平台版本)
- PHP - 多维数组
多维数组指的是包含一个或多个数组的数组. PHP 能理解两.三.四或五级甚至更多级的多维数组.不过,超过三级深的数组对于大多数人难于管理. 注释:数组的维度指示您需要选择元素的索引数. 对于二维数组, ...
- Spring定时任务的几种实现(转自iteye网gong1208)
原文地址: http://gong1208.iteye.com/blog/1773177 以下为正文: 近日项目开发中需要执行一些定时任务,比如需要在每天凌晨时候,分析一次前一天的日志信息,借此机会整 ...
- mysql、sqlServer、hsql、oracle、db2各数据库支持的字段类型与最大精度
- python3 module中__init__.py的需要注意的地方
网上关于__init__.py的作用的资料到处都是,我在此就不再啰嗦哪些了. 若有需要.请各位看官去搜搜即可. 最近刚开始用Python3 就遇到了这个比较有意思的事情 闲言少叙,下面要介绍的是pyt ...
- Android多媒体开发-- android中OpenMax的实现整体框架
1.android中用openmax来干啥? android中的AwesomePlayer就 是用openmax来做(code)编解码,其实在openmax接口设计中,他不光能用来当编解码.通过他的组 ...
- URL 对特殊字符的处理
看到很多人说可以通过转移字符来进行转义,避免URL在请求的时候出错. 现在有了更好的方法了.不然还不得把半个ASCII码表给进行转义了! import java.io.UnsupportedEncod ...
- 【熊猫】POS销售
select a.itemcode,b.itemname,b.spec,b.unit,b.rprice,sum(a.rqty) rqtyfrom tm_possale_detail a,sys_ite ...