hdoj MAX Average Problem 2993 (斜率优化DP)
2016-02-29 22:21
393 查看
MAX Average Problem
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 7613 Accepted Submission(s): 1667
[align=left]Problem Description[/align]
Consider a simple sequence which only contains positive integers as a1, a2 ... an, and a number k. Define ave(i,j) as the average value of the sub sequence ai ... aj, i<=j. Let’s calculate max(ave(i,j)), 1<=i<=j-k+1<=n.
[align=left]Input[/align]
There multiple test cases in the input, each test case contains two lines.
The first line has two integers, N and k (k<=N<=10^5).
The second line has N integers, a1, a2 ... an. All numbers are ranged in [1, 2000].
[align=left]Output[/align]
For every test case, output one single line contains a real number, which is mentioned in the description, accurate to 0.01.
[align=left]Sample Input[/align]
10 6
6 4 2 10 3 8 5 9 4 1
[align=left]Sample Output[/align]
6.50//思路:http://www.docin.com/p-47950655.html一种新思想。。。//看网上的都是TLM。。。先贴上。。。
#include<stdio.h> #include<string.h> #include<algorithm> #define INF 0x3f3f3f3f #define ll long long #define N 100010 using namespace std; int sum ,s ,a ; bool cross(int i,int j,int k) { if((sum[j]-sum[i])*(k-i)>=(sum[k]-sum[i])*(j-i)) return true; return false; } double fx(int i,int t) { double tmp; tmp=1.0*(sum[t]-sum[i])/(t-i); return tmp; } int main() { int n,k; int i; while(scanf("%d%d",&n,&k)!=EOF) { sum[0]=0; for(i=1;i<=n;i++) { scanf("%d",&a[i]); sum[i]=sum[i-1]+a[i]; } int top=0,low=0; double ans=0; for(i=k;i<=n;i++) { int j=i-k; while(top-low>=1&&cross(s[top-1],s[top],j)) top--; s[++top]=j; while(top-low>=1&&fx(s[low+1],i)>=fx(s[low],i)) low++; ans=max(ans,fx(s[low],i)); } printf("%.2lf\n",ans); } return 0; }
相关文章推荐
- Valid Anagram
- LeetCode-102,103,107,111总结
- 第一行代码-10.2 使用HTTP协议访问网络
- [iOS]关于微信支付
- 安卓通知的使用系列6:对话框通知的使用之自定义对话框
- 【C++】C++发展和特性
- HTML5新增属性,新增元素
- Java的位运算
- DB2编目的认识
- CodeForces 606 B. Testing Robots(水~)
- some url
- 基于端口的访问控制协议802.1X
- 常用嵌入式汇编限定符
- php预防XSS
- [疯狂Java笔记]AWT:布局管理器、绝对布局、FlowLayout流式布局
- sqlite数据库的char,varchar,text,nchar,nvarchar,ntext的区别
- 基础算法
- CKEditor配置
- 函数指针
- cocos2d-x之多点触碰初试