2017-5-6-Train:Codeforces Round #321 (Div. 2)

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 a**i money. Kefa loves progress, that‘s why he wants to know the length of the maximum non-decreasing subsegment in sequencea**i. 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,  ...,  a**n (1 ≤ a**i ≤ 109).

Output

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

Examples

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

Code:

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 static const int MAXN = 1e5 + 10;
 4 static const int inf = 0x7fffffff;
 5 int data[MAXN];
 6 int dp[MAXN];
 7 int main()
 8 {
 9     int n;
10     scanf("%d" , &n);
11     for(int i = 1 ; i <= n ; ++i)
12     {
13         scanf("%d" , data + i);
14     }
15     int cnt = 1 , ans = 0;
16     for(int i = 2 ; i <= n ; ++i)
17     {
18         if(data[i] >= data[i - 1])
19         {
20             ++cnt;
21         }
22         else
23         {
24             ans = max(ans , cnt);
25             cnt = 1;
26         }
27     }
28     printf("%d" , max(ans , cnt));
29 }

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 m**i, s**i (0 ≤ m**i, s**i ≤ 109) — the amount of money and the friendship factor, respectively.

Output

Print the maximum total friendship factir that can be reached.

Examples

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.

Code:

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 static const int MAXN = 1e5 + 10;
 4 typedef long long LL;
 5 struct Node
 6 {
 7     LL m , s;
 8 }data[MAXN];
 9 int n;
10 LL d;
11 LL pre[MAXN];
12 int Find(int l , int r , LL up)
13 {
14     int ret = l;
15     while(l <= r)
16     {
17         int mid = l + r >> 1;
18         if(data[mid].m < up)//注意不包含up
19         {
20             l = mid + 1;
21             ret = mid;
22         }
23         else
24             r = mid - 1;
25     }
26     return ret;
27 }
28 int main()
29 {
30     scanf("%d%I64d" , &n , &d);
31     for(int i = 0 ; i < n ; ++i)
32     {
33         scanf("%I64d%I64d" , &data[i].m , &data[i].s);
34     }
35     sort(data , data + n , [](Node a , Node b){return a.m == b.m ? a.s > b.s : a.m < b.m;});
36     pre[0] = data[0].s;
37     for(int i = 1 ; i < n ; ++i)
38     {
39         pre[i] = pre[i - 1] + data[i].s;
40     }
41     LL ans = 0;
42     for(int i = 0 ; i < n ; ++i)
43     {
44         int j = Find(i , n - 1 , data[i].m + d);
45         ans = max(ans , pre[j] - pre[i - 1]);
46     }
47     printf("%I64d" , ans);
48 }

C. Kefa and Park(DFS)

Kefa decided to celebrate his first big salary by going to the restaurant.

He lives by an unusual park. The park is a rooted tree consisting of n vertices with the root at vertex 1. Vertex 1 also contains Kefa‘s house. Unfortunaely for our hero, the park also contains cats. Kefa has already found out what are the vertices with cats in them.

The leaf vertices of the park contain restaurants. Kefa wants to choose a restaurant where he will go, but unfortunately he is very afraid of cats, so there is no way he will go to the restaurant if the path from the restaurant to his house contains more than m consecutivevertices with cats.

Your task is to help Kefa count the number of restaurants where he can go.

Input

The first line contains two integers, n and m (2 ≤ n ≤ 105, 1 ≤ m ≤ n) — the number of vertices of the tree and the maximum number of consecutive vertices with cats that is still ok for Kefa.

The second line contains n integers a1, a2, ..., a**n, where each a**i either equals to 0 (then vertex i has no cat), or equals to 1 (then vertex i has a cat).

Next n - 1 lines contains the edges of the tree in the format "x**i y**i" (without the quotes) (1 ≤ x**i, y**i ≤ n, x**i ≠ y**i), where x**i and y**i are the vertices of the tree, connected by an edge.

It is guaranteed that the given set of edges specifies a tree.

Output

A single integer — the number of distinct leaves of a tree the path to which from Kefa‘s home contains at most m consecutive vertices with cats.

Examples

input

4 1
1 1 0 0
1 2
1 3
1 4

output

2

input

7 1
1 0 1 1 0 0 0
1 2
1 3
2 4
2 5
3 6
3 7

output

2

Note

Let us remind you that a tree is a connected graph on n vertices and n - 1 edge. A rooted tree is a tree with a special vertex called root. In a rooted tree among any two vertices connected by an edge, one vertex is a parent (the one closer to the root), and the other one is a child. A vertex is called a leaf, if it has no children.

Note to the first sample test:

The vertices containing cats are marked red. The restaurants are at vertices 2, 3, 4. Kefa can‘t go only to the restaurant located at vertex 2.

Note to the second sample test:

The restaurants are located at vertices 4, 5, 6, 7. Kefa can‘t go to restaurants 6, 7.

Code:

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 static const int MAXN = 1e5 + 10;
 4 vector<int> data[MAXN];
 5 bool vis[MAXN];
 6 int leaf[MAXN];
 7 bool iscat[MAXN];
 8 int ans;
 9 int n , m;
