Codeforces 868F - Yet Another Minimization Problem (分治DP,莫队)
2017-11-28 00:41
471 查看
题意:给定一个序列要求分成k个连续子序列,使得子序列的cost之和最小,子序列的cost定义为子序列中数对的两个数相同的数对个数, 如cost(1,1) = 1, oost(1,1,1,3) = cost(1,1,1) = 3*2/2 = 1+2 = 3;
题解:对于朴素的dp[i][j]表示1~j分为i个块的最小cost,有O(k*n^2)的算法dp[i][j] = min{dp[i-1][k]+cost(k+1,j), k < j}, 设给定j最优的k为P(j),不难发现P(j)单调,于是分治,solve(l,r,L,R)表示已知对于 l <= j <= r, 有 L <= p(j) <= R. 令m = (l+r)/2, 每次通过枚举计算出P(m)以及dp[i][m], 再分治计算solve(l,m-1,L,p(m))和solve(m+1,r,p(m),R).
再考虑cost(i,j), 不难发现cost(i,j)可以O(1)转移到相邻区间, 且在分治过程中每次solve区间移动O(n)次,所以莫队之即可,总复杂度O(knlogn)
题解:对于朴素的dp[i][j]表示1~j分为i个块的最小cost,有O(k*n^2)的算法dp[i][j] = min{dp[i-1][k]+cost(k+1,j), k < j}, 设给定j最优的k为P(j),不难发现P(j)单调,于是分治,solve(l,r,L,R)表示已知对于 l <= j <= r, 有 L <= p(j) <= R. 令m = (l+r)/2, 每次通过枚举计算出P(m)以及dp[i][m], 再分治计算solve(l,m-1,L,p(m))和solve(m+1,r,p(m),R).
再考虑cost(i,j), 不难发现cost(i,j)可以O(1)转移到相邻区间, 且在分治过程中每次solve区间移动O(n)次,所以莫队之即可,总复杂度O(knlogn)
#include <iostream> #include <cstdio> #include <cctype> #include <algorithm> #include <cstring> #include <string> #include <cmath> #include <vector> #include <set> #include <stack> #include <sstream> #include <queue> #include <map> #include <functional> #include <bitset> using namespace std; #define pb push_back #define mk make_pair #define ll long long #define ull unsigned long long #define pii pair<int, int> #define mk make_pair #define fi first #define se second #define ALL(A) A.begin(), A.end() #define rep(i,n) for(int (i)=0;(i)<(int)(n);(i)++) #define repr(i, n) for(int (i)=(int)(n);(i)>=0;(i)--) #define repab(i,a,b) for(int (i)=(int)(a);(i)<=(int)(b);(i)++) #define reprab(i,a,b) for(int (i)=(int)(a);(i)>=(int)(b);(i)--) #define m ((l+r)/2) #define sc(x) scanf("%d", &x) #define pr(x) printf("x:%d\n", x) #define fastio ios::sync_with_stdio(0), cin.tie(0) #define frein freopen("in.txt", "r", stdin) #define freout freopen("out.txt", "w", stdout) #define freout1 freopen("out1.txt", "w", stdout) #define lb puts("") #define PI M_PI #define debug cout<<"???"<<endl #define mid ((l+r)>>1) const ll mod = 1000000007; //const int INF = 0x3f3f3f3f; const ll INF = 0x3f3f3f3f3f3f3f3f; const double eps = 1e-6; template<class T> T gcd(T a, T b){if(!b)return a;return gcd(b,a%b);} const int maxn = 1e5+10; int n,k,a[maxn],cnt[maxn],le = 1, ri = 0, now, nxt; ll sum, dp[2][maxn]; void query(int l, int r) { while(ri < r){ sum += 1LL*cnt[a[++ri]]; cnt[a[ri]]++; //printf("ri:%d, sum:%I64d\n", ri, sum); } while(ri > r){ cnt[a[ri]]--; sum -= 1LL*cnt[a[ri--]]; } while(le > l){ sum += 1LL*cnt[a[--le]]; cnt[a[le]]++; } while(le < l){ cnt[a[le]]--; sum -= 1LL*cnt[a[le++]]; } //printf("query(%d,%d):%I64d\n", l, r, sum); } void solve(int l, int r, int L, int R) { if(l > r) return; dp[nxt][m] = INF; int M, ed = min(R,m); for(int i = L; i <= ed; i++){ query(i, m); if(dp[now][i-1] + sum < dp[nxt][m]){ dp[nxt][m] = dp[now][i-1] + sum; M = i; } } //printf("solve(%d,%d,%d,%d), dp[%d]:%I64d\n", l,r,L,R,m,dp[nxt][m]); solve(l,m-1,L,M); solve(m+1,r,M,R); } int main() { //frein; while(cin >> n >> k){ for(int i = 1; i <= n; i++) sc(a[i]); memset(cnt, 0, sizeof(cnt)); now = 0; nxt = 1; le = 1; ri = 0; sum = 0; memset(dp, INF, sizeof(dp)); dp[now][0] = 0; for(int i = 0; i < k; i++){ solve(1,n,1,n); swap(now,nxt); } printf("%I64d\n", dp[now] ); } return 0; }
相关文章推荐
- Codeforces Round #438:F. Yet Another Minimization Problem(DP决策单调性+二分+莫队)
- CF 868 F. Yet Another Minimization Problem
- CodeForces - 1000D:Yet Another Problem On a Subsequence (DP+组合数)
- [Codeforces 863D]Yet Another Array Queries Problem
- Codeforces 868F 分治优化Dp
- SRM 591 D2L3:YetAnotherTwoTeamsProblem,dp
- CF:Problem 383D - Antimatter 分治DP
- Yet Another Multiple Problem (hdu 4474)
- uva 10516 - Another Counting Problem(dp)
- 状态压缩DP codeforces 244 Problem C. The Brand New Function 和 codeforces 165 E. Compatible Numbers
- ZOJ 4008 Yet Another Tree Query Problem [想法+树状数组]
- ZOJ 4008 Yet Another Tree Query Problem 树状数组 离线处理
- HDOJ 4474 Yet Another Multiple Problem(BFS)
- HDU 4474 Yet Another Multiple Problem
- CF:Problem 383D - Antimatter 分治DP
- hdu 4474 Yet Another Multiple Problem(按位枚举,4级)
- HDU 4474 Yet Another Multiple Problem【BFS+一个判断技巧】
- Codeforces 340C Tourist Problem 数学+DP
- HDU 4474 Yet Another Multiple Problem (bfs+mod运算)
- HDU 4474 Yet Another Multiple Problem【2012成都regional K题】 【BFS+一个判断技巧】