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.
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.
Print a single integer — the answer to the problem.
5 1 5 10 7 3 20
3
6 2 100 1 10 40 10 40
97
3 0 1 2 3
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;
}
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 = b, sk + 1 = e, sk + 2 = a, sk + 3 = r.
Help the bear cope with the given problem.
The first line contains a non-empty string s (1 ≤ |s| ≤ 5000). It is guaranteed that the string only consists of lowercase English letters.
Print a single number — the answer to the problem.
bearbtear
6
bearaabearc
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;
}
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.
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.
Print m integers — the answers to the queries on the order the queries appear in the input.
6 5 5 7 10 14 15 3 2 11 3 12 4 4
9 7 0
7 2 3 5 7 11 4 8 2 8 10 2 123
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;
}