首页 > 其他分享 >CodeForces 1527E Partition Game

CodeForces 1527E Partition Game

时间:2022-10-04 21:45:56浏览次数:58  
标签:typedef lst ll Partition CodeForces long Game cost maxn

洛谷传送门

CF 传送门

考虑朴素 dp:设 \(f_{i,j}\) 表示分了 \(j\) 段且第 \(j\) 段的末尾是 \(i\) 的最小花费。

有转移:\(f_{i,j} \gets \min\limits_{k=0}^{i-1} f_{k,j-1} + cost(k+1,i)\),其中 \(cost(l,r)\) 表示将 \([l,r]\) 分一段的花费。分层计算,每次从 \(j - 1\) 推到 \(j\)。

考虑线段树上维护 \(f_{k,j-1}+cost(k+1,i)\)。当 \(cost(k+1,i-1) \to cost(k+1,i)\) 时,只有 \(k \in [0,lst_i-1]\) 的 \(f_{k,j-1}\) 会增加 \(i - lst_i\),其中 \(lst_i\) 表示最大的 \(j\) 满足 \(j \in [1,i-1]\) 且 \(a_j = a_i\)。前缀加、前缀查询 \(\min\) 即可。

时间复杂度 \(O(kn \log n)\)。

code
/*

p_b_p_b txdy
AThousandSuns txdy
Wu_Ren txdy
Appleblue17 txdy

*/

#include <bits/stdc++.h>
#define pb push_back
#define fst first
#define scd second
#define mems(a, x) memset((a), (x), sizeof(a))

using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ldb;
typedef pair<ll, ll> pii;

const int maxn = 35050;
const ll inf = 0x3f3f3f3f3f3f3f3fLL;

int n, m, a[maxn], pre[maxn], lst[maxn];
ll f[maxn];

namespace SGT {
	ll tree[maxn << 2], tag[maxn << 2];
	
	void pushup(int x) {
		tree[x] = min(tree[x << 1], tree[x << 1 | 1]);
	}
	
	void pushdown(int x) {
		if (!tag[x]) {
			return;
		}
		tree[x << 1] += tag[x];
		tree[x << 1 | 1] += tag[x];
		tag[x << 1] += tag[x];
		tag[x << 1 | 1] += tag[x];
		tag[x] = 0;
	}
	
	void build(int rt, int l, int r) {
		tag[rt] = 0;
		if (l == r) {
			tree[rt] = f[l];
			return;
		}
		int mid = (l + r) >> 1;
		build(rt << 1, l, mid);
		build(rt << 1 | 1, mid + 1, r);
		pushup(rt);
	}
	
	void update(int rt, int l, int r, int ql, int qr, ll x) {
		if (ql <= l && r <= qr) {
			tree[rt] += x;
			tag[rt] += x;
			return;
		}
		pushdown(rt);
		int mid = (l + r) >> 1;
		if (ql <= mid) {
			update(rt << 1, l, mid, ql, qr, x);
		}
		if (qr > mid) {
			update(rt << 1 | 1, mid + 1, r, ql, qr, x);
		}
		pushup(rt);
	}
	
	ll query(int rt, int l, int r, int ql, int qr) {
		if (ql <= l && r <= qr) {
			return tree[rt];
		}
		pushdown(rt);
		int mid = (l + r) >> 1;
		ll res = inf;
		if (ql <= mid) {
			res = min(res, query(rt << 1, l, mid, ql, qr));
		}
		if (qr > mid) {
			res = min(res, query(rt << 1 | 1, mid + 1, r, ql, qr));
		}
		return res;
	}
}

void solve() {
	scanf("%d%d", &n, &m);
	for (int i = 1; i <= n; ++i) {
		scanf("%d", &a[i]);
		lst[i] = pre[a[i]];
		pre[a[i]] = i;
	}
	mems(f, 0x3f);
	f[0] = 0;
	for (int j = 1; j <= m; ++j) {
		SGT::build(1, 0, n);
		for (int i = 1; i <= n; ++i) {
			if (lst[i]) {
				SGT::update(1, 0, n, 0, lst[i] - 1, i - lst[i]);
			}
			f[i] = SGT::query(1, 0, n, 0, i - 1);
		}
	}
	printf("%lld\n", f[n]);
}

int main() {
	int T = 1;
	// scanf("%d", &T);
	while (T--) {
		solve();
	}
	return 0;
}

标签:typedef,lst,ll,Partition,CodeForces,long,Game,cost,maxn
From: https://www.cnblogs.com/zltzlt-blog/p/16754556.html

相关文章

  • Codeforces Educational Codeforces Round 136 C D
    C一开始没有读懂题意,以为是轮流游戏,看别人翻译才发现先手在下一轮会变为反手,导致搞了半天过不了样例。可以知道如果\(n\)这张牌在Alice手中,则Alice先手打出这张牌必胜。......
  • Codeforces Round #824 (Div. 2)
    CodeforcesRound#824(Div.2)A#include<iostream>#include<algorithm>#include<cstring>#include<cmath>#defineendl'\n'usingnamespacestd;intt,n;inlin......
  • Codeforces Round #824赛时情况&赛后总结
    前两天的CF到今天才总结,还是太鸽了呢赛时首先看了题目,由于英语障碍,我还在看A题时,YSC就已经A了(我还是太逊了)。看懂后,发现A是道水题(正常),快速切掉。随后看B,阅读倒没什么障......
  • Codeforces Global Round 22
    题目链接这场彻底打崩了,只做了A,B,C,可以看出我离退役已经不远力!A.GloryAddictstrash题不写。感觉出得很没意思。B.PrefixSumAddicts用\(s_{n-k+1}\sims_n\)......
  • python partition函数_Python partition()函数的使用方法
    一、partition()函数的语法格式string_name.partition(separator)(1)string_name为要被分隔的字符串或字符串变量。(2)该函数有一个字符串类型的参数:separator,该参数用于指......
  • CodeForces 1455G Forbidden Value
    洛谷传送门CF传送门小清新动态开点线段树优化dp题。首先题目中的if嵌套看起来就很烦,可以考虑建树,外面再套一层大的if0...end,这样就将本题转化成一个树上问题。......
  • Codeforces Round #823 (Div. 2) A-D
    比赛链接A题解知识点:贪心。对于一个轨道,要么一次性清理,要么一个一个清理。显然,如果行星个数大于直接清理的花费,那么选择直接清理,否则一个一个清理。即\(\sum\min(c,......
  • codeforces/AtCoder补题整理
    目录cf1738CEvenNumberAddicts(博弈/记忆化搜索)题意题解cf1739EResetKEdges(树,二分+贪心)题意题解cf1730DPrefixesandSuffixes(字符串,思维)题意题解cf1734DS......
  • Codeforces Global Round 22 C. Even Number Addicts(博弈论)
    https://codeforces.com/contest/1738/problem/C题目大意:给定n个数字,Alice先手,Bob后手;拿完之后,Alice数字总和为奇数时Alice获胜,否则Bob获胜。问我们给定n个数字,双方......
  • python pygame 迷宫生成
    importrandomimportsysimportpygame#使用pygame之前必须初始化pygame.init()#参数设置box_w,box_h=5,5#盒子宽高window_w,window_h=400,400x,y=0,0#盒......