Codeforces Round #289 (Div. 2, ACM ICPC Rules)C、E

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:

The prettiness of s equals

Find the prettiness of the given song title.

We assume that the vowels are I, E, A, O, U, Y.

Input

The input contains a single string s (1 ≤ |s| ≤ 5·105) — the title of the song.

Output

Print the prettiness of the song with the absolute or relative error of at most10 - 6.

Sample test(s)
Input
IEAIAIO
Output
28.0000000
Input
BYOB
Output
5.8333333
Input
YISVOWEL
Output
17.0500000
Note

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;
}


C. Sums of Digits
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

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.

Input

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.

Output

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.

Sample test(s)
Input
3
1
2
3
Output
1
2
3
Input
3
3
2
1
Output
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;
}





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值