Google Kickstart Round.B C. Diverse Subarray
这题又是万恶的线段树
maxx[j]存储的是 l = xxx, r = j的时候的答案
我们会让 l 从 1到n 的遍历中,查询线段树的[l, n]中最大的答案
因为query的下界是n,所以单次查询复杂度是logn
再其次这样做必须得再每次单元操作之后 对线段树 进行update
#include <iostream>
#include <fstream>
#include <vector>
#include <set>
#include <map>
#include <bitset>
#include <algorithm>
#include <iomanip>
#include <cmath>
#include <ctime>
#include <functional>
#include <unordered_set>
#include <unordered_map>
#include <queue>
#include <deque>
#include <stack>
#include <complex>
#include <cassert>
#include <random>
#include <cstring>
#include <numeric>
#define ll long long
#define ld long double
#define null NULL
#define all(a) a.begin(), a.end()
#define forn(i, n) for (int i = 0; i < n; ++i)
#define sz(a) (int)a.size()
#define lson l , m , rt << 1
#define rson m + 1 , r , rt << 1 | 1
template<class T> int gmax(T &a, T b) { if (b > a) { a = b; return 1; } return 0; }
template<class T> int gmin(T &a, T b) { if (b < a) { a = b; return 1; } return 0; }
using namespace std;
int n, s;
const int N = 1e5 + 5;
int A[N]; // origin input array
vector<int> pos[N]; // positions in initial array split by its value
int nowSize[N]; // the order of A[i] in their own position array
int input[N]; // use for segment tree input
int maxx[N << 2];
int lazy[N << 2];
void pushUp(int rt) {
maxx[rt] = max(maxx[rt << 1], maxx[rt << 1 | 1]);
}
void pushDown(int rt) {
if(lazy[rt]) {
maxx[rt << 1] += lazy[rt];
maxx[rt << 1 | 1] += lazy[rt];
lazy[rt << 1] += lazy[rt];
lazy[rt << 1 | 1] += lazy[rt];
lazy[rt] = 0;
}
}
void build(int l, int r, int rt) {
lazy[rt] = 0;
if(l == r) {
maxx[rt] = input[l];
return;
}
int m = (l + r) >> 1;
build(lson); build(rson);
pushUp(rt);
}
int query(int L, int R, int l, int r, int rt) {
if(L <= l && r <= R) return maxx[rt];
pushDown(rt);
int m = (l + r) >> 1;
int ret = -1;
if(L <= m) ret = max(ret, query(L, R, lson));
if(R > m) ret = max(ret, query(L, R, rson));
return ret;
}
void update(int c, int L, int R, int l, int r, int rt) {
if(L <= l && r <= R) {
maxx[rt] += c;
lazy[rt] += c;
return;
}
pushDown(rt);
int m = (l + r) >> 1;
if(L <= m) update(c, L, R, lson);
if(R > m) update(c, L, R, rson);
pushUp(rt);
}
int main() {
int T;
scanf("%d", &T);
for(int cas = 1; cas <= T; ++ cas) {
scanf("%d %d", &n, &s);
for(int i = 0; i < N; ++i) pos[i].clear();
for(int i = 1; i <= n; ++i) scanf("%d", &A[i]);
for(int i = 1; i <= n; ++i) {
pos[A[i]].push_back(i);
nowSize[i] = pos[A[i]].size();
}
for(int i = 0; i < N; ++i) pos[i].push_back(n + 1);
input[0] = 0;
for(int i = 1; i <= n; ++i) {
input[i] = input[i-1] + 1;
if(nowSize[i] == s + 1) input[i] -= s + 1;
else if(nowSize[i] > s + 1) input[i] --;
}
build(1, n, 1);
int ans = -1;
for(int i = 1; i <= n; ++i) {
ans = max(ans, query(i, n, 1, n, 1));
int num = A[i];
if(nowSize[i] + s < pos[num].size()) {
int tmp = pos[num][nowSize[i] + s - 1];
int tmp2 = pos[num][nowSize[i] + s];
update(-1, i, tmp - 1, 1, n, 1);
update(s, tmp, tmp2 - 1, 1, n, 1);
} else {
update(-1, i, n, 1, n, 1);
}
}
printf("Case #%d: %d\n", cas, ans);
}
return 0;
}
Google Kickstart Round.B C. Diverse Subarray的更多相关文章
- google Kickstart Round G 2017 三道题题解
A题:给定A,N,P,计算A的N!次幂对P取模的结果. 数据范围: T次测试,1 ≤ T ≤ 100 1<=A,N,P<=105 快速幂一下就好了.O(nlogn). AC代码: #inc ...
- 2019 google kickstart round A
第一题: n个人,每个人有一个对应的技能值s,现在要从n个人中选出p个人,使得他们的技能值相同. 显然,如果存在p个人的技能值是相同的,输出0就可以了.如果不存在,就要找出p个人,对他们进行训练,治他 ...
- google Kickstart Round F 2017 四道题题解
Problem A. Kicksort 题意抽象一下为: 对于一个每次都从数列正中间取划分数的快速排序,给定一个1-n的排列,问快排的复杂度对于这个排列是否会退化为最坏复杂度. 数据范围: 测试组数1 ...
- Google Kickstart Round E 2018 B. Milk Tea
太蠢了,,,因为初始化大数据没过,丢了10分,纪念一下这个错误 大概思路:先求出让损失值最小的排列,由已生成的这些排列,通过更改某一个位置的值,生成下一个最优解,迭代最多生成m+1个最优解即可,遍历求 ...
- Google Kickstart在线测试规则以及注意事项
谷歌招聘在如火如荼的进行中,进谷歌都需要经过谷歌kickstart在线测试,然后过了之后还有五轮的面试- -.好吧毕竟你待遇高,你强你有理.. 下面介绍一下进谷歌的第一关google kickstar ...
- Google kickstart 2022 Round A题解
Speed Typing 题意概述 给出两个字符串I和P,问能否通过删除P中若干个字符得到I?如果能的话,需要删除字符的个数是多少? 数据规模 \[1≤|I|,|P|≤10^5 \] 双指针 设置两个 ...
- google kickstart 2018 round D A Candies
思路: 对于small数据,由于求和及奇数数量两个限制条件均满足区间单调性,可以直接使用尺取法(滑动窗口法)求解. 对于large数据,奇数数量依然是满足区间单调性的.首先使用尺取法,找到所有满足奇数 ...
- [Google Codejam] Round 1A 2016 - The Last Word
[Problem Description] Problem On the game show The Last Word, the host begins a round by showing the ...
- Let Me Count The Ways(Kickstart Round H 2018)
题目链接:https://code.google.com/codejam/contest/3324486/dashboard#s=p2 题目: 思路: 代码实现如下: #include <set ...
随机推荐
- [Hive_1] Hive 基本概念
Hive 系列01 Hive 简介 & Hive 应用场景 & Hive 与 Hadoop 的关系 & Hive 与传统数据库对比 1. Hive 简介 [ 官方介绍 ] Ap ...
- 【转】Mybatis学习---MyBatis知识、原始Dao开发和mapper代理开发
[原文]https://www.toutiao.com/i6594610137560777223/ 一.什么是MyBatis MyBatis 是一款优秀的持久层框架,它支持定制化 SQL.存储过程以及 ...
- ELK安装过程
官方安装文档:https://www.elastic.co/guide/en/elasticsearch/reference/current/zip-targz.html 官方硬件和配置项推荐:htt ...
- Post-installation steps for Chromium | Fedora
Flash 插件安装 网址: https://fedora.pkgs.org/ 下载: chromium-pepper-flash-version.fc28.x86_64.rpm 安装后重启浏览器 解 ...
- 微信支付的notify.php中如何获取订单号(php版)
不要直接使用demo中的notify.php,重写notify.php,继承WxPayNotify(可参考微信api),具体如下: require_once "WxPay.Api.php&q ...
- JS播放声音
<html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>JS播放声音</tit ...
- weex+vue2.x 踩坑实录(不定期更新)
执行 npm start 显示空白页面 这个是开始使用weex就出现的一个大坑,说实话对新手真的很不友好. 1.打开控制台显示:Cannot assign to read only property ...
- oracle kill 锁
select object_name as 对象名称,s.sid,s.serial#,p.spid as 系统进程号 from v$locked_object l , dba_objects o , ...
- VC++环境下单文档SDI与OpenGL多视图分割窗口的实现-类似3DMAX的主界面
本文主要讲述如何在VC++环境下实现单文档SDI与OpenGL多视图分割窗口,最终的界面类似3DMAX的主界面.首先给出我实现的效果图: 整个实现过程网络上有很多零散的博文,请各位自行搜索,在基于对话 ...
- leetcode322—Coin Change
You are given coins of different denominations and a total amount of money amount. Write a function ...