COGS 1658. [HZOI 2014] 合并石子 解题报告

合并类动归就是要注意区间!!!
合并类动归就是要注意区间!!!
合并类动归就是要注意区间!!!

/*
状态定义:dp[i][j]表示合并i到j时的最大得分 sum_stone[i][j]表示i到j石子个数 
转移方程:dp[i][j]=max{dp[i][k]+dp[k+1][j]+sum_stone[i][j],dp[i][j]} 
边界:max{dp[i][n+i-1]}为答案
*/ 
#include<cstdio>
#include<iostream>
#include<algorithm>
using namespace std;

const int maxn=500;
int sum[maxn],dp[maxn][maxn][2],st[maxn];//0 for min,1 for max
int n;

void Init(){
	scanf("%d",&n);
	for(int i=1;i<=n;i++){
		scanf("%d",&st[i]);
		st[i+n]=st[i];
	}
	for(int i=1;i<=n*2;i++)sum[i]=sum[i-1]+st[i];
}
void DP(){
	for(int p=1;p<n;p++)//枚举合并区间长度 
		for(int i=1,j=i+p;(i<n*2) && (j<=n*2);i++,j=i+p){//确定区间起终点 
			dp[i][j][0]=1<<30,dp[i][j][1]=0;
			for(int k=i;k<j;k++){//枚举断点 
				dp[i][j][0]=min(dp[i][k][0]+dp[k+1][j][0]+sum[j]-sum[i-1],dp[i][j][0]);
				dp[i][j][1]=max(dp[i][k][1]+dp[k+1][j][1]+sum[j]-sum[i-1],dp[i][j][1]);
			}
		}
	int _max=-1,_min=1<<30;
	for(int i=1;i<=n;i++){
		_max=max(_max,dp[i][n+i-1][1]);
		_min=min(_min,dp[i][n+i-1][0]);
	}
	printf("%d\n%d",_min,_max);
}
int main()
{
	#ifndef DEBUG
		string FileName="stone2";
		freopen((FileName+".in").c_str(),"r",stdin);
		freopen((FileName+".out").c_str(),"w",stdout);
	#endif
	Init();
	DP();
}


以下是将代码修改为cot平滑的方法: 1. 首先,需要使用边界角的cot权重计算每个顶点的权重。 2. 然后,使用cot权重对每个顶点的邻域点进行加权计算,得到平滑后的坐标。 3. 最后,根据平滑后的坐标更新每个顶点的位置。 修改后的代码如下: float smooth() { float err = -1; cogs.clear(); v_end = mesh.vertices_end(); //cot平滑 for (v_it = mesh.vertices_begin(); v_it != v_end; ++v_it) { cog[0] = cog[1] = cog[2] = weight_sum = 0.0; for (vv_it = mesh.vv_iter(*v_it); vv_it.is_valid(); ++vv_it) { double cot_weight = 0.0; MyMesh::HalfedgeHandle heh = mesh.find_halfedge(*v_it, *vv_it); if (!mesh.is_boundary(heh)) { MyMesh::HalfedgeHandle prev_heh = mesh.prev_halfedge_handle(heh); MyMesh::HalfedgeHandle next_heh = mesh.next_halfedge_handle(heh); MyMesh::VertexHandle prev_vh = mesh.to_vertex_handle(prev_heh); MyMesh::VertexHandle next_vh = mesh.to_vertex_handle(next_heh); MyMesh::Point prev_p = mesh.point(prev_vh); MyMesh::Point curr_p = mesh.point(*v_it); MyMesh::Point next_p = mesh.point(next_vh); double cot_alpha = cot(prev_p - curr_p, next_p - curr_p); double cot_beta = cot(curr_p - prev_p, next_p - prev_p); cot_weight = cot_alpha + cot_beta; } cog += cot_weight * mesh.point(*vv_it); weight_sum += cot_weight; } cogs.push_back(cog / weight_sum); } for (v_it = mesh.vertices_begin(), cog_it = cogs.begin(); v_it != v_end; ++v_it, ++cog_it) { if (!mesh.is_boundary(*v_it)) { MyMesh::Point p = mesh.point(*v_it); err = max(err, (p - *cog_it).norm()); mesh.set_point(*v_it, *cog_it); } } return err; } 其中cot函数的定义如下: double cot(MyMesh::Point a, MyMesh::Point b) { return dot(a, b) / cross(a, b).norm(); } 注意,这里使用的是边界角的cot权重,因此在计算cot权重时需要判断当前边是否为边界。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值