Codeforces Round #320 (Div. 2) [Bayan Thanks-Round] E 三分
2016-07-12 15:44
405 查看
链接:戳这里
E. Weakness and Poorness
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
You are given a sequence of n integers a1, a2, ..., an.
Determine a real number x such that the weakness of the sequence a1 - x, a2 - x, ..., an - x is as small as possible.
The weakness of a sequence is defined as the maximum value of the poorness over all segments (contiguous subsequences) of a sequence.
The poorness of a segment is defined as the absolute value of sum of the elements of segment.
Input
The first line contains one integer n (1 ≤ n ≤ 200 000), the length of a sequence.
The second line contains n integers a1, a2, ..., an (|ai| ≤ 10 000).
Output
Output a real number denoting the minimum possible weakness of a1 - x, a2 - x, ..., an - x. Your answer will be considered correct if its relative or absolute error doesn't exceed 10 - 6.
Examples
input
3
1 2 3
output
1.000000000000000
input
4
1 2 3 4
output
2.000000000000000
input
10
1 10 2 9 3 8 4 7 5 6
output
4.500000000000000
Note
For the first case, the optimal value of x is 2 so the sequence becomes - 1, 0, 1 and the max poorness occurs at the segment "-1" or segment "1". The poorness value (answer) equals to 1 in this case.
For the second sample the optimal value of x is 2.5 so the sequence becomes - 1.5, - 0.5, 0.5, 1.5 and the max poorness occurs on segment "-1.5 -0.5" or "0.5 1.5". The poorness value (answer) equals to 2 in this case.
题意:
给出n个数,找出一个实数x,使得weakness值最小
weakness:表示所有区间内的poorness的最大值
poorness:表示一段区间内的区间和的绝对值
思路:
由于是取绝对值的区间和,所以减去这个x使得值越大不行,越小也不行,肯定存在一个最优值使得它比x左边和右边都更优
三分这个极值,每次处理一下就可以了
至于怎么处理这个绝对值最大问题,我们先-x按区间段最大找一遍,再把数组去相反数找一次取max
第一次写三分,好像直接for100次来的更简单粗暴,毫无精度可言
代码:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<string>
#include<vector>
#include <ctime>
#include<queue>
#include<set>
#include<map>
#include<stack>
#include<iomanip>
#include<cmath>
#define mst(ss,b) memset((ss),(b),sizeof(ss))
#define maxn 0x3f3f3f3f
#define MAX 1000100
///#pragma comment(linker, "/STACK:102400000,102400000")
typedef long long ll;
typedef unsigned long long ull;
#define INF (1ll<<60)-1
using namespace std;
int n;
double a[200100],b[200100];
const double eps=1e-12;
double getmax(){
double ans=0,sum=0;
for(int i=1;i<=n;i++){
sum=sum+b[i];
if(sum<0) sum=0;
ans=max(ans,sum);
}
return ans;
}
double calc(double x){
for(int i=1;i<=n;i++) b[i]=a[i]-x;
double ans=getmax();
for(int i=1;i<=n;i++) b[i]=-b[i];
ans=max(ans,getmax());
return ans;
}
int main(){
scanf("%d",&n);
for(int i=1;i<=n;i++) scanf("%lf",&a[i]);
double l=-10000.0,r=10000.0,mid;
for(int i=0;i<100;i++){
double m1=l+(r-l)/3;
double m2=r-(r-l)/3;
if(calc(m1)<calc(m2)) r=m2;
else l=m1;
}
printf("%.12f\n",calc(l));
return 0;
}
E. Weakness and Poorness
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
You are given a sequence of n integers a1, a2, ..., an.
Determine a real number x such that the weakness of the sequence a1 - x, a2 - x, ..., an - x is as small as possible.
The weakness of a sequence is defined as the maximum value of the poorness over all segments (contiguous subsequences) of a sequence.
The poorness of a segment is defined as the absolute value of sum of the elements of segment.
Input
The first line contains one integer n (1 ≤ n ≤ 200 000), the length of a sequence.
The second line contains n integers a1, a2, ..., an (|ai| ≤ 10 000).
Output
Output a real number denoting the minimum possible weakness of a1 - x, a2 - x, ..., an - x. Your answer will be considered correct if its relative or absolute error doesn't exceed 10 - 6.
Examples
input
3
1 2 3
output
1.000000000000000
input
4
1 2 3 4
output
2.000000000000000
input
10
1 10 2 9 3 8 4 7 5 6
output
4.500000000000000
Note
For the first case, the optimal value of x is 2 so the sequence becomes - 1, 0, 1 and the max poorness occurs at the segment "-1" or segment "1". The poorness value (answer) equals to 1 in this case.
For the second sample the optimal value of x is 2.5 so the sequence becomes - 1.5, - 0.5, 0.5, 1.5 and the max poorness occurs on segment "-1.5 -0.5" or "0.5 1.5". The poorness value (answer) equals to 2 in this case.
题意:
给出n个数,找出一个实数x,使得weakness值最小
weakness:表示所有区间内的poorness的最大值
poorness:表示一段区间内的区间和的绝对值
思路:
由于是取绝对值的区间和,所以减去这个x使得值越大不行,越小也不行,肯定存在一个最优值使得它比x左边和右边都更优
三分这个极值,每次处理一下就可以了
至于怎么处理这个绝对值最大问题,我们先-x按区间段最大找一遍,再把数组去相反数找一次取max
第一次写三分,好像直接for100次来的更简单粗暴,毫无精度可言
代码:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<string>
#include<vector>
#include <ctime>
#include<queue>
#include<set>
#include<map>
#include<stack>
#include<iomanip>
#include<cmath>
#define mst(ss,b) memset((ss),(b),sizeof(ss))
#define maxn 0x3f3f3f3f
#define MAX 1000100
///#pragma comment(linker, "/STACK:102400000,102400000")
typedef long long ll;
typedef unsigned long long ull;
#define INF (1ll<<60)-1
using namespace std;
int n;
double a[200100],b[200100];
const double eps=1e-12;
double getmax(){
double ans=0,sum=0;
for(int i=1;i<=n;i++){
sum=sum+b[i];
if(sum<0) sum=0;
ans=max(ans,sum);
}
return ans;
}
double calc(double x){
for(int i=1;i<=n;i++) b[i]=a[i]-x;
double ans=getmax();
for(int i=1;i<=n;i++) b[i]=-b[i];
ans=max(ans,getmax());
return ans;
}
int main(){
scanf("%d",&n);
for(int i=1;i<=n;i++) scanf("%lf",&a[i]);
double l=-10000.0,r=10000.0,mid;
for(int i=0;i<100;i++){
double m1=l+(r-l)/3;
double m2=r-(r-l)/3;
if(calc(m1)<calc(m2)) r=m2;
else l=m1;
}
printf("%.12f\n",calc(l));
return 0;
}
相关文章推荐
- linux上安装部署activemq
- 5文件上传与下载
- HP-UX 11g RAC安装 记录
- SQL-order by两个字段同时排序
- 类型“GridView”的控件必须放在具有 runat=server 的窗体标记内?
- tomcat目录结构
- 安装oracle数据库软件报错
- leetcode 6.ZigZag Conversion
- js将html生成为图片,并保存在本地
- SpringMVC日期转换之JsonSerialize
- Tkinter教程之tkCommonDialog篇
- 机器学习笔记-SVM
- Imagenet classification with deep convolutional neural networks
- 数据库高级特性(听课讲的,如果之后用到再具体百度)
- 2使用struts2的标签库
- eclipse4.4 +mavne3.3.9 + tomcat7.0
- RecycleView + SwipeRefreshLayout 对adapter和刷新的封装
- AFNetWorking过滤NSNULL
- 实现Android页面回到顶部
- 最简单的Java工厂设计模式样例