TC SRM 547 div2
250pt: 题意是给定一个正六边形,在正六边形内划三条不相交的线将其分成四个三角形,求最小的三角形的面积。果断地在推公式的时候把变量当做常量带入,所以致使公式推错。纠结了好久的。划分出来的四个三角形面积都是固定的,推公式计算最小就好了。 s = (sqrt(3)*L*L)/4;
500pt:自己没想出来,dp的一道题目;有n个柱子在一条直线方向排列,每个柱子i的高度的取值范围是[1,height[i]],若用一条绳子将每个柱子的最顶端连接起来(共形成n-1段),求所用绳子的最大长度。
dp[i][0]表示当前柱子i取最短1,dp[i][1]表示当前柱子i取最长height[i];
状态转移方程:
dp[i][0] = max(dp[i - 1][0] + w,dp[i - 1][1] + hypot(height[i - 1] - 1.0,w));
dp[i][1] = max(dp[i - 1][0] + hypot(height[i] - 1,w),dp[i - 1][1] + hypot(height[i] - height[i - 1],w));
#include <cstdio> #include <cmath> #include <iostream> #include <vector> #include <cstring> #define maxn 107 using namespace std; class PillarsDivTwo { public: double maximalLength(vector <int> height, int w) { double dp[maxn][2]; memset(dp,0,sizeof(dp)); int sz = height.size(); for (int i = 1; i < sz; ++i) { dp[i][0] = max(dp[i - 1][0] + w,dp[i - 1][1] + hypot(height[i - 1] - 1.0,w)); dp[i][1] = max(dp[i - 1][0] + hypot(height[i] - 1,w),dp[i - 1][1] + hypot(height[i] - height[i - 1],w)); } return max(dp[sz - 1][0],dp[sz - 1][1]); } };