uva 11598 - Optimal Segments(DP+暴力)

这是一个关于如何在限制条件下将单元格划分为多个段的问题,每个段至少包含一个特殊单元格。通过动态规划计算不同状态下的最优分段方案,以最小化权重比例。当比例超过一定阈值时,输出'overflow',否则输出分段结果。

C

Optimal Segments

Input: Standard Input

Output: Standard Output

Consider a grid of size 1 x N. Each cell of the grid has the following properties

· Cell C of the grid has a value of VC(1 C N)

· The value of each cell is a positive integer less than26

· Some of the cells arespecial and they are represented with the character X

· Cell C has a weight of (two to the power of cell value)

· The special cells have weights of1

You will be given the values of these N cells and your job will be to divide these intoK segments so that

· Each segment contains at least one cell

· There is at least onespecial cell in each segment

The weight of a segment is equal to the product of the weights of the cells it contains. You have to form segments in such a way so that ratio

(Highest weight of all the segments) / (Lowest weight of all the segments) is minimized.

In case there are multiple answers with the same lowest ratio, you have to make sure the number of cells in the first segment is maximized. If there is still a tie, then make sure the number of cells in the second segment is maximized and so on.

Example:

N = 5 and K = 2
Cell values = {1 2 X 3 X }
Cell weights = {2 4 1 8 1}
Optimal segmentation = (2 4 1)(8 1)
Weights of segments = (8)(8)
Ratio = 1
Final Result = (1 2 X)(3 X)

Input

The first line of input is an integer T(T 200) that indicates the number of test cases. Each case starts with two integersN(1 < N < 31) and K(1 < K < 16). The meaning ofN and K are mentioned above. The next line containsN integers where the Ith integer gives the value ofVI. The integers that are special will be represented byX.

Output

For each case, output the case number first. If there is no way to divide the N cells into K segments, meeting the constraints above, then print “not possible!” If there is a way but the ratio is greater than1015 then print “overflow”. If the ratio is not greater than 1015 then output the ratio first followed by the segmentations. Each segment is enclosed by brackets. Look at the output for detailed format.

Sample Input Output for Sample Input

4

5 2

1 2 X 3 X

6 3

X X 2 3 4 5

10 3

X X X 25 25 25 25 25 25 25

10 3

4 X 3 1 X 3 X X 3 2

Case 1: 1 (1 2 X)(3 X)

Case 2: not possible!

Case 3: overflow

Case 4: 8 (4 X 3)(1 X 3 X)(X 3 2)


这题我用了一个特别笨的方法,先用状态DP算好每个状态的方法数,在取个差值最小的!

int dp[n][k]表示遍历到第n个数时要划分成k分的可行性,可以为1,不可以为0;

vector<ll> dp_max[n][k]表示遍历到第n个数时要划分成k分,所有情况的最大值;

vector<ll> dp_min[n][k]表示遍历到第n个数时要划分成k分,所有情况的最小值;

vector<ll> Next[n][k]表示遍历到第n个数时要划分成k分,所以情况的下一划;

vector<ll> Next[n][k]表示遍历到第n个数时要划分成k分,所以情况来自下一划的第几个情况。

最后:

#include <iostream>
#include <cstdio>
#include <sstream>
#include <vector>
using namespace std;

#define ll long long
const int maxn = 35;
int vis[maxn][maxn];
ll dp[maxn][maxn] ,value[maxn];
vector<ll> dp_max[maxn][maxn] , dp_min[maxn][maxn] , next[maxn][maxn] , Next[maxn][maxn];
string num[maxn];
int N , K;

void initial(){
	for(int i = 0 ; i < maxn ; i++){
		for(int j = 0 ; j < maxn ; j++){
			dp[i][j] = 0;
			next[i][j].clear();
			Next[i][j].clear();
			dp_max[i][j].clear();
			dp_min[i][j].clear();
			vis[i][j] = 0;
		}
		value[i] = 0;
		num[i].clear();
	}
}

void readcase(){
	scanf("%d%d" , &N , &K);
	for(int i = 0 ; i < N ; i++){
		cin >> num[i];
		if(num[i] != "X"){
			stringstream ss;
			ss << num[i];
			ss >> value[i];
		}
	}
}

ll DP(int n , int k){
	if(N-n < k) return 0;
	if(n >= N){
		if(k == 0) return 1;
		return 0;
	}
	if(vis[n][k] == 1){
		return dp[n][k];
	}
	vis[n][k] = 1;
	ll sum = 0 , T = 0;
	for(int i = n;i < N;i++){
		sum += value[i];
		if(num[i] == "X") T = 1;
		if(T && DP(i+1 , k-1)){
			dp[n][k] = 1;
			for(int j = 0;j < dp_max[i+1][k-1].size();j++){
				dp_max[n][k].push_back(max(sum , dp_max[i+1][k-1][j]));
				dp_min[n][k].push_back(min(sum , dp_min[i+1][k-1][j]));
				Next[n][k].push_back(i+1);
				next[n][k].push_back(j);
			}
		}
	}
	return dp[n][k];
}

