Codeforces 1129 D. Isolation
Codeforces 1129 D. Isolation
解题思路:
令 \(f(l,r)\) 为 \([l,r]\) 中之出现一次的元素个数,然后可以得到暴力 \(\text{dp}\) 的式子。
\]
实际上任意一个位置为左端点,\(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的更多相关文章
- Codeforces 1129 E.Legendary Tree
Codeforces 1129 E.Legendary Tree 解题思路: 这题好厉害,我来复读一下官方题解,顺便补充几句. 首先,可以通过询问 \(n-1\) 次 \((S=\{1\},T=\{ ...
- 【Codeforces 1129C】Morse Code
Codeforces 1129 C 题意:给一个0/1串,问它的每一个前缀中的每一个子串能解析成莫尔斯电码的串的种数. 思路:首先对于这个串构造后缀自动机,那么从起点走到每一个节点的每一条路径都代表了 ...
- 【Codeforces Round 1129】Alex Lopashev Thanks-Round (Div. 1)
Codeforces Round 1129 这场模拟比赛做了\(A1\).\(A2\).\(B\).\(C\),\(Div.1\)排名40. \(A\)题是道贪心,可以考虑每一个站点是分开来的,把目的 ...
- Codeforces 1129D - Isolation(分块优化 dp)
Codeforces 题目传送门 & 洛谷题目传送门 又独立切了道 *2900( 首先考虑 \(dp\),\(dp_i\) 表示以 \(i\) 为结尾的划分的方式,那么显然有转移 \(dp_i ...
- Codeforces.1029D.Isolation(DP 分块)
题目链接 \(Description\) 给定长为\(n\)的序列\(A_i\)和一个整数\(K\).把它划分成若干段,满足每段中恰好出现过一次的数的个数\(\leq K\).求方案数. \(K\le ...
- 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 ...
- Codeforces Round #539Ȟȟȡ (Div. 1) 简要题解
Codeforces Round #539 (Div. 1) A. Sasha and a Bit of Relax description 给一个序列\(a_i\),求有多少长度为偶数的区间\([l ...
- 数据库的快照隔离级别(Snapshot Isolation)
隔离级别定义事务处理数据读取操作的隔离程度,在SQL Server中,隔离级别只会影响读操作申请的共享锁(Shared Lock),而不会影响写操作申请的互斥锁(Exclusive Lock),隔离级 ...
- python爬虫学习(5) —— 扒一下codeforces题面
上一次我们拿学校的URP做了个小小的demo.... 其实我们还可以把每个学生的证件照爬下来做成一个证件照校花校草评比 另外也可以写一个物理实验自动选课... 但是出于多种原因,,还是绕开这些敏感话题 ...
随机推荐
- 【Codeforces811E】Vladik and Entertaining Flags [线段树][并查集]
Vladik and Entertaining Flags Time Limit: 20 Sec Memory Limit: 512 MB Description n * m的矩形,每个格子上有一个 ...
- 引用类型 ( 对象定义 )——Object 类型
本文地址:http://www.cnblogs.com/veinyin/p/7607100.html 创建实例 new 操作符后跟构造函数 var people = new Object(); pe ...
- Django中的MiddleWare中间件
1. middleware简介 Django的middleware的概念相当于SSH框架里面的filter的概念.中间键的作用就是对所有的request,在request前,和在response后做一 ...
- 利用Addon Domain和A记录使两个域名同时指向同一个网站
今天碰到这样的需求:已有网站A.com, 以及新注册的域名B.net, 现需要将B.net指向与A.com相同的内容. 这里提出的方法是在空间后台添加Addon domain, 以及在域名B.net后 ...
- python进阶之内置函数和语法糖触发魔法方法
前言 前面已经总结了关键字.运算符与魔法方法的对应关系,下面总结python内置函数对应的魔法方法. 魔法方法 数学计算 abs(args):返回绝对值,调用__abs__; round(args): ...
- Myeclipse编辑jsp文件很卡是什么原因?
可能是配置问题,配置的时候不要把myeclipse连接到网络.否则每次编辑的时候要在网上查找,所以照成很卡.window->perferences->java->Installed ...
- CSS背景横向平铺BUG,解决方法
给定DIV一个背景图片横向平铺,缩小浏览器,拉动横向滚动条,此时触发此BUG:背景图片平铺不完整 解决办法: 1.把背景图片写在BODY上,此办法局限于没有使用iframe的情况下,所以少用 2.设定 ...
- (My)SQL
1.SQL语句分类 DDL(Data Definition Languages)语句:用来创建 删除 修改数据库.表.列.索引等数据库对象.常用的语句关键字主要包括create.drop.alter等 ...
- springcloud 显示服务详细健康信息
pom.xml <?xml version="1.0" encoding="UTF-8"?> <project xmlns="htt ...
- js获取json对象中的key和value,并组成新数组
//比如有一个json var json = {"name" : "Tom", "age" : 18}; //想分别获取它的key 和 va ...