7 26 个人比赛 to me
The heat during the last few days has been really intense. Scientists from all over the Berland study how the temperatures and weather change, and they claim that this summer is abnormally hot. But any scientific claim sounds a lot more reasonable if there are some numbers involved, so they have decided to actually calculate some value which would represent how high the temperatures are.
Mathematicians of Berland State University came up with a special heat intensity value. This value is calculated as follows:
Suppose we want to analyze the segment of n
consecutive days. We have measured the temperatures during these n days; the temperature during i-th day equals ai
.
We denote the average temperature of a segment of some consecutive days as the arithmetic mean of the temperature measures during this segment of days. So, if we want to analyze the average temperature from day x
to day y, we calculate it as ∑i=xyaiy−x+1 (note that division is performed without any rounding). The heat intensity value is the maximum of average temperatures over all segments of not less than k consecutive days. For example, if analyzing the measures [3,4,1,2] and k=3, we are interested in segments [3,4,1], [4,1,2] and [3,4,1,2]
(we want to find the maximum value of average temperature over these segments).
You have been hired by Berland State University to write a program that would compute the heat intensity value of a given period of days. Are you up to this task?
Input
The first line contains two integers n
and k (1≤k≤n≤5000
) — the number of days in the given period, and the minimum number of days in a segment we consider when calculating heat intensity value, respectively.
The second line contains n
integers a1, a2, ..., an (1≤ai≤5000) — the temperature measures during given n
days.
Output
Print one real number — the heat intensity value, i. e., the maximum of average temperatures over all segments of not less than k
consecutive days.
Your answer will be considered correct if the following condition holds: |res−res0|<10−6
, where res is your answer, and res0
is the answer given by the jury's solution.
Example
Input
4 3
3 4 1 2
Output
2.666666666666667
题意:第一行输入n k n代表有n天 下面一行n个数 代表n天的平均温度 求>=k天内 平均温度最大是多少 公式在题中
本来套了3重循环 结果时间超限 就不打算做了 比赛结束后听说有人用前缀和做 试了一下 就少了一个循环。。。。
还是不熟练 太菜了。。。。。。。。。
附上ac代码:
#include <iostream>
#include <algorithm>
#include<cstdio>
#include<stack>
#include <deque>
#include <cstdlib>
#include <cstring>
using namespace std;
stack <int> str;
typedef long long ll;
double a[5100],b[5100];
int main()
{
ll n,k,i,j,l;
double ans=0;
scanf("%lld%lld",&n,&k);
for(i=1; i<=n; i++)
scanf("%lf",&a[i]);
memset(b,0,sizeof(b));
b[1]=a[1];
for(i=2; i<=n; i++)
{
b[i]=b[i-1]+a[i];
}
for(i=1; i<=n; i++)
{
for(j=i; j<=n; j++)
{
if(j-i>=k-1)
{
double sum=0;
sum=b[j]-b[i-1];
double m=j-i+1;
ans=(double)max(ans,(double)sum/m);
}
}
}
printf("%lf\n",ans);
return 0;
}
You are given three integers a, b and x. Your task is to construct a binary string s of length n=a+b such that there are exactly a zeroes, exactly b ones and exactly x indices i (where 1≤i<n) such that si≠si+1
. It is guaranteed that the answer always exists.
For example, for the string "01010" there are four indices i
such that 1≤i<n and si≠si+1 (i=1,2,3,4). For the string "111001" there are two such indices i (i=3,5
).
Recall that binary string is a non-empty sequence of characters where each character is either 0 or 1.
Input
The first line of the input contains three integers a
, b and x (1≤a,b≤100,1≤x<a+b)
.
Output
Print only one string s
, where s
is any binary string satisfying conditions described above. It is guaranteed that the answer always exists.
Examples
Input
2 2 1
Output
1100
Input
3 3 3
Output
101100
Input
5 3 6
Output
01010100
Note
All possible answers for the first example:
- 1100;
- 0011.
All possible answers for the second example:
- 110100;
- 101100;
- 110010;
- 100110;
- 011001;
- 001101;
- 010011;
- 001011.
题意:输入abx 输出一个长度为a+b的字符串s 使得有x个s[i]不等于s[i-1]。。。。
没事多做做这种题 有利于提高智商
思路:判断a b的大小 谁大先输出谁 输出0101 直到x满足-1个不相等的s[i] s[i-1] 最后补全剩下的0 1(注意最后补全时 先判断上一个是谁补全谁)总会满足剩下一个不相等的
附上ac代码
#include <iostream>
#include <algorithm>
#include<cstdio>
#include<stack>
#include <deque>
#include <cstdlib>
#include <cstring>
using namespace std;
stack <int> str;
typedef long long ll;
char s[300];
int main()
{
int a,b,x,i;
char v='0',w='1';
scanf("%d%d%d",&a,&b,&x);
memset(s,0,sizeof(s));
if(a>=b)
{
s[0]=v;
a--;
i=1;
while(1)
{
if(x<=1)
break;
if(s[i-1]==v)
s[i++]=w,b--;
else
s[i++]=v,a--;
x--;
}
}
else
{
s[0]=w;
b--;
i=1;
while(1)
{
if(x<=1)
break;
if(s[i-1]==v)
s[i++]=w,b--;
else
s[i++]=v,a--;
x--;
}
}
if(s[i-1]==v)
{
while(a)
{
s[i++]=v;
a--;
}
while(b)
{
s[i++]=w;
b--;
}
}
else
{
while(b)
{
s[i++]=w;
b--;
}
while(a)
{
s[i++]=v;
a--;
}
}
s[i]=0;
puts(s);
return 0;
}
总结:平时基本功不扎实 还是要多多练习啊