找规律发现只要找到两个相同数字之间,有多少个不同的数字,即为答案。

可以用树状数组离线处理。

坑点是卡有很多张,没用完的情况,后面的卡直接放在哪里,

就是

10 5

1 2 3 4 5 这样

开始数据要输出到10

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std;
#define inf (0x3f3f3f3f)
typedef long long int LL; #include <iostream>
#include <sstream>
#include <vector> const int maxn = 1e5 + ;
int n,m;
int c[maxn];//树状数组,多case的记得要清空
int lowbit (int x)//得到x二进制末尾0的个数的2次方 2^num
{
return x&(-x);
}
void add (int pos,int val)//在第pos位加上val这个值
{
while (pos<=m) { //n是元素的个数
c[pos] += val;
pos += lowbit(pos);
}
return ;
}
int get_sum (int pos) //求解:1--pos的总和
{
int ans = ;
while (pos) {
ans += c[pos];
pos -= lowbit(pos);
}
return ans;
}
int ans[maxn];
int a[maxn];
struct node {
int L,R,id;
bool operator < (const node &rhs) const {
return R < rhs.R;
}
}query[maxn];
int ansQ[maxn];
int book[maxn];
int cur[maxn];
vector<int>pos[maxn];
void work ()
{
int lenans = ;
scanf ("%d%d", &n, &m);
for (int i = ; i <= m; ++i) scanf ("%d", &a[i]);
for (int i = ; i <= n; ++i) cur[i] = ;
for (int i = ; i <= m; ++i) {
if (book[a[i]] == ) {
ans[++lenans] = a[i];
book[a[i]] = ;
}
pos[a[i]].push_back(i);
}
for (int i = ; i <= n; ++i) {
if (book[i] == ) {
ans[++lenans] = i;
}
}
memset (book, , sizeof book);
for (int i = ; i <= m; ++i) {
if (pos[a[i]].size() <= cur[a[i]]) {
query[i].L = i; query[i].R = m; query[i].id = i;
} else {
query[i].L = i; query[i].R = pos[a[i]][cur[a[i]]];
query[i].id = i;
}
cur[a[i]]++;
}
sort (query + , query + + m);
int now = ;
for (int i = ; i <= m; ++i) {
for (int j = now; j <= query[i].R; ++j) {
if (book[a[j]]) {
add(book[a[j]], -);
}
book[a[j]] = j;
add(j,);
}
now = query[i].R + ;
ansQ[query[i].id] = get_sum(query[i].R) - get_sum(query[i].L - ) - ;
}
for (int i = ; i <= lenans; ++i) {
printf ("%d ",ans[i]);
}
printf ("\n");
for (int i = ; i <= m; ++i) {
printf ("%d\n",ansQ[i]);
}
return ;
}
int main()
{
#ifdef local
freopen("data.txt","r",stdin);
#endif
work ();
return ;
}

URAL 2080 Wallet的更多相关文章

  1. URAL 2080 Wallet 莫队算法

    题目链接:Wallet 题意:给出n张卡片,k次使用.要求每次使用的卡片都在最上面.首先希望你合理的安排每张卡片的初始位置,并且输出.然后,问每次使用完卡片之后插入的位置上面有几张卡片,才能使得每次使 ...

  2. 【莫队算法】URAL - 2080 - Wallet

    http://www.cnblogs.com/icode-girl/p/5783983.html 要注意卡片没有都被使用的情况. #include<cstdio> #include< ...

  3. URAL 2080 莫队

    题意 有m种卡 给出卡的使用序列 要求每次从卡堆的顶部抽一张出来 刚好符合序列 输出初始 卡堆的排序 再输出每次抽出卡用后 卡插回卡堆的时候 这张卡上面有几张卡 初始排序很容易就可以搞出来 但是需要注 ...

  4. URAL 2078~2089

    URAL 2078~2089 A - Bowling game 题目描述:给出保龄球每一局击倒的球数,按照保龄球的规则,算出总得分的最小值和最大值. solution 首先是最小值:每一局第一球击倒\ ...

  5. BZOJ 2080: [Poi2010]Railway 双栈排序

    2080: [Poi2010]Railway Time Limit: 10 Sec  Memory Limit: 259 MBSubmit: 140  Solved: 35[Submit][Statu ...

  6. 后缀数组 POJ 3974 Palindrome && URAL 1297 Palindrome

    题目链接 题意:求给定的字符串的最长回文子串 分析:做法是构造一个新的字符串是原字符串+反转后的原字符串(这样方便求两边回文的后缀的最长前缀),即newS = S + '$' + revS,枚举回文串 ...

  7. ural 2071. Juice Cocktails

    2071. Juice Cocktails Time limit: 1.0 secondMemory limit: 64 MB Once n Denchiks come to the bar and ...

  8. ural 2073. Log Files

    2073. Log Files Time limit: 1.0 secondMemory limit: 64 MB Nikolay has decided to become the best pro ...

  9. ural 2070. Interesting Numbers

    2070. Interesting Numbers Time limit: 2.0 secondMemory limit: 64 MB Nikolay and Asya investigate int ...

随机推荐

  1. BZOJ3680:吊打XXX

    我对模拟退火的理解:https://www.cnblogs.com/AKMer/p/9580982.html 我对爬山的理解:https://www.cnblogs.com/AKMer/p/95552 ...

  2. iOS获取设备型号的方法

    1. [UIDevice currentDevice].model   自己写的看只抓到模拟器和iPhone.暂时不推荐. 2.自己写的找的方法再添加.直接  NSString * deviceMod ...

  3. 【转】LCS

    动态规划法 经常会遇到复杂问题不能简单地分解成几个子问题,而会分解出一系列的子问题.简单地采用把大问题分解成子问题,并综合子问题的解导出大问题的解的方法,问题求解耗时会按问题规模呈幂级数增加. 为了节 ...

  4. nefu26(求数的位数)

    Description 根据密码学需要,要计算某些数的阶乘的位数. Input 第一行为整数n ,接下来 n 行, 每行1个数m (1 ≤ m ≤ 10^7) . Output 输出m的阶乘的位数. ...

  5. 杂项:zabbix(WEB界面的提供分布式系统监视以及网络监视功能)

    ylbtech-杂项:zabbix(WEB界面的提供分布式系统监视以及网络监视功能) zabbix(音同 zæbix)是一个基于WEB界面的提供分布式系统监视以及网络监视功能的企业级的开源解决方案.z ...

  6. 移植完linux-3.4.2内核,启动系统后使用命令ifconfig -a查看网络配置,没有eth0

    问题: / # ifconfig / # ifconfig eth0  ifconfig: eth0: error fetching interface information: Device not ...

  7. 编译内核是出现:arch/arm/mm/tlb-v4wbi.S:64:error: too many positional arguments

    内核:Linux-3.4.2 编译内核出现arch/arm/mm/tlb-v4wbi.S:64:error: too many positional arguments 交叉工具链太老了,换新一点的. ...

  8. Python命令模块argparse学习笔记(三)

    参数组 ArgumentParser.add_argument_group(title=None, description=None) 默认情况下,当显示帮助消息时,ArgumentParser将命令 ...

  9. 将gridview 的数据导出EXCEL

    gridview数据 单击“导出EXCEL”按钮后        1.在上面的代码中,先将gridview绑定到指定的数据源中,然后在button按钮(用来做导出到EXCEL的)的事件中,写入相关的代 ...

  10. Javascript作用域和变量提升

    下面的程序是什么结果? var foo = 1; function bar() { if (!foo) { var foo = 10; } alert(foo); } bar(); 结果是10: 那么 ...