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. HDU 3790 最短生成树 (最短路)

    题目链接 Problem Description 给你n个点,m条无向边,每条边都有长度d和花费p,给你起点s终点t,要求输出起点到终点的最短距离及其花费,如果最短距离有多条路线,则输出花费最少的. ...

  2. C - K-inversions URAL - 1523 (dp + 线段树)

    题目链接:https://cn.vjudge.net/contest/275079#problem/C 具体思路:我们可以分层的去建立,假设我们要找k层,我们可以先把满足1.2....k-1层的满足情 ...

  3. (转)USB的描述符及各种描述符之间的依赖关系

    全文链接:http://justmei.blog.163.com/blog/static/11609985320102421659260/?latestBlog 1 推荐 [原创] USB入门系列之七 ...

  4. Linux轻量级自动运维工具-Ansible浅析【转】

    转自 Linux轻量级自动运维工具-Ansible浅析 - ~微风~ - 51CTO技术博客http://weiweidefeng.blog.51cto.com/1957995/1895261 Ans ...

  5. 关于text-decoration无法清除继承的问题

    因为text-decoration的值可以叠加,所以即使设置了none,浏览器也是看成是叠加,而不是清除的意思.

  6. PhantomJS、Selenium、Chrome驱动的mac版安装和配置

    PhantomJS(爬取动态页面需要用到) 基于webkit的javaScript API.提供了css选择器,提供了处理文件的I/O操作,支持Web标准.DOM操作.JSON.HTML5.Canva ...

  7. Mathtype公式位置偏上

    Mathtype公式位置偏上 部分Mathtype公式与文档文字没有很好的对齐,而是浮起来了,也就是说Mathtype公式的位置比正常文字稍高,这是我写论文时碰到的一个很麻烦的问题.然后就是行距稍微大 ...

  8. 阻止MyEclipse启动项目时自动跳转的debug视图

    启动web项目时,习惯使用debug方式启动,但此时会自动跳转到debug视图,很不习惯. 通过修改首选项配置,可以禁止跳转

  9. fedroa20 没法开启ntpd服务器

    1现象:ntpd老是没法开启,ntpd -d显示有个进程占用123端口. [root@vd13crmtb01 ~]# systemctl enable ntpd.service         //开 ...

  10. TCP三次链接和四次断开

    经典的三次握手示意图:(#add,“握手”即图中左边到右边的连线)    经典的四次握手关闭图:    TCP是主机对主机层的传输控制协议,提供可靠的连接服务,采用三次握手确认建立一个连接: 位码即t ...