大意: 给定序列$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;
#endif int 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]);
}

最新文章

  1. js DOM Document类型
  2. SQL Server 2008 下载及安装教程
  3. 团队作业week2
  4. 使用unetbootin制作Debian安装U盘
  5. 多语言本地化开发Localized
  6. 使用virtualenvwrapper隔离python环境
  7. cocos2d-x CCTableView
  8. phpcms 源码分析五:文件缓存实现
  9. Java 多并发之原子访问(Atomic Access)
  10. python书籍推荐
  11. DateTime.Now的一些用法
  12. pentaho之kettle篇---kettle基本操作
  13. Java进阶(三十七)java 自动装箱与拆箱
  14. Linux c 获取cpu使用率(2)
  15. Docker学习笔记之了解 Docker 的核心组成
  16. Lua和C++交互 学习记录之六:全局函数交互
  17. Nginx 用log_format设置日志格式
  18. SparkSQL UDF使用方法与原理详解
  19. HDUOJ-----1066Last non-zero Digit in N!
  20. Git的4个阶段的撤销更改

热门文章

  1. finally的一个妙用
  2. 学习UEFI 之你把C语言学好了码?学习UEFI 之你把C语言学好了吗?
  3. Location of Docker images in all Operating Systems (Linux, Windows, Redhat, Mac OS X)
  4. tensorflow源码分析——BasicLSTMCell
  5. Php+Redis函数使用总结
  6. Anaconda 改为国内镜像的方法
  7. LC 499. The Maze III 【lock,hard】
  8. AndroidManifest.xml中的&lt;uses-feature&gt;以及和&lt;uses-permission&gt;之间的联系
  9. shell 部分语法
  10. notepad++ 插件说明(一)