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 信心题,还说是信心题,题目给的真好.但是一点都不像信心题. 又是一个新的算法,莫队算法 莫队算法是一个用数组就可以 ...
随机推荐
- db2实例、数据库、表空间
创建数据数据库之前必须创建实例,数据库是运行在实例之上的.实例在本质上是由一些后台进程和共享内存组成.实例相当于是数据库的一个容器,可以包含多个数据库,但是一个数据库只能由一个实例进行管理.相当于Wi ...
- OpenWrt——神奇的路由系统
鉴于最近大家对这个系统比较感兴趣而且疑问很多所以本渣就整理下我对这个系统的理解和最实用的802.1x认证的理解.还望大家多多互相交流. 如果您时间紧张直接看最后的步骤,时间充裕的请仔细阅读,理解. O ...
- UICollectionView介绍
文章原出处未知,如有朋友知道,请告诉我,我会补上. 1.1. Collection View 全家福: UICollectionView, UITableView, NSCollectionView ...
- HttpContext.Current.Cache使用文件依赖问题
HttpContext.Current.Cache.Insert("FCacheMs", tb, New CacheDependency(HttpContext.Current.S ...
- C# 的Brush 及相关颜色的操作 (并不是全转)
C# 的Brush 及相关颜色的操作 2013-12-13 14:08 4977人阅读 评论(0) 收藏 ...
- C# 使用 Abot 实现 爬虫 抓取网页信息 源码下载
下载地址 ** dome **
- Bootstrapper.cs
using System.Windows; using Microsoft.Practices.Prism.Modularity; using Microsoft.Practices.Prism.Un ...
- Shell基本功能&配置&Oh-My-Zsh
http://blog.csdn.net/yangcs2009/article/details/45720193
- Intent 匹配规则
1.在AndroidManifest.xml中可以为 每个 Activity,Service 设置多个Intent-Filter; 在系统启动和程序安装之后,android会收集AndroidMani ...
- iOS常用第三方
名称 作用 说明 AFNetworking 基于HTTP协议联网 SDWebImage 图片缓存和异步加载 YYWebImage 图片缓存和异步加载 Ono XML解析 Rapture ...