Codeforces 1129 D. Isolation

解题思路

令 \(f(l,r)\) 为 \([l,r]\) 中之出现一次的元素个数,然后可以得到暴力 \(\text{dp}\) 的式子。

\[dp[i]=\sum_{j=i-1}[f(j+1,i)\leq k]dp[j]
\]

实际上任意一个位置为左端点,\(i\) 为右端点的 \(f(l,r)\) 值是可以动态维护的。

\((i-1)\rightarrow i\) ,设 \(pre[i]\) 为 \(i\) 之前上一个出现 \(a[i]\) 的位置,那么相当与 \(f(pre[i]+1,i)\dots f(i,i)\) 的值会 \(+1\),\(f(pre[pre[i]]+1,i)\dots f(pre[i],i)\) 的值会 \(-1\) ,分个块维护一下所有当前 \(f\) 值小于等于 \(k\) 的 \(dp\) 值之和即可,复杂度 \(\mathcal O(n\sqrt n)\) 。

code

/*program by mangoyang*/
#include <bits/stdc++.h>
#define inf (0x7f7f7f7f)
#define Max(a, b) ((a) > (b) ? (a) : (b))
#define Min(a, b) ((a) < (b) ? (a) : (b))
typedef long long ll;
using namespace std;
template <class T>
inline void read(T &x){
int ch = 0, f = 0; x = 0;
for(; !isdigit(ch); ch = getchar()) if(ch == '-') f = 1;
for(; isdigit(ch); ch = getchar()) x = x * 10 + ch - 48;
if(f) x = -x;
}
const int M = 205, N = 100005, L = 100000, mod = 998244353;
int a[N], bel[N], tag[N], tot[N], ans[M], s[M][N<<1], pre[N], lst[N], dp[N], n, k, T;
inline void up(int &x, int y){
x = x + y >= mod ? x + y - mod : x + y;
}
inline void gao(int i, int x){
up(s[bel[i]][L+tot[i]], mod - dp[i]);
tot[i] += x;
up(s[bel[i]][L+tot[i]], dp[i]);
if(x == 1 && tot[i] + tag[bel[i]] == k + 1)
up(ans[bel[i]], mod - dp[i]);
if(x == -1 && tot[i] + tag[bel[i]] == k)
up(ans[bel[i]], dp[i]);
}
inline void modify(int l, int r, int x){
if(bel[l] + 1 >= bel[r]){
for(int i = l; i <= r; i++) gao(i, x);
return;
}
for(int i = l; i < (bel[l] + 1) * T; i++) gao(i, x);
for(int i = bel[r] * T; i <= r; i++) gao(i, x);
for(int i = bel[l] + 1; i < bel[r]; i++){
if(x == 1) up(ans[i], mod - s[i][L+k-tag[i]]);
if(x == -1) up(ans[i], s[i][L+k-tag[i]+1]);
tag[i] += x;
}
}
inline int query(int l, int r){
int res = 0;
if(bel[l] + 1 >= bel[r]){
for(int i = l; i <= r; i++)
if(tot[i] + tag[bel[i]] <= k) up(res, dp[i]);
return res;
}
for(int i = l; i < (bel[l] + 1) * T; i++)
if(tot[i] + tag[bel[i]] <= k) up(res, dp[i]);
for(int i = bel[r] * T; i <= r; i++)
if(tot[i] + tag[bel[i]] <= k) up(res, dp[i]);
for(int i = bel[l] + 1; i < bel[r]; i++) up(res, ans[i]);
return res;
}
int main(){
read(n), read(k), T = 500;
for(int i = 0; i <= n; i++) bel[i] = i / T;
for(int i = 1; i <= n; i++) read(a[i]);
dp[0] = 1;
up(ans[bel[0]], dp[0]);
up(s[bel[0]][L-tag[bel[0]]], dp[0]);
for(int i = 1; i <= n; i++){
pre[i] = lst[a[i]], lst[a[i]] = i;
modify(pre[i], i - 1, 1);
if(pre[i]) modify(pre[pre[i]], pre[i] - 1, -1);
dp[i] = query(0, i - 1);
up(ans[bel[i]], dp[i]);
up(s[bel[i]][L-tag[bel[i]]], dp[i]);
}
cout << dp[n];
return 0;
}

