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

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

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

就是

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. JAVA的推荐书目

    本文是摘自别人的网站,自己读的书少,谨以此作为自己要读的书的一个书目列表吧. 原文地址:http://blog.sina.com.cn/s/blog_6aa1784101011hl5.html 正文: ...

  2. lvs-nat搭建httpd

    拓扑图: #172.16.252.10 [root@~ localhost]#route -n Kernel IP routing table Destination Gateway Genmask ...

  3. Mysql 增量备份和全量备份

    全量备份: # vim /root/DBFullyBak.sh //添加以下内容 #!/bin/bash # Program # use mysqldump to Fully backup mysql ...

  4. ViewPageIndicator--仿网易的使用

    仿微信(网易的界面) 第一步: AndroidManifest.xml 的配置 <?xml version="1.0" encoding="utf-8"? ...

  5. CountDownLatch分析

    1 什么是CountDownLatch呢? 先看看官网的定义 :一种同步帮助,允许一个或多个线程等待,直到在其他线程中执行的一组操作完成. 现在由我来解释什么是CountDownLatch吧:比如说我 ...

  6. linux下将编译错误输出到一个文本文件

    linux下将编译错误输出到一个文本文件 command > filename 把把标准输出重定向到一个新文件中 command > > filename 把把标准输出重定向到一个文 ...

  7. Tomcat+Nginx实现动静分离

    Tomcat是我们经常用的服务器,轻便快捷,但是数据量大的时候,会影响访问.响应速度,这时Nginx就出现了. Nginx可做反向代理.负载均衡.动态与静态资源的分离的工作,这里我们就用它来做动静分离 ...

  8. Static与Const的区别

    static static局部变量 将一个变量声明为函数的局部变量,那么这个局部变量在函数执行完成之后不会被释放,而是继续保留在内存中 static 全局变量 表示一个变量在当前文件的全局内可访问 s ...

  9. 业务逻辑:完成基于CRM地址完全匹配的自动分单业务逻辑

    思路: 后台系统的业务接口服务处理接收到的数据并使用Webservice技术来远程调用CRM系统的业务接口服务来进行定区的查询操作,随后从该定区中匹配一个快递员来分配工单并发送短信通知取件 操作步骤: ...

  10. Ubuntu12.04安装 vsftpd

    Ubuntu12.04 FTP 的配置   ubuntu安装ftp服务器 1: 安装vsftpd ~$ sudo apt-get install vsftpd 2: 配置vsftpd 2.1 修改vs ...