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 信心题,还说是信心题,题目给的真好.但是一点都不像信心题. 又是一个新的算法,莫队算法 莫队算法是一个用数组就可以 ...
随机推荐
- smartdraw2013破解方法
smartdraw是一个非常好的画图工作,最大的优点就是支持多种图形,采用模板的方式在线扩充,可以快速画出你想要的图形,具体的介绍见其他资料. 这里是我自己的破解办法. 网上的下载都包含破解工具,但是 ...
- 【转】赞一下huicpc035
以下转自:http://hi.baidu.com/fpkelejggfbfimd/item/99421eaefa93a814a9cfb722 本来以为HNU的huicpc035和我一样退役了,后来听说 ...
- C# 程序中的变量
变量命名规则: 不能是c#关键字 由字母,数字,下划线构成 第一个不能是数字 不要超过31个字符 不能是函数名,类名 c#是大小写敏感的. 本质上,数据类型就是他存储方式和他参与运算的抽象. c#的数 ...
- java开发_读写txt文件操作
package com.mi.util; import java.io.BufferedReader; import java.io.File; import java.io.FileInputStr ...
- Repeater嵌套绑定Repeater
前台Html代码 <asp:Repeater runat="server" ID="rpList" OnItemDataBound="rpLis ...
- core animation (转)
iOS Core Animation 简明系列教程 看到无数的CA教程,都非常的难懂,各种事务各种图层关系看的人头大.自己就想用通俗的语言翻译给大家听,尽可能准确表达,如果哪里有问题,请您指出我会尽快 ...
- CMD执行BCP命令
C:\>BCP "EXEC GetU '2016-7-11' ,'-1'" queryout "C:\\C3Marketing\SummaryReport_test ...
- WEB前端的原理及组成
1:认识WEB前端的组成部分和相关专业术语!具体的总结如下:
- spring cache
spring-ehcache.xml文件内容如下: <?xml version="1.0" encoding="UTF-8"?> <beans ...
- 【Spring】Spring的定时任务
> 参考的优秀文章 Task Execution and Scheduling > 版本说明 <dependencies> <dependency> <gro ...