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 }