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 ...
随机推荐
- cookie与web Storage
一.cookie 1. http头与cookie cookie是HTTP Cookie的简称.该标准要求: (1)服务器的HTTP响应头包含 Set-Cookie字段 响应头Eg: HTTP/1.1 ...
- java.util Properties使用记录
转:http://www.2cto.com/px/201006/47834.html 在java.util 包下面有一个类 Properties,该类主要用于读取以项目的配置文件(以.properti ...
- poj 1273 Drainage Ditches(最大流,E-K算法)
一.Description Every time it rains on Farmer John's fields, a pond forms over Bessie's favorite clove ...
- 人物-IT-张志东:张志东
ylbtech-人物-IT-张志东:张志东 张志东,广东东莞人,腾讯创办人之一,腾讯高级副总裁兼科技总裁,于1993年取得深圳大学理学学士学位,并于1996年取得华南理工大学计算机应用及系统架构硕士学 ...
- Python函数式编程(把函数作为参数传入)
map:接受两个参数(函数,Iterable),map将传入的函数依次作用于Iterable的每个元素,并且返回新的Iterable def f(x): return x*x r = map(f,[1 ...
- ES6学习之Reflect
Reflect对象与Proxy对象一样,也是 ES6 为了操作对象而提供的新 API Reflect设计目的: 将Object对象的一些明显属于语言内部的方法(比如Object.definePrope ...
- eclipse中使用Maven插件报错:-Dmaven.multiModuleProjectDirectory system property is not set. Check $M2_HOME environment variable and mvn script match.
步骤: 1.添加M2_HOME的环境变量 2.Preference->Java->Installed JREs->Edit 选择一个jdk 3.添加 -Dmaven.multiMod ...
- Spring开发环境搭建
----------------siwuxie095 Spring 是运行在 Java 环境下的开发框架,因此在开发前需要 准备以下相关软件: JDK 7 或者以上版本 Eclipse 4 或以上版本 ...
- C#中小数转化为string的问题
在C#中,把小数转化为string, 比如 45.12, 转换为string时,应该就是"45.12" 但是在项目开发中,开发法国的branch时,由于culture使用的是FR- ...
- Spring 属性配置
此文已由作者尧飘海授权网易云社区发布. 欢迎访问网易云社区,了解更多网易技术产品运营经验. 随着Spring的不断发展与完善,早期它的功能可能只看做是IOC(反转控制)的容器,或者其最大的亮点为DI( ...