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.... 其实我们还可以把每个学生的证件照爬下来做成一个证件照校花校草评比 另外也可以写一个物理实验自动选课... 但是出于多种原因,,还是绕开这些敏感话题 ...
随机推荐
- cms替换主页
cms替换主页的步骤 1.先做好静态页面: 2.在D:\wamp\www\phpcms\install_package\phpcms\templates文件夹下建新的文件夹tianqiwangluo( ...
- 探讨一个“无法创建JVM”的问题(已解决)
ava版本:1.4 运行设置: -Xms1G -Xmx4G 报错: [ Incompatible initial and maximum heap sizes specified: ][ initia ...
- HDU 1711 Number Sequence (字符串处理 KMP)
题目链接 Problem Description Given two sequences of numbers : a[1], a[2], ...... , a[N], and b[1], b[2], ...
- Tornado/Python 学习笔记(一)
1.源代码下载及安装:http://www.tornadoweb.org/en/stable/ 2.python中xmlrpc库官方文档:https://docs.python.org/3/libra ...
- NASA: SpaceX的猎鹰9号火箭将龙飞船发射到国际空间站
At 5:42 a.m. EDT Friday, June 29, 2018, SpaceX’s Dragon spacecraft lifts off on a Falcon 9 rocket fr ...
- Linux中fork()函数的底层实现【转】
转自:http://blog.csdn.net/duoru_xiong/article/details/76358812 1. fork(),vfork(),clone()的区别 这三个系统调用的底层 ...
- linux系统查找具体进程
ps -ef | grep '查找内容' eg:ps -ef | grep '测试USB设备穿透'
- MySQL 作业题及答案
MySQL 测试题 一. 表关系: 请创建如下表,并创建相关约束 创建表sql如下: /* Navicat MySQL Data Transfer Source Server : 192.168.11 ...
- Percona XtraDB Cluster(PXC) -集群环境安装
Percona XtraDB Cluster(PXC) ---服务安装篇 1.测试环境搭建: Ip 角色 OS PXC-version 172.16.40.201 Node1 Redhat/C ...
- 2.4G无线模块NRF2401
RF24L01+,是工作在2.4~2.5GHz 频段的,具备自动重发功能,6 个数据传输通道,最大无线传输速率为2Mbits.MCU 可与该芯片通过SPI 接口访问芯片的寄存器进行配置,达到控制模块. ...