Codeforces Round #226 (Div. 2) 总结

123 篇文章 0 订阅

A

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
Note

In the first sample the bear will lend a honey barrel at day 3 and then sell it for 7. Then the bear will buy a barrel for 3 and return it to the friend. So, the profit is (7 - 3 - 1) = 3.

In the second sample bear will lend a honey barrel at day 1 and then sell it for 100. Then the bear buy the barrel for 1 at the day 2. So, the profit is (100 - 1 - 2) = 97.

解: 找前一个减后一个差最大的就行。

#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
const int maxm=1e3+10;
const int inf=-1<<29;
int x[maxm];
int main()
{
    int n,c;
    while(scanf("%d%d",&n,&c)!=EOF)
    {
        for(int i=0;i<n;i++)
        {
            scanf("%d",&x[i]);
        }
        int Max=0;
        for(int i=0;i<n-1;i++)
        {
            Max=max(Max,x[i]-x[i+1]-c);
        }
        printf("%d\n",Max);
    }
    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
Note

In the first sample, the following pairs (i, j) match: (1, 4), (1, 5), (1, 6), (1, 7), (1, 8), (1, 9).

In the second sample, the following pairs (i, j) match:(1,  4), (1,  5), (1,  6), (1,  7), (1,  8), (1,  9), (1,  10), (1,  11), (2,  10), (2,  11), (3,  10), (3,  11), (4,  10), (4,  11), (5,  10), (5,  11), (6,  10), (6,  11), (7,  10), (7,  11).

解: 找到"bear“,然后计算前后有多少字母,就可以求出有多少组合数,注意要减去重复计算的,复杂度为O(n)。
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
const int maxm=1e4+10;
char s[maxm];
int main()
{
    while(scanf("%s",s)!=EOF)
    {
        int len=strlen(s);
        int sum=0;
        int x=0;
        for(int i=0;i<len;i++)
        {
            if(s[i]=='b'&&s[i+1]=='e'&&s[i+2]=='a'&&s[i+3]=='r')
            {
                sum+=(i+1)*(len-i-3)-x*(len-i-3);
                x=i+1;
            }
        }
        printf("%d\n",sum);
    }
    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
Note

Consider the first sample. Overall, the first sample has 3 queries.

  1. The first query l = 2r = 11 comes. You need to count f(2) + f(3) + f(5) + f(7) + f(11) = 2 + 1 + 4 + 2 + 0 = 9.
  2. The second query comes l = 3r = 12. You need to count f(3) + f(5) + f(7) + f(11) = 1 + 4 + 2 + 0 = 7.
  3. The third query comes l = 4r = 4. As this interval has no prime numbers, then the sum equals 0。
解: 在计算质数的时候,可以把f也顺便求出来,如2是质数,那么4,6,8都不是质数,但是如果输入中有4,6,8的话,那么他们就可以被2这个质数整除。 另外,像这种有m个query的题目,题目给定left和right,一般都处理成[0,i]的的结果,这样在求答案的时候只要[0.right]-[0,left]就行,而不用再去遍历求解,复杂度降低了
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
const int maxm=1e7+10;
int vis[maxm];
int f[maxm];
int b[maxm];
void Init()
{
    for(int i=2;i<=maxm;i++)
    {
        if(vis[i]==0)
        {
            for(int j=i;j<=maxm;j+=i)
            {
                f[i]+=b[j];
                vis[j]=1;
            }
        }
    }
    for(int i=3;i<=maxm;i++)
    {
        f[i]+=f[i-1];
    }
}
int main()
{
    int n;
    while(scanf("%d",&n)!=EOF)
    {
        memset(f,0,sizeof(f));
        memset(vis,0,sizeof(vis));
        memset(b,0,sizeof(b));
        int a,l,r,q;
        for(int i=0;i<n;i++)
        {
            scanf("%d",&a);
            b[a]++;
        }
        Init();
        scanf("%d",&q);
        int Max=1e7;
        for(int i=0;i<q;i++)
        {
            scanf("%d%d",&l,&r);
            l=min(Max,l);
            r=min(Max,r);
            printf("%d\n",f[r]-f[l-1]);
        }
    }
    return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值