这题又是万恶的线段树

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的更多相关文章

  1. 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 ...

  2. 2019 google kickstart round A

    第一题: n个人,每个人有一个对应的技能值s,现在要从n个人中选出p个人,使得他们的技能值相同. 显然,如果存在p个人的技能值是相同的,输出0就可以了.如果不存在,就要找出p个人,对他们进行训练,治他 ...

  3. google Kickstart Round F 2017 四道题题解

    Problem A. Kicksort 题意抽象一下为: 对于一个每次都从数列正中间取划分数的快速排序,给定一个1-n的排列,问快排的复杂度对于这个排列是否会退化为最坏复杂度. 数据范围: 测试组数1 ...

  4. Google Kickstart Round E 2018 B. Milk Tea

    太蠢了,,,因为初始化大数据没过,丢了10分,纪念一下这个错误 大概思路:先求出让损失值最小的排列,由已生成的这些排列,通过更改某一个位置的值,生成下一个最优解,迭代最多生成m+1个最优解即可,遍历求 ...

  5. Google Kickstart在线测试规则以及注意事项

    谷歌招聘在如火如荼的进行中,进谷歌都需要经过谷歌kickstart在线测试,然后过了之后还有五轮的面试- -.好吧毕竟你待遇高,你强你有理.. 下面介绍一下进谷歌的第一关google kickstar ...

  6. Google kickstart 2022 Round A题解

    Speed Typing 题意概述 给出两个字符串I和P,问能否通过删除P中若干个字符得到I?如果能的话,需要删除字符的个数是多少? 数据规模 \[1≤|I|,|P|≤10^5 \] 双指针 设置两个 ...

  7. google kickstart 2018 round D A Candies

    思路: 对于small数据,由于求和及奇数数量两个限制条件均满足区间单调性,可以直接使用尺取法(滑动窗口法)求解. 对于large数据,奇数数量依然是满足区间单调性的.首先使用尺取法,找到所有满足奇数 ...

  8. [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 ...

  9. Let Me Count The Ways(Kickstart Round H 2018)

    题目链接:https://code.google.com/codejam/contest/3324486/dashboard#s=p2 题目: 思路: 代码实现如下: #include <set ...

随机推荐

  1. 使用环信开发项目遇到错误提示 configure your build for VectorDrawableCompat

    问题描述:在使用AndroidStudio开发项目时,使用环信重写了聊天界面后,运行时app就崩掉了,查看日志报告,提示报错如下: java.lang.RuntimeException: Unable ...

  2. ELK安装过程

    官方安装文档:https://www.elastic.co/guide/en/elasticsearch/reference/current/zip-targz.html 官方硬件和配置项推荐:htt ...

  3. Alpha冲刺&总结报告(12/12)(麻瓜制造者)

    各个成员今日完成的任务 邓弘立: 完成了上传头像的功能 符天愉: 对所有接口进行了再次测试 江郑: 完成了发布需求接口部分的进一步测试和接口文档的编写 刘双玉: 完成了商品信息接口部分的进一步测试和接 ...

  4. Python3编写网络爬虫01-基本请求库urllib的使用

    安装python后 自带urllib库 模块篇 分为几个模块如下: 1. urllib.request 请求模块 2. urllib.parse 分析模块 3. urllib.error 异常处理模块 ...

  5. 如何弹出QQ临时对话框实现不添加好友在线交谈效果

    如何不添加好友弹出QQ临时对话框实现在线交谈效果,这样的一个需求,我们真的是太需要了,实现起来也很简单,一行代码即可搞定,需要的朋友可以参考下 其实这个很简单,在img我们加入一个a标签,然后 < ...

  6. swift static与class修饰符:static不参与动态派发

    static与class 都有类型成员的含义:相对于实例成员: static的另一个意思是静态派发:所以不能被继承. 要使用动态派发和继承的机制必须使用class继承. static的其它常见含义: ...

  7. 加强对HEAD 请求的处理(转贴)

    最近发现有些搜索引擎爬虫在抓取数据的时候,先是通过一个HEAD 请求获取response的header 信息,然后再通过GET 请求获取response 的body信息(即页面的内容)——先发送HEA ...

  8. MP实战系列(十一)之封装方法详解(续一)

    之前写的封装方法详解,比较简要. 今天我主要讲增加和删除及其修改.查的话得单独再详讲. 增删改查,无论是Java或者C#等等,凡是对数据库操作的都离不开这四个. 一.增加方法讲解 MyBatis Pl ...

  9. C++ vector 容器

    //vector类 resemble array 自动扩容... 暂存于内存中 //格式 vector<类(型)名> 对象名 example: vector<string> v ...

  10. Java 包(package)

    为了更好地组织类,Java 提供了包机制,用于区别类名的命名空间. 1.包的作用 1.把功能相似或相关的类或接口组织在同一个包中,方便类的查找和使用. 2.如同文件夹一样,包也采用了树形目录的存储方式 ...