首页 > 其他分享 >The 2nd Universal Cup. Stage 4: Taipei - I(状压DP)

The 2nd Universal Cup. Stage 4: Taipei - I(状压DP)

时间:2023-10-08 16:45:07浏览次数:33  
标签:typedef Cup int Universal 状压 long pair 操作 define

目录

I. Interval Addition

题意
给定一个长度为 n $(1\le n \le 23) $ 的数组 a。你可以进行一种操作:选择区间 \([l, r]\) 并给这个区间所有的数都加上一个任意的数。问你使得整个数组均为 0 所需的最小操作次数?

思路
考虑差分数组
无论怎么对于区间 \([l, r]\) 进行操作,\(b_l + b_{r + 1}\) 的值保持不变
对于每个数单独操作,至多 n 次即可达成目标。如果说我们对分段连续的区间进行操作,那么其构成的大区间可以减少一次总的操作次数。

代码

//>>>Qiansui
#include<bits/stdc++.h>
#define ll long long
#define ull unsigned long long
#define mem(x,y) memset(x, y, sizeof(x))
#define debug(x) cout << #x << " = " << x << '\n'
#define debug2(x,y) cout << #x << " = " << x << " " << #y << " = "<< y << '\n'
//#define int long long

using namespace std;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<ull, ull> pull;
typedef pair<double, double> pdd;
/*

*/
const int N = 25 + 5, M = 1 << 23 | 3, inf = 0x3f3f3f3f;
const ll INF = 0x3f3f3f3f3f3f3f3f, mod = 998244353;
int n, a[N], b[N], ans, dp[M];
ll sum[M];

void solve(){
	cin >> n;
	for(int i = 1; i <= n; ++ i) cin >> a[i];
	for(int i = 0; i < n; ++ i) b[i] = a[i + 1] - a[i];
	for(int i = 1, ed = (1 << n) - 1; i <= ed; ++ i){
		for(int j = 0; j < n; ++ j){
			if(i >> j & 1){
				sum[i] = sum[i - (1 << j)] + b[j];
				break;
			}
		}
	}
	dp[0] = -1;
	for(int i = 0, ed = (1 << n) - 1; i <= ed; ++ i){
		if(sum[i] == 0) ++ dp[i];
		for(int j = 0; j < n; ++ j){
			if((i >> j & 1) == 0){
				dp[i | 1 << j] = max(dp[i | 1 << j], dp[i]);
			}
		}
		ans = max(ans, dp[i]);
	}
	cout << n - ans << '\n';
	return ;
}

signed main(){
	ios::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
	int _ = 1;
	// cin >> _;
	while(_ --){
		solve();
	}
	return 0;
}

标签:typedef,Cup,int,Universal,状压,long,pair,操作,define
From: https://www.cnblogs.com/Qiansui/p/17749537.html

相关文章

  • 代码源:互不侵犯(SCOI,状压DP)
    点击查看代码#include<bits/stdc++.h>usingnamespacestd;intn,m;longlongf[10][1024][100];intv[1024];voidinit(){ for(inti=1;i<1<<n;++i) { intc=0; for(intj=i;j;j=j&(j-1))c++; v[i]=c; }}intmain(){ ios::sync_with_std......
  • The 2018 ACM-ICPC Asia Qingdao Regional Contest, Online (The 2nd Universal Cup
    The2018ACM-ICPCAsiaQingdaoRegionalContest,Online(The2ndUniversalCup.Stage1:Qingdao)J-PresstheButton\(1\leqa,b,c,d\leq10^6\)题解容易发现存在循环节,每次位于\(gcd(a,c)\)的倍数的位置所以我们考虑处理一个循环节内的情况如果\(v\le......
  • Gym 104270 The 2018 ICPC Asia Qingdao Regional Programming Contest (The 1st Univ
    A.SequenceandSequenceB.KawaExam可以发现,对答案会产生影响的只有割边,把所有边双缩起来,然后就是一个森林。考虑一个树的时候怎么做,就是对于每条边求出这条边两端的众数个数,考虑线段树合并,每次动态维护子树内的众数和子树外的众数。#include<iostream>#include<cstdio>......
  • qoj6735. Tree (The 1st Universal Cup. Stage 22: Shaanxi)
    https://qoj.ac/contest/1287/problem/6735考虑定一个根,然后把每个点的点权附属在父边权上,让每条边的边权变成一个pair。这样,一个符合条件的路径需要满足的条件是:路径内所有边的边权pair相同,以及路径根节点(lca)的颜色符合。对于当前树上每个边权相同的连通块分开考虑。对......
  • CF957 Codeforces Round 472 (rated, Div. 2, based on VK Cup 2018 Round 2)
    CF957ATritonicIridescence如果原序列中有两个相同的字符,显然不合法。如果开头或者结尾为?,或者有两个连续的?,或者一个?两边的字符不同显然合法。否则一定不合法。#include<iostream>#include<cstdio>usingnamespacestd;constintN=105;intn;chars[N];intma......
  • CF1079 Codeforces Round 522 (Div. 2, based on Technocup 2019 Elimination Round 3
    CF1079AKitchenUtensils令\(c_i\)表示餐具\(i\)出现的数量,最小的餐具套数为\(t=\lceil\frac{\max\{c_i\}}{k}\rceil\),按照这个计算就好了。#include<iostream>#include<cstdio>#include<algorithm>usingnamespacestd;constintN=105;intn,k;inta[N]......
  • CF1072 Codeforces Round 517 (Div. 2, based on Technocup 2019 Elimination Round 2
    CF1072AGoldenPlate第\(i\)个矩形的周长为\(2(w-4(i-1))+2(h-4(i-1))-4\),枚举\(i\)求和。#include<iostream>#include<cstdio>usingnamespacestd;intn,m,k;intmain(){ scanf("%d%d%d",&n,&m,&k); intans=0; for(i......
  • CF1162 Codeforces Round 557 (Div. 2) [based on Forethought Future Cup - Final Ro
    CF1162AZoningRestrictionsAgain每个位置越高越好,暴力模拟即可。#include<iostream>#include<cstdio>usingnamespacestd;constintN=55;intn,h,m;inta[N];intmain(){ scanf("%d%d%d",&n,&h,&m); for(inti=1;i<=n;i++) a[i]=h;......
  • 动态规划——状压DP 学习笔记
    动态规划——状压DP学习笔记引入前置知识:位运算动态规划的过程是随着阶段的增长,在每个状态维度上不断扩展的。在任意时刻,已经求出最优解的状态与尚未求出最优解的状态在各维度上的分界点组成了DP扩展的“轮廓”。对于某些问题,我们需要在动态规划的“状态”中记录一个集合......
  • Technocup 2022 - Elimination Round 2 Two Arrays
    给定两个数组\(a_1,a_2,\cdots,a_n\)和\(b_1,b_2,\cdots,b_n\)。定义\(a\)的一次操作:选择任意一个非负整数\(k(0\leqk\leqn)\)。选择任意\(k\)个独立的下标\(i_1\leqi_2\leq\cdots\leqi_k\leqn\)。对\(a_{i_1},a_{i_2},\cdots,a_{i_k}\)......