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. Python概念-上下文管理协议中的__enter__和__exit__

    所谓上下文管理协议,就是咱们打开文件时常用的一种方法:with __enter__(self):当with开始运行的时候触发此方法的运行 __exit__(self, exc_type, exc_va ...

  2. openstack cloud init set password

    设置代理和password #!/bin/bash cd /home/ubuntu wget otcloud-gateway.bj.intel.com/script.tar.gz ]; then cu ...

  3. Linux基础笔记—— 走进Linux

    走进Linux 操作系统 操作系统是计算机中必不可少的基础系统软件,他的作用是管理和控制计算机系统中的硬件和软件资源,合理有效的组织系统的工作流程,在计算机系统(硬件)与使用者之间提供接口作用. 操作 ...

  4. AnswerOpenCV(0826-0901)一周佳作欣赏

    1.OpenCV to detect how missing tooth in equipment Hello everyone. I am just starting with OpenCV and ...

  5. 【python018--函数参数】

    1.形参和实参 >>> def MyFirstFunction(name):    '函数定义过程中的name是叫形参'    #因为Ta只是一个形式,表示占据一个参数位置    p ...

  6. rsync: read error: Connection reset by peer (104)

    Centos7    rsync守护进程上传文件失败 [root@nfs ~]# rsync -avz /etc rsync_backup@172.16.1.41::backupsending inc ...

  7. topcoder srm 315 div1

    problem1  link 直接模拟即可. import java.util.*; import java.math.*; import static java.lang.Math.*; publi ...

  8. topcoder srm 683 div1

    problem1 link 肯定存在相邻两堆满足不会存在任何操作在这两堆之间进行.然后就成为一条链,那么只需要维护链的前缀和即可判断当前堆和前一堆之间需要多少次操作. problem2 link 对于 ...

  9. CentOS7学习记录(工具使用篇)

    一.   远程连接终端中文乱码:如xShell 检查当前系统语言:echo $LANG 查看系统安装语言包:locale ,如果包含zh_CN.UTF-8表示已经安装中文语言.如果没有中文包,使用命令 ...

  10. SpringBoot 全局统一记录日志

    1.记录日志 使用aop来记录controller中的请求返回日志 pom.xml引入: <dependency> <groupId>org.springframework.b ...