Codeforces Round #321 (Div. 2)

A. Kefa and First Steps
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

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.

题意:求最长连续不降子系列长度。

水题,暴力即可

#include<iostream>
using namespace std;
const int MAXN = 100005;
int num[MAXN];
int main()
{
    int max = 0,n,temp=1;
    cin >> n;
    for (int i = 0; i < n; i++)
    {
        cin >> num[i];
    }
    num[n] = 0;
    for (int i = 0; i < n; i++)
    {
        if (num[i] <= num[i + 1])
        {
            temp++;
        }
        else
        {
            max = max>temp ? max : temp;
            temp = 1;
        }
    }
    cout << max << endl;
    return 0;
}

B. Kefa and Company
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

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.


题意:有n个朋友,最大钱差d,然后n行,每行分别是每个人拥有的财富值,和好友度。现在要求选一部分人使得这些人之间的财富值差不超过d,求这部分人的总好友度最多是多少。

思路:排序+二分     按照财富值排序,然后二分找到不大于"当前这位朋友的财富值+d"的最大位置,也可以预处理先求出前缀和。

#include<iostream>
#include<algorithm>
using namespace std;
const int MAXN = 100005;
struct Node
{
	int m;
	int s;
};
long long int sum[MAXN];
Node peo[MAXN];
bool cmp(Node a, Node b)
{
	return a.m < b.m;
}
int bSearch(int begin, int end, int e)
{
	int mid, left = begin, right = end;
	while (left <= right)
	{
		mid = (left + right) >> 1;
		if (peo[mid].m > e)
		{
			right = mid - 1;
		}
		else
		{
			left = mid + 1;
		}
	}
	return right;
}
int main()
{
	int n, d;
	long long int temp = 0, max = 0;
	cin >> n >> d;
	for (int i = 0; i < n; i++)
	{
		cin >> peo[i].m >> peo[i].s;
	}
	sort(peo, peo + n, cmp);//按财富值排序
	for (int i = 0; i < n; i++)//预处理求前缀和
	{
		temp += peo[i].s;
		sum[i] = temp;
	}
	for (int i = 0; i < n; i++)
	{
		int ans = bSearch(0, n - 1, peo[i].m + d);
		//找到不大于"当前这位朋友的财富+d"的最大位置
		if (peo[ans].m == peo[i].m + d)//等于的情况
		{
			while (peo[ans].m == peo[i].m + d)//因为不能超过d,
			{                               //所以等于的情况也要排除
				ans--;
			}
			temp = sum[ans] - sum[i] + peo[i].s;
		}
		else
		{
			temp = sum[ans] - sum[i] + peo[i].s;
		}
		max = max > temp ? max : temp;//更新最大值
		if (ans == n - 1)//剪枝:如果当前二分查找已经到了最后的位置,
		{               //那么以后的查找都会到最后位置。
			break;
		}
	}
	cout << max << endl;
	return 0;
}

C. Kefa and Park
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

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 consecutive vertices 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 ≤ 1051 ≤ 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, ..., an, where each ai 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 "xi yi" (without the quotes) (1 ≤ xi, yi ≤ nxi ≠ yi), wherexi and yi 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 mconsecutive vertices with cats.

Sample test(s)
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.

题意:有n个结点,餐馆在叶子结点中,kefa结点1里,有些结点有猫存在,问有多少个子叶结点能满足从子叶结点到根结点的路径上,不会有超过有连续的m只猫。输入保证能构成一棵树。

思路:DFS,注意边是无向的,边两端的结点不一定按顺序得给出。

#include<iostream>
#include<vector>
#include<cstring>
using namespace std;
const int MAXN = 100005;
vector<int>map[MAXN];
int n, m, has[MAXN], vis[MAXN], ans;
void dfs(int start, int cat)
{
	if (vis[start])
	{
		return;
	}
	vis[start] = 1;
	if (has[start])
	{
		cat++;
	}
	else
	{
		cat = 0;
	}
	if (cat > m)
	{
		return;
	}
	if (start != 1 && map[start].size() == 1)
	{//结点为子叶结点当且仅当该结点不是根结点并且结点只要一条边
	//此边为链接子叶结点和子叶结点的父亲结点的边
		ans++;
	}
	for (int i = 0; i < map[start].size(); i++)
	{
		int temp = map[start][i];
		if (!vis[temp])
		{
			dfs(temp, cat);
		}
	}
}
int main()
{
	ans = 0;
	cin >> n >> m;
	memset(vis, 0, sizeof(vis));
	for (int i = 1; i <= n; i++)
	{
		cin >> has[i];
	}
	for (int i = 1; i < n; i++)
	{
		int u, v;
		cin >> u >> v;
		map[u].push_back(v);
		map[v].push_back(u);
	}
	dfs(1, 0);
	cout << ans << endl;
	return 0;
}



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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值