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. 【BZOJ】2466: [中山市选2009]树 高斯消元解异或方程组

    [题意]给定一棵树的灯,按一次x改变与x距离<=1的点的状态,求全0到全1的最少次数.n<=100. [算法]高斯消元解异或方程组 [题解]设f[i]=0/1表示是否按第i个点的按钮,根据 ...

  2. 【CodeForces】698 C. LRU

    [题目]C. LRU [题意]给定空间为k的背包和n个物品,每次每个物品有pi的概率加入(Σpi=1),加入时若发现背包中已有该物品则不改变,若背包满k个物品后再加入新物品则弹出最早加入的物品,求加入 ...

  3. 使用generatorConfig工具自动生成mybatis的实体类以及dao接口和映射文件

    1:数据准备 创建一个数据库表 CREATE TABLE `logininfo` ( `id` ) NOT NULL AUTO_INCREMENT, `username` ) DEFAULT NULL ...

  4. Master of Phi (欧拉函数 + 积性函数的性质 + 狄利克雷卷积)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6265 题目大意:首先T是测试组数,n代表当前这个数的因子的种类,然后接下来的p和q,代表当前这个数的因 ...

  5. ajax技术整理总结(1)

    1.创建ajax对象 var xhr=new XMLHttpRequest(); 4.监听状态信息 xhr.onreadystatechange=function(){ //4接收完毕 ){ docu ...

  6. JS 判断手机操作系统代码

    还是利用UA, 返回值: ios, android, unknown function getMobileType () { var ua = window.navigator.userAgent.t ...

  7. Ubuntu 10.04 分辨率调整

    最近学长们看了我的本本都在问我,显卡驱动是不是出现什么问题了···分辨率这么差.当时我的分辨率是1024X768,于是我就想修改我的屏幕分辨率改成1280X800.本来很简单的事情,我做起来却非常的曲 ...

  8. PHP用imageTtfText函数在图片上写入汉字

    https://blog.csdn.net/smstong/article/details/43955705 PHP绘图,imageString()这个函数并不支持汉字的绘制.这往往会给入门者当头一棒 ...

  9. TypeError: not all arguments converted during string formatting

    print ("So, you're 5r old, %r tall and %r heavy." % (age, height, weight)) print ("So ...

  10. Python抓取微博评论(二)

    对于新浪微博评论的抓取,首篇做的时候有些考虑不周,然后现在改正了一些地方,因为有人问,抓取评论的时候“爬前50页的热评,或者最新评论里的前100页“,这样的数据看了看,好像每条微博的评论都只能抓取到前 ...