Codeforces Round #321(Div.2) (2/5)

maksyuki 发表于 比赛 分类,标签:
0

A Kefa and First Steps

Kefa decided to make some money doing business on the Internet for exactly n days. He knows that on the i-th day (1 ≤ i ≤ n) he makes ai money. Kefa loves progress, that's why he wants to know the length of the maximum non-decreasing subsegment in sequence ai. Let us remind you that the subsegment of the sequence is its continuous fragment. A subsegment of numbers is called non-decreasing if all numbers in it follow in the non-decreasing order.

Help Kefa cope with this task!

Input

The first line contains integer n (1 ≤ n ≤ 105).

The second line contains n integers a1,  a2,  ...,  an (1 ≤ ai ≤ 109).

Output

Print a single integer — the length of the maximum non-decreasing subsegment of sequence a.

Sample test(s)

input

6
2 2 1 3 4 1

output

3

input

3
2 2 9

output

3

Note

In the first test the maximum non-decreasing subsegment is the numbers from the third to the fifth one.

In the second test the maximum non-decreasing subsegment is the numbers from the first to the third one.

 

题目类型:线性DP

算法分析:dp[i]表示以第i个字符结尾的最长连续非递减子序列的长度,则边界为dp[1] = 1,状态转移方程为:若a[i-1] <= a[i],则dp[i] = dp[i-1] + 1;否则dp[i] = 1

B Kefa and Company

Kefa wants to celebrate his first big salary by going to restaurant. However, he needs company.

Kefa has n friends, each friend will agree to go to the restaurant if Kefa asks. Each friend is characterized by the amount of money he has and the friendship factor in respect to Kefa. The parrot doesn't want any friend to feel poor compared to somebody else in the company (Kefa doesn't count). A friend feels poor if in the company there is someone who has at least d units of money more than he does. Also, Kefa wants the total friendship factor of the members of the company to be maximum. Help him invite an optimal company!

Input

The first line of the input contains two space-separated integers, n and d (1 ≤ n ≤ 105, ) — the number of Kefa's friends and the minimum difference between the amount of money in order to feel poor, respectively.

Next n lines contain the descriptions of Kefa's friends, the (i + 1)-th line contains the description of the i-th friend of type misi (0 ≤ mi, si ≤ 109) — the amount of money and the friendship factor, respectively.

Output

Print the maximum total friendship factir that can be reached.

Sample test(s)

input

4 5
75 5
0 100
150 20
75 1

output

100

input

5 100
0 7
11 32
99 10
46 8
87 54

output

111

Note

In the first sample test the most profitable strategy is to form a company from only the second friend. At all other variants the total degree of friendship will be worse.

In the second sample test we can take all the friends.

 

题目类型:排序

算法分析:首先将输入的数据按照money从小到大排序,然后使用两个指针从头往后扫,即常说的“追逐法”

 

算法分析:也可以使用二分的方法来做,此时枚举所有的点a,然后查找a的val值+d的下界,最后通过维护的一个前缀和来将最大值更新,注意此时所有的节点之间的money都要满足差小于d!!!所以只能够计算[val, val + d)的值,不能够计算(val - d, val + d)的值!!!

 

bzoj1087

maksyuki 发表于 oj 分类,标签:
0

1087: [SCOI2005]互不侵犯King

Description

在N×N的棋盘里面放K个国王,使他们互不攻击,共有多少种摆放方案。国王能攻击到它上下左右,以及左上左下右上右下八个方向上附近的各一个格子,共8个格子。

Input

只有一行,包含两个数N,K ( 1 <=N <=9, 0 <= K <= N * N) 阅读全文 »