URAL 2080 Wallet
找规律发现只要找到两个相同数字之间,有多少个不同的数字,即为答案。
可以用树状数组离线处理。
坑点是卡有很多张,没用完的情况,后面的卡直接放在哪里,
就是
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的更多相关文章
- URAL 2080 Wallet 莫队算法
题目链接:Wallet 题意:给出n张卡片,k次使用.要求每次使用的卡片都在最上面.首先希望你合理的安排每张卡片的初始位置,并且输出.然后,问每次使用完卡片之后插入的位置上面有几张卡片,才能使得每次使 ...
- 【莫队算法】URAL - 2080 - Wallet
http://www.cnblogs.com/icode-girl/p/5783983.html 要注意卡片没有都被使用的情况. #include<cstdio> #include< ...
- URAL 2080 莫队
题意 有m种卡 给出卡的使用序列 要求每次从卡堆的顶部抽一张出来 刚好符合序列 输出初始 卡堆的排序 再输出每次抽出卡用后 卡插回卡堆的时候 这张卡上面有几张卡 初始排序很容易就可以搞出来 但是需要注 ...
- URAL 2078~2089
URAL 2078~2089 A - Bowling game 题目描述:给出保龄球每一局击倒的球数,按照保龄球的规则,算出总得分的最小值和最大值. solution 首先是最小值:每一局第一球击倒\ ...
- BZOJ 2080: [Poi2010]Railway 双栈排序
2080: [Poi2010]Railway Time Limit: 10 Sec Memory Limit: 259 MBSubmit: 140 Solved: 35[Submit][Statu ...
- 后缀数组 POJ 3974 Palindrome && URAL 1297 Palindrome
题目链接 题意:求给定的字符串的最长回文子串 分析:做法是构造一个新的字符串是原字符串+反转后的原字符串(这样方便求两边回文的后缀的最长前缀),即newS = S + '$' + revS,枚举回文串 ...
- ural 2071. Juice Cocktails
2071. Juice Cocktails Time limit: 1.0 secondMemory limit: 64 MB Once n Denchiks come to the bar and ...
- ural 2073. Log Files
2073. Log Files Time limit: 1.0 secondMemory limit: 64 MB Nikolay has decided to become the best pro ...
- ural 2070. Interesting Numbers
2070. Interesting Numbers Time limit: 2.0 secondMemory limit: 64 MB Nikolay and Asya investigate int ...
随机推荐
- Percona Xtrabackup 备份MySQL 实例(转)
老规矩,开场白,刚开始用mysqldump,备份100G+的数据库,再加上服务器繁忙,备份速度像蜗牛似的,于是寻找更高效的备份方法.网上都说用xtrabackup比较适合备份大的数据库,而且备份效率也 ...
- C#程序运行计时
var stp = new System.Diagnostics.Stopwatch(); stp.Start();//计时启动 ..........程序代码........... stp.Stop( ...
- ES6学习之Symbol
定义:一种新的原始数据类型,表示独一无二的值 const a = Symbol(); const b = Symbol("foo") //接收参数的Symbol,参数表示对Symb ...
- numpy.ones_like(a, dtype=None, order='K', subok=True)返回和原矩阵一样形状的1矩阵
Return an array of ones with the same shape and type as a given array. Parameters: a : array_like Th ...
- C语言学习笔记--单引号和双引号
(1)C 语言中单引号用来表示字符字面量(是个数值)被编译为对应的 ASCII 码 (2)C 语言中双引号用来表示字符串字面量(是个指针)被编译为对应的内存地址 例如:'a'表示字符字面量(97),在 ...
- JS中数组方法小总结
1.array.concat(item……) 返回:一个新数组 该方法产生一个新数组,它包含一份array的浅复制,并把一个或多个参数item附加在其后.如果参数item是一个数组,那么它的每个元素会 ...
- ansible一键部署LAMP
一.实现ansible跟节点间无密码访问,不会配置的请看 文章 . 二.创建目录 $ mkdir -p playbooks/{files,templates} 三.创建php测试文件index.p ...
- Python最小二乘法解非线性超定方程组
求解非线性超定方程组,网上搜到的大多是线性方程组的最小二乘解法,对于非线性方程组无济于事. 这里分享一种方法:SciPy库的scipy.optimize.leastsq函数. import numpy ...
- 怀旧系列(3)----Pascal
Pascal语言是高中时代2000年左右为了参加计算机竞赛学习的一门语言(Turbo Pascal7.0).据说这么语言的结构化非常好,非常适合青少年形成一定的编程思想.但是现在的角度想想都是扯淡,现 ...
- 26.【转载】挖洞技巧:绕过短信&邮箱轰炸限制以及后续
邮箱轰炸可能对企业来说危害很小,但对用户危害很大.短信轰炸相比邮箱轰炸,带来的危害涉及到企业和用户. 那么这些问题都存在在哪些方面呢? ①:登录处 ②:注册处 ③:找回密码处 ④:绑定处 ⑤:活动领取 ...