首页 技术 正文
技术 2022年11月20日
0 收藏 831 点赞 2,138 浏览 1810 个字

大意: 给定序列$a$, 要求将$a$分成$k$个非空区间, 使得区间和模$p$的和最小, 要求输出最小值.

$k$和$p$比较小, 直接暴力$dp$, 时间复杂度是$O(nklogp)$, 空间是$O(nk+kp)$

$dp[i][j]=min(…,f[j-1][s[i]-1]+1,f[j][s[i]],f[j][s[i]+1]-1+p,…)$

看了其他提交, 好像有$O(nk)$的做法.

#include <iostream>
#include <sstream>
#include <algorithm>
#include <cstdio>
#include <math.h>
#include <set>
#include <map>
#include <queue>
#include <string>
#include <string.h>
#include <bitset>
#define REP(i,a,n) for(int i=a;i<=n;++i)
#define PER(i,a,n) for(int i=n;i>=a;--i)
#define hr putchar(10)
#define pb push_back
#define lc (o<<1)
#define rc (lc|1)
#define mid ((l+r)>>1)
#define ls lc,l,mid
#define rs rc,mid+1,r
#define x first
#define y second
#define io std::ios::sync_with_stdio(false)
#define endl '\n'
#define DB(a) ({REP(__i,1,n) cout<<a[__i]<<' ';hr;})
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
const int P = 1e9+7, INF = 0x3f3f3f3f;
ll gcd(ll a,ll b) {return b?gcd(b,a%b):a;}
ll qpow(ll a,ll n) {ll r=1%P;for (a%=P;n;a=a*a%P,n>>=1)if(n&1)r=r*a%P;return r;}
ll inv(ll x){return x<=1?1:inv(P%x)*(P-P/x)%P;}
inline int rd() {int x=0;char p=getchar();while(p<'0'||p>'9')p=getchar();while(p>='0'&&p<='9')x=x*10+p-'0',p=getchar();return x;}
//head#ifdef ONLINE_JUDGE
const int N = 5e5+10;
#else
const int N = 111;
#endifint n, k, p, a[N], s[N];
int dp[N][102];
struct BIT {
int c[102];
BIT () {memset(c,0x3f,sizeof c);}
void add1(int x, int v) {
for (++x; x<=p; x+=x&-x) c[x]=min(c[x],v);
}
void add2(int x, int v) {
for (++x; x; x^=x&-x) c[x]=min(c[x],v);
}
int qry1(int x) {
int r=INF;
for (++x; x; x^=x&-x) r=min(r,c[x]);
return r;
}
int qry2(int x) {
int r=INF;
for (++x; x<=p; x+=x&-x) r=min(r,c[x]);
return r;
}
} f1[102], f2[102];int main() {
scanf("%d%d%d", &n, &k, &p);
REP(i,1,n) {
scanf("%d", a+i);
s[i]=(s[i-1]+a[i])%p;
}
f1[0].add1(0,0);
f2[0].add2(0,0);
REP(i,1,n) {
REP(j,1,min(i,k)) {
dp[i][j] = min(f1[j-1].qry1(s[i])+s[i],f2[j-1].qry2(s[i])+s[i]+p);
}
REP(j,1,min(i,k)) if (dp[i][j]<=INF) {
f1[j].add1(s[i],dp[i][j]-s[i]);
f2[j].add2(s[i],dp[i][j]-s[i]);
}
}
printf("%d\n", dp[n][k]);
}
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:8,965
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,486
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,331
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,114
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,747
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,781