1494: 连续子串和续

1494: 连续子串和续

http://www.acmore.net/problem.php?id=1494

Time Limit: 10 Sec  Memory Limit: 128 MB
Submit: 99  Solved: 18

Description

 

小Y要去参加一场笔试,这场笔试有N道题目,每道题目有不同的分值和难度,分别为ai和bi。小Y想知道从某一题开始,至少连续K道题目,分值的和与难度的和的比率最大是多少?

 

 

Input

 

若干组测试数据,每组测试数据占3行。

第一行输出两个正整数N(1<=N<=10^6),K(1<=K<=N)。

第二行N个正整数a1,a2...aN表示N道题目的分值,其中(1<=ai<=1000)。

第三行N个正整数b1,b2...bN表示N道题目的难度,其中(1<=bi<=1000)。

 

 

Output

 

每组测试数据输出一个实数,表示满足题意的最大的比率,精确到小数点后4位。

 

Sample Input

 

5 3 1 2 3 4 5 1 1 3 4 5 3 1 100 200 300 3 2 1

 

Sample Output

 

1.2000 300.0000

 

HINT

 

 

 

Source

 

Lyush

 

设一个比例参数p,然后就是解决一个ai-p*bi的序列,至少连续K个,sum{ ai-p*bi } >= 0的问题了,二分枚举这个参数即可

 

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<algorithm>
 5 
 6 using namespace std;
 7 
 8 #define eps 1e-8
 9 
10 const int INF=0x3f3f3f3f;
11 const int maxn=1000010;
12 
13 int n,k;
14 int a[maxn],b[maxn];
15 double num[maxn];
16 
17 int AC(double p){
18     double tmp=INF;
19     num[0]=0;
20     for(int i=1;i<=n;i++)
21         num[i]=num[i-1]+a[i]-p*b[i];
22     for(int i=k;i<=n;i++){
23         tmp=min(tmp,num[i-k]);
24         if(num[i]-tmp>=0)
25             return 1;
26     }
27     return 0;
28 }
29 
30 double BinSearch(double l,double r){
31     double mid,ans;
32     while(r-l>eps){
33         mid=(l+r)/2;
34         if(AC(mid)){
35             ans=mid;
36             l=mid+eps;
37         }else
38             r=mid-eps;
39     }
40     return ans;
41 }
42 
43 int main(){
44 
45     //freopen("input.txt","r",stdin);
46 
47     while(~scanf("%d%d",&n,&k)){
48         for(int i=1;i<=n;i++)
49             scanf("%d",&a[i]);
50         for(int i=1;i<=n;i++)
51             scanf("%d",&b[i]);
52         printf("%.4lf\n",BinSearch(0,1000));
53     }
54     return 0;
55 }

 

posted @ 2013-04-13 20:31  Jack Ge  阅读(386)  评论(0编辑  收藏  举报