Codeforces 1129 D. Isolation的更多相关文章

  1. Codeforces 1129 E.Legendary Tree

    Codeforces 1129 E.Legendary Tree 解题思路: 这题好厉害,我来复读一下官方题解,顺便补充几句. 首先,可以通过询问 \(n-1​\) 次 \((S=\{1\},T=\{ ...

  2. 【Codeforces 1129C】Morse Code

    Codeforces 1129 C 题意:给一个0/1串,问它的每一个前缀中的每一个子串能解析成莫尔斯电码的串的种数. 思路:首先对于这个串构造后缀自动机,那么从起点走到每一个节点的每一条路径都代表了 ...

  3. 【Codeforces Round 1129】Alex Lopashev Thanks-Round (Div. 1)

    Codeforces Round 1129 这场模拟比赛做了\(A1\).\(A2\).\(B\).\(C\),\(Div.1\)排名40. \(A\)题是道贪心,可以考虑每一个站点是分开来的,把目的 ...

  4. Codeforces 1129D - Isolation(分块优化 dp)

    Codeforces 题目传送门 & 洛谷题目传送门 又独立切了道 *2900( 首先考虑 \(dp\),\(dp_i\) 表示以 \(i\) 为结尾的划分的方式,那么显然有转移 \(dp_i ...

  5. Codeforces.1029D.Isolation(DP 分块)

    题目链接 \(Description\) 给定长为\(n\)的序列\(A_i\)和一个整数\(K\).把它划分成若干段,满足每段中恰好出现过一次的数的个数\(\leq K\).求方案数. \(K\le ...

  6. Codeforces Round #542 [Alex Lopashev Thanks-Round] (Div. 1) C(二分+KMP)

    http://codeforces.com/contest/1129/problem/C #include<bits/stdc++.h> #define fi first #define ...

  7. Codeforces Round #539&#542&#543&#545 (Div. 1) 简要题解

    Codeforces Round #539 (Div. 1) A. Sasha and a Bit of Relax description 给一个序列\(a_i\),求有多少长度为偶数的区间\([l ...

  8. 数据库的快照隔离级别(Snapshot Isolation)

    隔离级别定义事务处理数据读取操作的隔离程度,在SQL Server中,隔离级别只会影响读操作申请的共享锁(Shared Lock),而不会影响写操作申请的互斥锁(Exclusive Lock),隔离级 ...

  9. python爬虫学习(5) —— 扒一下codeforces题面

    上一次我们拿学校的URP做了个小小的demo.... 其实我们还可以把每个学生的证件照爬下来做成一个证件照校花校草评比 另外也可以写一个物理实验自动选课... 但是出于多种原因,,还是绕开这些敏感话题 ...

随机推荐

  1. 【SRM20】数学场

    第一题 n个m位二进制,求异或值域总和. [题解]异或值域--->使用线性基,解决去重问题. m位二进制--->拆位,每位根据01数量可以用组合数快速统计总和. #include<c ...

  2. 【CodeForces】671 D. Roads in Yusland

    [题目]D. Roads in Yusland [题意]给定n个点的树,m条从下往上的链,每条链代价ci,求最少代价使得链覆盖所有边.n,m<=3*10^5,ci<=10^9,time=4 ...

  3. Laravel 5.4 migrate时报错: Specified key was too long error

    Laravel 5.4默认使用utf8mb4字符编码,而不是之前的utf8编码.因此运行php artisan migrate 会出现如下错误: [Illuminate\Database\QueryE ...

  4. jQuery.Event的一些用法

    直接写用法 //创建一个事件 var event = $.Event("事件类型",["定义的事件参数最终将出现在e1中"]); //绑定一个处理器 $(obj ...

  5. python并发编程之Queue线程、进程、协程通信(五)

    单线程.多线程之间.进程之间.协程之间很多时候需要协同完成工作,这个时候它们需要进行通讯.或者说为了解耦,普遍采用Queue,生产消费模式. 系列文章 python并发编程之threading线程(一 ...

  6. 关于SQLite3 编译及交叉编译的一些问题

    from : http://blog.sina.com.cn/s/blog_5f2e119b0101ibwn.html SQLite3 (http://www.sqlite.org)是一个非常强大的小 ...

  7. 在64位ubuntu中安装代码比较工具beyond compare

    1,//从http://www.scootersoftware.com/download.php 官方地址下载 bcompare-3.3.2.14050.tar.gz 或 bcompare-4.0.7 ...

  8. Linux中等待队列的实现

    1.       等待队列数据结构 等待队列由双向链表实现,其元素包括指向进程描述符的指针.每个等待队列都有一个等待队列头(wait queue head),等待队列头是一个类型为wait_quequ ...

  9. Entity Framework 5.0 Code First全面学习 (转)

    原文地址:感谢原文作者 http://blog.csdn.net/gentle_wolf/article/details/14004345 不贴图片了,太累. Code First 约定 借助 Cod ...

  10. 136.Single Number---异或、位运算

    题目链接 题目大意:给出一串数组,里面的数都是两个,只有一个数是一个,把这个只有一个的数找出来.时间复杂度最好是线性的,空间复杂度最好为O(1). 法一:利用map,空间换时间,代码如下(耗时26ms ...