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 ...
随机推荐
- JVM体系结构之三:方法区之1
一.简介 方法区在JVM中也是一个非常重要的区域,它与堆一样,是被线程共享的区域.在方法区中,存储了每个类的信息(包括类的名称.方法信息.字段信息).静态变量.常量以及编译器编译后的代码等. 方法区( ...
- DCloud-MUI:Hello MUI2
ylbtech-DCloud: 1. <head> <meta charset="utf-8"> <title>Hello MUI</ti ...
- RESTEasy入门学习
RESTEasy是JBoss的开源项目之一,是一个RESTful Web Services框架.RESTEasy的开发者Bill Burke同时也是JAX-RS的J2EE标准制定者之一.JAX-RS是 ...
- [bzoj3670] [NOI2014] [lg2375] 动物园
nxt数组为KMP的next数组num[i]储存了i前面可以匹配的串的个数.先在KMP求nxt中顺便求出num最后再找到对于i的最大的前后缀不重叠的可匹配的j,ans*=(num[j]+1)%1000 ...
- js中一些小知识点总结--持续更新
以下知识点来自于编写高质量代码-改善JavaScript程序的188个建议,只用于自我知识的补充. 一.NaN 1.NaN是一个特殊的数量值,不表示一个数字,尽管下面的代码仍然是返回类型为number ...
- 值得细细品读的URL资源
Web安全渗透方面的资源指南: 知乎综合问答:https://www.zhihu.com/question/21914899 渗透测试经典演练系统:http://www.freebuf.com/sec ...
- 安装yum在ubnutu上
安装yum在ubnutu上1:首先检测是否安装了build-essential程序包 apt-get install build-essential2.安装yumapt-get install yum ...
- SpringSecurity02 表单登录、SpringSecurity配置类
1 功能需求 springSecuriy默认的登录窗口是一个弹出窗口,而且会默认对所有的请求都进行拦截:要求更改登录页面(使用表单登录).排除掉一些请求的拦截 2 编写一个springSecurity ...
- 理解setTimeout和setInterval
setTimeout和setInterval,这两个js函数都是用来定时执行.setTimeout执行一次,setInterval执行多次. 问题出现在今天,使用setInterval是,设置执行速度 ...
- linux下mysql远程链接
前言:我的系统是ubuntu,默认不支持mysql远程链接.接下来的步骤改变这点. 1,首先取消mysql本机绑定 编辑/etc/mysql/my.cnf 将”bind-address = 127.0 ...