void out(int len){
	int n = 0 , tNext = Next[0][K][len],  tnext = next[0][K][len], k = K;
	while(tNext != -1){
		int i = 0;
		while(n < tNext){
			if(i == 0){
				printf("(%s" , num[n].c_str());
			}else{
				printf(" %s" , num[n].c_str());
			}
			n++;
			i++;
		}
		printf(")");
		int t = tnext;
		tnext = next[n][k-1][t];
		tNext = Next[n][k-1][t];
		k--;
		
	}
	printf("\n");
}

void computing(){
	dp_min[N][0].push_back(1e16);
	dp_max[N][0].push_back(0);
	Next[N][0].push_back(-1);
	next[N][0].push_back(0);
	if(DP(0 , K)){
		ll ans = 1e16;
		int k = 0;
		for(int i = 0 ; i < Next[0][K].size() ; i++){
			if(ans >= dp_max[0][K][i]-dp_min[0][K][i]){
				ans = dp_max[0][K][i]-dp_min[0][K][i];
				k = i;
			}
		}
		if(ans >= 50){
			printf("overflow\n");
		}else{
			printf("%lld " , ((long long)1 << ans));
			out(k);
		}
	}else{
		printf("not possible!\n");
	}
}

int main(){
	int t;
	scanf("%d" , &t);
	for(int i = 1;i <= t;i++){
		initial();
		readcase();
		printf("Case %d: " , i);
		computing();
	}
	return 0;
}


<think>嗯,用户想了解PostgreSQL中try_vector_engine_strategy参数里的off、force和optimal三个选项的具体含义。首先,我需要确认这个参数是否真实存在,因为根据我的知识库,PostgreSQL并没有这个参数。可能用户混淆了其他数据库系统或扩展的功能,比如ClickHouse的向量化执行引擎,或者某些定制化的PostgreSQL分支。 接下来,我应该检查用户提供的引用内容,看看是否有相关线索。引用内容提到了Hive的安装配置和MySQL驱动,这些和PostgreSQL的参数无关,可能用户是在配置其他系统时遇到的问题。但需要明确告知用户这一点,避免误导。 然后,考虑用户可能的实际需求。用户可能在使用某个支持向量化执行的PostgreSQL扩展,或者在使用基于PostgreSQL的数据库产品,如Greenplum或TimescaleDB,这些可能有类似的参数。需要建议用户查阅相关文档或确认参数的正确性。 最后,应该以清晰的结构解释off、force、optimal可能的含义,基于常见的数据库参数设计,比如off禁用功能,force强制使用,optimal由优化器决定。同时提醒用户PostgreSQL原生不支持该参数,并建议核实信息来源或检查扩展模块。</think>关于 PostgreSQL 中 `try_vector_engine_strategy` 参数的解释如下: ### 参数说明 根据 PostgreSQL 官方文档和已知配置参数体系,**`try_vector_engine_strategy` 并非 PostgreSQL 原生支持的参数**。此参数名称更可能出现在以下场景中: 1. 基于 PostgreSQL 的定制化分支或扩展(如面向向量化计算的插件) 2. 用户对参数名称的误写或混淆(例如与 `enable_` 开头的优化器参数混淆) 3. 特定云数据库产品的自定义参数 ### 相似参数分析 若假设该参数与**向量化执行引擎**相关,其选项可能对应以下行为(以典型向量化引擎设计为参考): | 参数值 | 可能含义 | |-----------|--------------------------------------------------------------------------| | `off` | 完全禁用向量化执行策略,强制使用传统行存执行模式 | | `force` | 强制启用向量化执行,即使优化器认为行存模式更优 | | `optimal` | 由优化器根据代价估算自动选择向量化或行存执行模式(默认值) | ### 使用建议 1. 通过以下命令验证参数是否存在: ```sql SHOW try_vector_engine_strategy; ``` 2. 检查是否安装了向量化执行扩展(如 `pg_vector`、`citus` 等) 3. 若使用云数据库(如 AWS Aurora、Azure PostgreSQL),需查阅对应服务商的文档 ### 典型场景示例 ```sql -- 查看当前执行计划是否使用向量化 EXPLAIN ANALYZE SELECT * FROM large_table WHERE vector_column @> ARRAY[1,2,3]; -- 修改参数测试性能差异(假设参数存在) SET try_vector_engine_strategy = 'force'; ``` ### 参考资料建议 1. PostgreSQL 官方参数列表:https://www.postgresql.org/docs/current/runtime-config.html 2. 向量化执行扩展文档(如 Citus 或 pg_vendor 的说明) 3. 云服务商提供的数据库参数说明
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值