C3 - Encryption (hard)

思路:

记sum[i]表示0 - i 的和对 p 取模的值。

1.如果k * p > n,那么与C2的做法一致,O(k*p*n)复杂度低于1e8。

2.如果k * p <= n

那么根据抽屉原理,必有k个sum[i]相同,

那么任意取k - 1个相同的 sum[i],记它们的下标为 l1,l2,......,lk-1 ,那么显然区间[li + 1, li+1](1<=i<k-1)的贡献为0

有贡献的区间只有[1,l1]和[lk-1 + 1,n]由于两个区间的贡献加起来小于2 * (p - 1) ,所以最后的答案要么为 sum[n],要么为 sum[n] + p

那么怎么判断是前者还是后者呢?

只要判断在sum中能不能找到一个以sum[n]结尾的长度为k的非严格上升子序列就可以了

如果能找到就是sum[n],否则就是 sum[n] + p

LIS的复杂度O(nlogn)

代码:

#include<bits/stdc++.h>
using namespace std;
#define LL long long
#define pb push_back
#define mem(a, b) memset(a, b, sizeof(a)) const int N = 5e5 + ;
const int INF = 0x3f3f3f3f;
int a[N];
int dp[][];
int _dp[N];
int main() {
int n, k, p;
scanf("%d%d%d", &n, &k, &p);
for (int i = ; i <= n; i++) scanf("%d", &a[i]),a[i] += a[i-], a[i] %= p;
if (k * p > n) {
mem(dp, INF);
dp[][] = ;
for (int i = ; i <= n; i++) {
for (int j = k; j >= ; j--) {
for (int l = ; l < p; l++){
dp[a[i]][j] = min(dp[a[i]][j], dp[l][j-] + ((a[i] - l)%p+p)%p);
}
}
}
printf("%d\n", dp[a[n]][k]);
}
else {
mem(_dp, INF);
for (int i = ; i <= n - ; i++) {
*upper_bound(_dp + , _dp + n, a[i]) = a[i];
}
if(_dp[k - ] <= a[n]) printf("%d\n", a[n]);
else printf("%d\n", a[n] + p);
}
return ;
}

Codeforces 958C3 - Encryption (hard)的更多相关文章

  1. Codeforces 958C3 - Encryption (hard) 区间dp+抽屉原理

    转自:http://www.cnblogs.com/widsom/p/8863005.html 题目大意: 比起Encryption 中级版,把n的范围扩大到 500000,k,p范围都在100以内, ...

  2. Encryption (hard) CodeForces - 958C3 (树状数组)

    大意: 给定序列$a$, 要求将$a$分成$k$个非空区间, 使得区间和模$p$的和最小, 要求输出最小值. $k$和$p$比较小, 直接暴力$dp$, 时间复杂度是$O(nklogp)$, 空间是$ ...

  3. CodeForces - 999B Reversing Encryption

    B - Reversing Encryption A string s of length n can be encrypted by the following algorithm: iterate ...

  4. Codeforces Gym 100015H Hidden Code 暴力

    Hidden Code 题目连接: http://codeforces.com/gym/100015/attachments Description It's time to put your hac ...

  5. (AB)Codeforces Round #528 (Div. 2, based on Technocup 2019 Elimination Round

    A. Right-Left Cipher time limit per test 1 second memory limit per test 256 megabytes input standard ...

  6. Codeforces Round #528-A. Right-Left Cipher(字符串模拟)

    time limit per test 1 second memory limit per test 256 megabytes input standard input output standar ...

  7. Dynamics CRM 2015-Data Encryption激活报错

    在CRM的日常开发中,Data Encryption经常是不得不开启的一个功能.但是有时,我们可能遇到一种情况,Organization导入之后,查看Data Encryption是已激活的状态,但是 ...

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

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

  9. 【Codeforces 738D】Sea Battle(贪心)

    http://codeforces.com/contest/738/problem/D Galya is playing one-dimensional Sea Battle on a 1 × n g ...

随机推荐

  1. P3690 【模板】Link Cut Tree (动态树)

    P3690 [模板]Link Cut Tree (动态树) 认父不认子的lct 注意:不 要 把 $fa[x]$和$nrt(x)$ 混 在 一 起 ! #include<cstdio> v ...

  2. mysql/oracle jdbc大数据量插入优化

    10.10.6  大数据量插入优化 在很多涉及支付和金融相关的系统中,夜间会进行批处理,在批处理的一开始或最后一般需要将数据回库,因为应用和数据库通常部署在不同的服务器,而且应用所在的服务器一般也不会 ...

  3. Bootstrap3基础 btn-group-vertical 按钮组(横着、竖着排列)

      内容 参数   OS   Windows 10 x64   browser   Firefox 65.0.2   framework     Bootstrap 3.3.7   editor    ...

  4. Docker 搭建Spark 依赖singularities/spark:2.2镜像

    singularities/spark:2.2版本中 Hadoop版本:2.8.2 Spark版本: 2.2.1 Scala版本:2.11.8 Java版本:1.8.0_151 拉取镜像: [root ...

  5. HihoCoder 1634 Puzzle Game(最大子矩阵和)题解

    题意:给一个n*m的矩阵,你只能选择一个格子把这个格子的数换成p(也可以一个都不换),问最大子矩阵和最小可能是多少? 思路: 思路就是上面这个思路,这里简单讲一下怎么n^3求最大子矩阵和:枚举两行(或 ...

  6. hihoCoder week16 RMQ-ST算法

    RMQ问题 用的st表吧,读入数据挺多的,输出数据也挺多的 我还用了 cout<<endl;  T了.. 真的是 做题不带脑子的 #include <bits/stdc++.h> ...

  7. R class of subset of matrix and data.frame

    a = matrix(     c(2, 4, 3, 1, 5, 7), # the data elements     nrow=2,              # number of rows   ...

  8. 操作系统04_IO管理

    输入输出系统 IO系统的层次结构 用户层IO软件 设备独立性软件 设备驱动程序 中断处理程序 对IO设备的控制方式 使用轮询的可编程IO方式 cpu不停地检查设备的状态,以字节为单位,非中断方式,利用 ...

  9. LOJ6283 数列分块入门7(分块)

    pushdown的addtag[x]打成addtag[i],结果WA了一次 #include <cstdio> #include <algorithm> #include &l ...

  10. (转)Awesome Object Detection

    Awesome Object Detection 2018-08-10 09:30:40 This blog is copied from: https://github.com/amusi/awes ...