Codeforces Round #226 (Div. 2)

A. Bear and Raspberry
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

The bear decided to store some raspberry for the winter. He cunningly found out the price for a barrel of honey in kilos of raspberry for each of the following n days. According to the bear's data, on the i-th (1 ≤ i ≤ n) day, the price for one barrel of honey is going to is xikilos of raspberry.

Unfortunately, the bear has neither a honey barrel, nor the raspberry. At the same time, the bear's got a friend who is ready to lend him a barrel of honey for exactly one day for c kilograms of raspberry. That's why the bear came up with a smart plan. He wants to choose some day d (1 ≤ d < n), lent a barrel of honey and immediately (on day d) sell it according to a daily exchange rate. The next day (d + 1)the bear wants to buy a new barrel of honey according to a daily exchange rate (as he's got some raspberry left from selling the previous barrel) and immediately (on day d + 1) give his friend the borrowed barrel of honey as well as c kilograms of raspberry for renting the barrel.

The bear wants to execute his plan at most once and then hibernate. What maximum number of kilograms of raspberry can he earn? Note that if at some point of the plan the bear runs out of the raspberry, then he won't execute such a plan.

Input

The first line contains two space-separated integers, n and c (2 ≤ n ≤ 100, 0 ≤ c ≤ 100), — the number of days and the number of kilos of raspberry that the bear should give for borrowing the barrel.

The second line contains n space-separated integers x1, x2, ..., xn (0 ≤ xi ≤ 100), the price of a honey barrel on day i.

Output

Print a single integer — the answer to the problem.

Sample test(s)
input
5 1
5 10 7 3 20
output
3
input
6 2
100 1 10 40 10 40
output
97
input
3 0
1 2 3
output
0

题意:借罐子卖掉然后第二天买回来还别人,问最大差价

题解:直接扫一遍。。。

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<math.h>
long long a[108],ma,c;
int main()
{
    int n,i;

    scanf("%d%I64d",&n,&c);
    for(i=0; i<n; i++) scanf("%I64d",a+i);
    ma=0;
    for(i=1; i<n; i++)
    {
        if(a[i-1]-a[i]>ma) ma=a[i-1]-a[i];
    }
    printf("%I64d\n",ma-c>0?ma-c:0);

    return 0;
}


B. Bear and Strings
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

The bear has a string s = s1s2... s|s| (record |s| is the string's length), consisting of lowercase English letters. The bear wants to count the number of such pairs of indices i, j (1 ≤ i ≤ j ≤ |s|), that string x(i, j) = sisi + 1... sj contains at least one string "bear" as a substring.

String x(i, j) contains string "bear", if there is such index k (i ≤ k ≤ j - 3), that sk = bsk + 1 = esk + 2 = ask + 3 = r.

Help the bear cope with the given problem.

Input

The first line contains a non-empty string s (1 ≤ |s| ≤ 5000). It is guaranteed that the string only consists of lowercase English letters.

Output

Print a single number — the answer to the problem.

Sample test(s)
input
bearbtear
output
6
input
bearaabearc
output
20

题意:问包含bear的字串有多少个

题解:从前面开始扫描,如果后面有bear,那么由第一个字符的后缀,直到该字符的后缀,都是符合的,反之不符合

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<math.h>
int res[5005];
char s[5005];
int main()
{
    int ans,now,i,all,len;

    while(scanf("%s",s)>0)
    {
        len=strlen(s);
        for(all=ans=now=i=0;i<len;i++)
        {
            if(s[i]=='b') now=1;
            else if(s[i]=='e'&&now==1) now=2;
            else if(s[i]=='a'&&now==2) now=3;
            else if(s[i]=='r'&&now==3) now=0,res[all++]=i;
            else now=0;
        }
        now=0;
        for(i=0;i<len;i++)
        {
            while(now<all&&res[now]-3<i) now++;
            if(now>=all) continue;
            ans+=len-res[now];
        }
        printf("%d\n",ans);
    }

    return 0;
}


C. Bear and Prime Numbers
time limit per test
2 seconds
memory limit per test
512 megabytes
input
standard input
output
standard output

Recently, the bear started studying data structures and faced the following problem.

You are given a sequence of integers x1, x2, ..., xn of length n and m queries, each of them is characterized by two integers li, ri. Let's introduce f(p) to represent the number of such indexes k, that xk is divisible by p. The answer to the query li, ri is the sum: , where S(li, ri) is a set of prime numbers from segment [li, ri] (both borders are included in the segment).

Help the bear cope with the problem.

Input

The first line contains integer n (1 ≤ n ≤ 106). The second line contains n integers x1, x2, ..., xn (2 ≤ xi ≤ 107). The numbers are not necessarily distinct.

The third line contains integer m (1 ≤ m ≤ 50000). Each of the following m lines contains a pair of space-separated integers, li and ri (2 ≤ li ≤ ri ≤ 2·109) — the numbers that characterize the current query.

Output

Print m integers — the answers to the queries on the order the queries appear in the input.

Sample test(s)
input
6
5 5 7 10 14 15
3
2 11
3 12
4 4
output
9
7
0
input
7
2 3 5 7 11 4 8
2
8 10
2 123
output
0
7

题解:在筛素数的时候,直接计算每个素数的倍数的个数,然后加起来,最后问的时候直接相减就是答案了

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<math.h>
#define MAXN 10000000
int num[MAXN+8],mark[MAXN+8],sum[MAXN+8];
int main()
{
    int i,j,n,m,l,r;

    //freopen("t.txt","r",stdin);
    memset(num,0,sizeof(num));
    memset(sum,0,sizeof(sum));
    memset(mark,0,sizeof(mark));
    scanf("%d",&n);
    for(i=0;i<n;i++)
    {
        scanf("%d",&l);
        num[l]++;
    }
    for(i=2;i<MAXN;i++)
    {
        if(!mark[i])
        {
            for(j=i; j<=MAXN; j+=i)
            {
                sum[i]+=num[j];
                mark[j]=1;
            }
        }
        sum[i]+=sum[i-1];
    }

    scanf("%d",&m);
    while(m--)
    {
        scanf("%d%d",&l,&r);
        if(l>=MAXN) l=MAXN;
        if(r>=MAXN) r=MAXN-1;
        printf("%d\n",sum[r]-sum[l-1]);
    }

    return 0;
}


1、资源项目源码均已通过严格测试验证,保证能够正常运行;、 2项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行;、 2项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值