10 void Add(int u , int v)
11 {
12     ++leaf[u];++leaf[v];
13     data[u].push_back(v);
14     data[v].push_back(u);
15 }
16 void Dfs(int x , int cat)
17 {
18     if(vis[x])
19         return;
20     else
21         vis[x] = 1;
22     if(iscat[x])
23         ++cat;
24     else
25         cat = 0;
26     if(cat > m)
27         return;
28     if(leaf[x] < 2 && x != 1)
29         ++ans;
30     for(auto v: data[x])
31     {
32         Dfs(v , cat);
33     }
34 }
35 int main()
36 {
37     scanf("%d%d" , &n , &m);
38     for(int i = 1 ; i <= n ; ++i)
39     {
40         scanf("%d" , &iscat[i]);
41     }
42     int x , y;
43     for(int i = 1 ; i < n ; ++i)
44     {
45         scanf("%d%d" , &x , &y);
46         Add(x , y);
47     }
48     Dfs(1 , 0);
49     printf("%d" , ans);
50 }

时间: 2024-11-08 09:06:06

2017-5-6-Train:Codeforces Round #321 (Div. 2)的相关文章

codeforces水题100道 第十四题 Codeforces Round #321 (Div. 2) A. Kefa and First Steps (brute force)

题目链接:http://www.codeforces.com/problemset/problem/580/A题意:求最长连续非降子序列的长度.C++代码: #include <iostream> using namespace std; const int maxn = 100100; int n, a[maxn], tmp, ans; int main() { cin >> n; for (int i = 0; i < n; i ++) cin >> a[i]

Codeforces Round #321 (Div. 2) D. Kefa and Dishes (状压dp,再A一发)

题意: 有n个菜,需要m个菜,k个规则  每个菜吃下去的满足感为 a1......an 每个规则: 吃完第u个菜后吃第v个菜满足感+c; 问:怎么吃才能幸福感最大? dp[1<<18][20]:一维表示吃了的菜(1表示吃了,0表吃没吃),第二维表示最后一个吃的菜 dp的初始化: dp[1<<i][i] = saty[i]; 状态转移:dp[i|(1 << k)][k] = max(dp[i][j] + rule[j][k] + safy[k],dp[i|(1 <&

CodeForces Round 321 div 2

最近做的div2,很水的一次. 1)求最长不下降子序列.听说还要dp?这里的不下降子序列必须要求连续...所以水过 #include <cstdio> #include <cstring> #include <cstdlib> #include <cmath> #include <iostream> #include <string> #include <queue> #include <vector> #in

Codeforces Round #321 (Div. 2) A, B, C, D, E

580A. Kefa and First Steps 题目链接: A. Kefa and First Steps 题意描述: 给出一个序列,求最长公共子序列多长? 解题思路: 水题,签到 代码: 1 #include <cstdio> 2 #include <cstring> 3 #include <iostream> 4 #include <algorithm> 5 using namespace std; 6 7 int main () 8 { 9 in

Codeforces Round #321 (Div. 2) Kefa and Park 深搜

原题链接: 题意: 给你一棵有根树,某些节点的权值是1,其他的是0,问你从根到叶子节点的权值和不超过m的路径有多少条. 题解: 直接dfs一下就好了. 代码: #include<iostream> #include<cstring> #include<vector> #include<algorithm> #define MAX_N 100005 using namespace std; vector<int> G[MAX_N]; int n,m

Codeforces Round #321 (Div. 2) D Kefa and Dishes

用spfa,和dp是一样的.转移只和最后一个吃的dish和吃了哪些有关. 把松弛改成变长.因为是DAG,所以一定没环.状态最多有84934656,514ms跑过,cf机子就是快. #include<bits/stdc++.h> using namespace std; typedef pair<int,int> nd; typedef long long ll; #define fi first #define se second int n,m,a[18]; int g[18][

Codeforces Round #321 (Div. 2) C Kefa and Park

dfs一遍,维护当前连续遇到的喵的数量,然后剪枝,每个统计孩子数量判断是不是叶子结点. #include<bits/stdc++.h> using namespace std; const int maxn = 2e5+5; int a[maxn]; int head[maxn],nxt[maxn<<1],to[maxn<<1],ect; inline void addEdge(int u,int v) { to[ect] = v; nxt[ect] = head[u]

Codeforces Round #321 (Div. 2) B. Kefa and Company

排序以后O(n)枚举尾部.头部单调,维护一下就好. #include<bits/stdc++.h> using namespace std; typedef long long ll; //#define LOCAL const int maxn = 1e5+5; struct Node { int m,s; void IN(){ scanf("%d%d",&m,&s); } bool operator < (const Node&rh) co

Codeforces Round #321 (Div. 2) E - Kefa and Watch

题目大意:给你一个由0-9组成的字符串,有m个询问,两种操作,第一种将l到r的字符全部变成c,第二种问l到r这段 字符串的循环节是不是d. 思路:首先我们要知道怎么判断字符串的循环节的长度是不是d,如果这个字符串小于等于d,那么肯定是的,否则,如果l 到 r-d 和l+d 到 r 这两段字符串则循环节的长度是d,反之不是. 然后我们就用线段树维护区间字符串哈希值就好啦. #include<bits/stdc++.h> #define read(x) scanf("%d",&