E. Pretty Song
When Sasha was studying in the seventh grade, he started listening to music a lot. In order to evaluate which songs he likes more, he introduced the notion of the song'sprettiness. The title of the song is a word consisting of uppercase Latin letters. Theprettiness of the song is the prettiness of its title.
Let's define the simple prettiness of a word as the ratio of the number of vowels in the word to the number of all letters in the word.
Let's define the prettiness of a word as the sum ofsimple prettiness of all the substrings of the word.
More formally, let's define the function vowel(c) which is equal to1, if c is a vowel, and to0 otherwise. Let si be thei-th character of string s, and si..j be the substring of words, staring at the i-th character and ending at thej-th character (sisi + 1...sj, i ≤ j).
Then the simple prettiness of s is defined by the formula:
![](https://i-blog.csdnimg.cn/blog_migrate/6838934d4c5f560151227ef39c9db128.png)
The prettiness of s equals
![](https://i-blog.csdnimg.cn/blog_migrate/a9f0237738a9dc85b2ba62533eb9658c.png)
Find the prettiness of the given song title.
We assume that the vowels are I, E, A, O, U, Y.
The input contains a single string s (1 ≤ |s| ≤ 5·105) — the title of the song.
Print the prettiness of the song with the absolute or relative error of at most10 - 6.
IEAIAIO
28.0000000
BYOB
5.8333333
YISVOWEL
17.0500000
In the first sample all letters are vowels. The simple prettiness of each substring is 1. The word of length7 has 28 substrings. So, theprettiness of the song equals to 28.
思路(转载):
我们单独考虑每一个元音字符,对于一个元音字符,先考虑以它为结尾字符的串,设这个字符的位置为i,字符串长度为len
则有 (1 / 2) + ...+ (1 / (i + 1))
再考虑 以它开始的串
有 (1 / 2) + ...+ (1 / (len - i ))
单个字符的串 1个
这个字符在串中间出现:
(1 / 3) + (1 / 4) + ... + (1 / (len - i + 1)) = sum[len - i + 1] - sum[2]
(1 / 4) + (1 / 5) + ... + (1 / (len - i + 2)) = sum[len - i + 2] - sum[3]
...
(1 / (i + 2)) + (1 / (i + 3)) + ... + (1 / len) = sum[len] - sum[i + 1]
各个式子相加: sum[len - i + 1] + ... + sum[len] - (sum[2] + sum[3] + .. + sum[i + 1]) = t[len] - t[len - i - 1] - (t[i + 1] - t[1])
于是只要维护前缀和和它的前缀和就行了
#include<iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include<vector>
#include<cmath>
#include<queue>
#include<stack>
#include<map>
#include<set>
#include<algorithm>
using namespace std;
typedef long long LL;
const int maxn=500010;
char str[maxn];
double sum[maxn],t[maxn];
bool vovml(char x)
{
return x=='A'||x=='E'||x=='I'||x=='O'||x=='U'||x=='Y';
}
void init()
{
sum[1]=1;
for(int i=2;i<maxn;i++)
sum[i]=(1.0/i)+sum[i-1];
t[1]=1;
for(int i=2;i<maxn;++i)
t[i]=t[i-1]+sum[i];
}
int main ()
{
init();
while (scanf("%s", str)!=EOF)
{
int len=strlen(str);
double ans=0;
for(int i=0;i<len;i++)
{
if (vovml(str[i]))
{
ans+=sum[i+1]-1+sum[len-i];
ans+=t[len]-t[len-i] - t[i + 1] + t[1];
}
}
printf("%.7f\n", ans);
}
return 0;
}
Vasya had a strictly increasing sequence of positive integers a1, ..., an. Vasya used it to build a new sequence b1, ..., bn, where bi is the sum of digits of ai's decimal representation. Then sequence ai got lost and all that remained is sequence bi.
Vasya wonders what the numbers ai could be like. Of all the possible options he likes the one sequence with the minimum possible last number an. Help Vasya restore the initial sequence.
It is guaranteed that such a sequence always exists.
The first line contains a single integer number n (1 ≤ n ≤ 300).
Next n lines contain integer numbers b1, ..., bn — the required sums of digits. All bi belong to the range 1 ≤ bi ≤ 300.
Print n integer numbers, one per line — the correct option for numbers ai, in order of following in sequence. The sequence should be strictly increasing. The sum of digits of the i-th number should be equal to bi.
If there are multiple sequences with least possible number an, print any of them. Print the numbers without leading zeroes.
3 1 2 3
1 2 3
3 3 2 1
3 11 100首先构造好第一个,尽量为9,然后向下构造,如果b[i]>b[i-1],这种情况比较好说,只要把差值加到不够9的为上就可以了;否则,先把地位的收集起来,然后高一位的加一,然后重新分配低位
#include<iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include<vector>
#include<cmath>
#include<queue>
#include<stack>
#include<map>
#include<set>
#include<algorithm>
using namespace std;
const int maxn=2000;
int N;
int b[maxn],a[maxn];
int main()
{
while(scanf("%d",&N)!=EOF)
{
for(int i=0;i<N;i++)scanf("%d",&b[i]);
int x=b[0];
int cnt=0;
memset(a,0,sizeof(a));
while(x)
{
if(x>=9)a[cnt++]=9,x-=9;
else a[cnt++]=x,x=0;
}
for(int i=cnt-1;i>=0;i--)
printf("%d",a[i]);
int len=cnt;
printf("\n");
for(int i=1;i<N;i++)
{
if(b[i]>b[i-1])
{
x=b[i]-b[i-1];
int j=0;
for(j=0;x;j++)
if(9-a[j]>=x)a[j]+=x,x=0;
else x-=9-a[j],a[j]=9;
len=max(j,len);
}
else
{
x=b[i-1]-b[i];
int sum=0;
int pos;
for(int j=0;;j++)
{
if(sum+a[j]>x)
{
pos=j+1;
a[j]=0;
break;
}
else sum+=a[j],a[j]=0;
}
while(a[pos]==9)
pos++;
int y=a[pos]+1;
a[pos]=y;
x=0;
for(int j=len-1;j>pos;j--)
x+=a[j];
x=b[i]-a[pos]-x;
for(int j=0;j<pos;j++)
{
if(x>9)a[j]=9,x-=9;
else a[j]=x,x=0;
}
len=max(pos+1,len);
}
for(int j=len-1;j>=0;j--)
printf("%d",a[j]);
printf("\n");
}
}
return 0;
}