题目链接:hdu 4923 Room and Moor
题目大意:给定一个序列a,元素由0,1组成,求一个序列b,元素在0~1之间,并且保证递增。输出最小的∑(ai−bi)2,
解题思路:首先剔除为首的0,和末尾的1,然后将中间部分成若干段由连续1开头,连续0结尾的各个段落。对于每一段有一个最优的值x=aa+b(a为1的个数,b为0的个数),用栈维护各个段的x值,如果当前x值小于前面一个段的x值,那么就要将两个段合并,a=ai−1+ai,b=bi−1+bi.
#include <cstdio>
#include <cstring>
#include <cmath>
#include <queue>
#include <stack>
#include <algorithm>
using namespace std;
const int maxn = 1e5+5;
int n, a[maxn], l, r;
struct state {
int p, q;
double x;
state (int p, int q) {
this->p = p;
this->q = q;
this->x = p * 1.0 / (p + q);
}
};
double solve () {
l = 0; r = n-1;
while (l < n && a[l] == 0)
l++;
while (r >= 0 && a[r] == 1)
r--;
if (r <= l)
return 0;
bool flag = true;
int cntp = 0, cntq = 0;
queue<state> que;
for (int i = l; i <= r; i++) {
if (flag && a[i] == 1)
cntp++;
else if (flag == false && a[i] == 0)
cntq++;
else if (flag == false && a[i] == 1) {
que.push(state(cntp, cntq));
cntp = 1;
flag = true;
} else {
cntq = 1;
flag = false;
}
}
que.push(state(cntp, cntq));
stack<state> sta;
while (!que.empty()) {
state u = que.front();
que.pop();
while (!sta.empty() && sta.top().x > u.x) {
u.p += sta.top().p;
u.q += sta.top().q;
u.x = u.p * 1.0 / (u.p + u.q);
sta.pop();
}
sta.push(u);
}
double ans = 0;
while (!sta.empty()) {
state u = sta.top();
sta.pop();
ans += (pow(1-u.x, 2) * u.p + pow(u.x, 2) * u.q);
}
return ans;;
}
int main () {
int cas;
scanf("%d", &cas);
while (cas--) {
scanf("%d", &n);
for (int i = 0; i < n; i++)
scanf("%d", &a[i]);
printf("%.6lf\n", solve());
}
return 0;
}
分享到:
相关推荐
HDU的1250,主要是利用高精度加法,但是代码有点繁琐,效率不是很高
HDU1059的代码
杭电ACMhdu1163
hdu1001解题报告
hdu 1574 passed sorce
HDU的一题........HDU DP动态规
hdu acm 教案 搜索入门 hdu acm 教案 搜索入门
hdu2101AC代码
搜索 dfs 解题代码 hdu1241
hdu acm 教案 动态规划(1) hdu acm 教案 动态规划(1)
ACM HDU题目分类,我自己总结的大概只有十来个吧
hdu 5007 Post Robot 字符串枚举。 暴力一下就可以了。
HDU最全ac代码
hdu 1166线段树代码
hdu动态规划算法集锦
hdu-acm源代码(上百题)hdu-acm源代码、hdu-acm源代码hdu-acm源代码
hdu题目分类
自己做的HDU ACM已经AC的题目
HDU图论题目分类
hdu 1005.比较简单的一道题,有兴趣的可以看看。