题目链接:Wallet

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

思路:初始位置很容易得到。每次插入卡片时上面的卡片数,就是该卡片两次使用之间有多少个不同的数字。于是,问题变成:所有的两个相同数字之间有多少个不同的数字。

嗯..不分块会TLE的...乱用STL也会TLE的....

#include <stdio.h>
#include <string.h>
#include <iostream>
#include <map>
#include <vector>
#include <algorithm>
#include <math.h>
#define maxn 100010
using namespace std; int a[maxn];
int num[maxn];
int Ans, L, R;
int Init[maxn], cnt;
int n, k;
int los[maxn]; void getInit() {
bool vis[maxn];
memset(vis, 0, sizeof(vis));
int id = 0;
for (int i=1; i<=k; ++i) {
if (!vis[a[i]]) {
vis[a[i]]++;
Init[id++] = a[i];
}
}
for (int i=1; i<=n; ++i) {
if (!vis[i]) {
Init[id++] = i;
}
}
} struct Node {
int id;
int l, r;
}Query[maxn]; vector<int> pos[maxn];
int ans[maxn]; void init() {
memset(num, 0, sizeof(num));
Ans = 1, L = 1, R = 1;
cnt = 0;
for (int i=0; i<=n; ++i) {
pos[i].clear();
}
memset(ans, 0, sizeof(ans));
} void getQuery() {
for (int i=1; i<=k; ++i) {
pos[a[i]].push_back(i);
}
for (int i=1; i<=n; ++i) {
int len = pos[i].size();
if (len == 0) continue;
for (int j=0; j<len-1; ++j) {
Query[cnt].l = pos[i][j];
Query[cnt].r = pos[i][j+1];
Query[cnt].id = pos[i][j];
cnt++;
}
ans[pos[i][len-1]] = n-1;
}
} bool cmp(Node a, Node b) {
if (los[a.l] != los[b.l]) return a.l < b.l;
return a.r < b.r;
} void Del(int x) {
num[a[x]]--;
if (!num[a[x]]) {
Ans--;
}
} void Add(int x) {
if (!num[a[x]]) {
Ans++;
}
num[a[x]]++;
} int main() {
//freopen("in.cpp", "r", stdin);
while(~scanf("%d%d", &n, &k)) {
int kuai = (int)sqrt(k);
for (int i=1; i<=k; ++i) {
scanf("%d", &a[i]);
los[i] = i/kuai;
}
init();
getInit();
num[a[1]]++;
getQuery();
sort(Query, Query+cnt, cmp);
for (int i=0; i<cnt; ++i) {
//cout << Query[i].l << " " << Query[i].r << " " << L << " " << R << "#\n";
while(Query[i].l > L) {
Del(L);
L++;
}
while(Query[i].l < L) {
L--;
Add(L);
}
while(Query[i].r > R) {
R++;
Add(R);
}
while(Query[i].r < R) {
Del(R);
R--;
}
ans[Query[i].id] = Ans-1;
//cout << Ans << "@\n";
}
for (int i=0; i<n; ++i) {
if (i != 0) printf(" ");
printf("%d", Init[i]);
}
printf("\n");
for (int i=1; i<=k; ++i) {
printf("%d\n", ans[i]);
}
}
return 0;
}

URAL 2080 Wallet 莫队算法的更多相关文章

  1. NBUT 1457 莫队算法 离散化

    Sona Time Limit:5000MS     Memory Limit:65535KB     64bit IO Format: Submit Status Practice NBUT 145 ...

  2. BZOJ 2038: [2009国家集训队]小Z的袜子(hose) [莫队算法]【学习笔记】

    2038: [2009国家集训队]小Z的袜子(hose) Time Limit: 20 Sec  Memory Limit: 259 MBSubmit: 7687  Solved: 3516[Subm ...

  3. NPY and girls-HDU5145莫队算法

    Time Limit: 8000/4000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Problem Description ...

  4. Codeforces617 E . XOR and Favorite Number(莫队算法)

    XOR and Favorite Number time limit per test: 4 seconds memory limit per test: 256 megabytes input: s ...

  5. Bzoj 2038---[2009国家集训队]小Z的袜子(hose) 莫队算法

    题目链接 http://www.lydsy.com/JudgeOnline/problem.php?id=2038 Description 作为一个生活散漫的人,小Z每天早上都要耗费很久从一堆五颜六色 ...

  6. 【BZOJ-3052】糖果公园 树上带修莫队算法

    3052: [wc2013]糖果公园 Time Limit: 200 Sec  Memory Limit: 512 MBSubmit: 883  Solved: 419[Submit][Status] ...

  7. 莫队算法 2038: [2009国家集训队]小Z的袜子(hose)

    链接:http://www.lydsy.com/JudgeOnline/problem.php?id=2038 2038: [2009国家集训队]小Z的袜子(hose) Time Limit: 20 ...

  8. Codeforces 617E XOR and Favorite Number(莫队算法)

    题目大概说给一个序列,多次询问区间异或和为k的连续子序列有多少个. 莫队算法,利用异或的性质,通过前缀和求区间和,先处理出序列各个前缀和,然后每次区间转移时维护i以及i-1前缀和为某数的个数并增加或减 ...

  9. 信心题--FUOJ2226(莫队算法)

    http://acm.fzu.edu.cn/problem.php?pid=2226 信心题,还说是信心题,题目给的真好.但是一点都不像信心题. 又是一个新的算法,莫队算法 莫队算法是一个用数组就可以 ...

随机推荐

  1. c语言数据结构和算法库--cstl---王博--相关网站和博客

    1.官网 http://libcstl.org/download.html 2.下载地址 http://www.pudn.com/downloads171/sourcecode/os/detail79 ...

  2. HTML5 Canvas arc()函数//////////////////////(转)

    HTML5 Canvas arc()函数   实例 创建一个圆形: var c=document.getElementById("myCanvas"); var ctx=c.get ...

  3. 白话LINQ系列1---什么是LINQ?

    一.本系列目标 1.理解LINQ: 2.能写得复杂的LINQ语句(比如:动态查询): 3.理解表达式树及相关概念: 4.熟练运用LINQ写出优美的代码(希望一起努力,最终达到): 二.LINQ为何物? ...

  4. mysql之各种命令总结

    1:使用SHOW语句找出在服务器上当前存在什么数据库:mysql> SHOW DATABASES;2:2.创建一个数据库MYSQLDATAmysql> CREATE DATABASE MY ...

  5. c3p0数据库连接池使用

  6. Azure Table storage 之改进DynamicTableEntity类为其添加动态语言扩展

    在之前的一篇文章中提到,storage类库中包含一个可以用来动态获取Azure table storage 表结构的类-DynamicTableEntity. 我们可以通过这个类,我们无需为每一个表提 ...

  7. Linux按键驱动程序设计--从简单到不简单【转】

    本文转载自:http://blog.csdn.net/coding__madman/article/details/51399353 混杂设备驱动模型: 1. 混杂设备描述 在Linux系统中,存在一 ...

  8. css样式控制鼠标滑过显示

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  9. shiro 从入门到放弃

    Apache Shiro是Java的一个安全框架.目前,使用Apache Shiro的人越来越多,因为它相当简单,对比Spring Security,可能没有Spring Security做的功能强大 ...

  10. javaScript实现修改输入框之后标红

    <html> <title>实现标红</title> <script type="text/javascript">  functi ...