URAL 2080 Wallet 莫队算法
题目链接: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 莫队算法的更多相关文章
- NBUT 1457 莫队算法 离散化
Sona Time Limit:5000MS Memory Limit:65535KB 64bit IO Format: Submit Status Practice NBUT 145 ...
- BZOJ 2038: [2009国家集训队]小Z的袜子(hose) [莫队算法]【学习笔记】
2038: [2009国家集训队]小Z的袜子(hose) Time Limit: 20 Sec Memory Limit: 259 MBSubmit: 7687 Solved: 3516[Subm ...
- NPY and girls-HDU5145莫队算法
Time Limit: 8000/4000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Problem Description ...
- Codeforces617 E . XOR and Favorite Number(莫队算法)
XOR and Favorite Number time limit per test: 4 seconds memory limit per test: 256 megabytes input: s ...
- Bzoj 2038---[2009国家集训队]小Z的袜子(hose) 莫队算法
题目链接 http://www.lydsy.com/JudgeOnline/problem.php?id=2038 Description 作为一个生活散漫的人,小Z每天早上都要耗费很久从一堆五颜六色 ...
- 【BZOJ-3052】糖果公园 树上带修莫队算法
3052: [wc2013]糖果公园 Time Limit: 200 Sec Memory Limit: 512 MBSubmit: 883 Solved: 419[Submit][Status] ...
- 莫队算法 2038: [2009国家集训队]小Z的袜子(hose)
链接:http://www.lydsy.com/JudgeOnline/problem.php?id=2038 2038: [2009国家集训队]小Z的袜子(hose) Time Limit: 20 ...
- Codeforces 617E XOR and Favorite Number(莫队算法)
题目大概说给一个序列,多次询问区间异或和为k的连续子序列有多少个. 莫队算法,利用异或的性质,通过前缀和求区间和,先处理出序列各个前缀和,然后每次区间转移时维护i以及i-1前缀和为某数的个数并增加或减 ...
- 信心题--FUOJ2226(莫队算法)
http://acm.fzu.edu.cn/problem.php?pid=2226 信心题,还说是信心题,题目给的真好.但是一点都不像信心题. 又是一个新的算法,莫队算法 莫队算法是一个用数组就可以 ...
随机推荐
- angular懒加载机制 刷新后无法回退解决方案
今天在项目中遇到一个很奇怪的问题,使用oclazyload来懒加载angular的模块,刷新页面后,单击回退按钮无法返回上一个页面.估计是使用懒加载机制销毁了angular内部的state关联,导致无 ...
- windows nslookup、tracert 常用命令
nslookup www.baidu.com 可以指定查询的类型,可以查到DNS记录的生存时间还可以指定使用哪个DNS服务器进行解释. tracert www.baidu.com 路由
- 13. 星际争霸之php设计模式--正面模式
题记==============================================================================本php设计模式专辑来源于博客(jymo ...
- Java连接Redis (key-value存储系统)
Redis简介: redis是一个性能非常优秀的内存数据库,通过key-value存储系统.和Memcached类似,它支持存储的value类型相对更多,包括string(字符串).list(链表). ...
- ilbc编解码
针对国内的博客或者技术论坛对 ILBC的论述都是把文章抄来抄去, 本人在此对 ILBC的具体代码实现详细列出代码. ILBC是由Global IP Sound公司提出的一种专为包交换网络通信设计的编解 ...
- javaIO框架小析
IO即数据读写.数据是应用的中心要素,而数据读写的能力和可扩展性是编程平台的基础支撑. 概念框架 方式: 字节流 Byte 和 字符流 Char 方向: 输入 Input 和 输出 Output : ...
- 引用参数,值参数,ref,out
1,一个参数只有在引用的时候才能改变其值,这是一种情况 2,一个参数在引用后要永久的改变其值(可以用返回参数的形式) 3,多个参数在引用后要永久的改变其值或者多个参数中的部分(返回参数就适合了,因为只 ...
- C#:绘图问题
1.设置DPI Bitmap bitmap2 = new Bitmap((int)w, (int)h); bitmap2.SetResolution(, ); 2.设置Graphic(如:去锯齿等) ...
- RCP:如何保存TaskList及如何获取TaskList
如果我们在Eclipse RCP程序中添加TaskList View,用来管理Task或者TODO项,如下代码: PlatformUI.getWorkbench().getActiveWorkbenc ...
- [问题2014S03] 复旦高等代数II(13级)每周一题(第三教学周)
[问题2014S03] 设 \(A\in M_n(\mathbb R)\) 是非异阵并且 \(A\) 的 \(n\) 个特征值都是实数. 若 \(A\) 的所有 \(n-1\) 阶主子式之和等于零, ...