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

A. Left-handers, Right-handers and Ambidexters
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

You are at a water bowling training. There are l people who play with their left hand, r people, who play with their right hand, and aambidexters, who can play with left or right hand.

The coach decided to form a team of even number of players, exactly half of the players should play with their right hand, and exactly half of the players should play with their left hand. One player should use only on of his hands.

Ambidexters play as well with their right hand as with their left hand. In the team, an ambidexter can play with their left hand, or with their right hand.

Please find the maximum possible size of the team, where equal number of players use their left and right hands, respectively.

Input

The only line contains three integers lr and a (0 ≤ l, r, a ≤ 100) — the number of left-handers, the number of right-handers and the number of ambidexters at the training.

Output

Print a single even integer — the maximum number of players in the team. It is possible that the team can only have zero number of players.

Examples
input
Copy
1 4 2
output
6
input
Copy
5 5 5
output
14
input
Copy
0 2 0
output
0
Note

In the first example you can form a team of 6 players. You should take the only left-hander and two ambidexters to play with left hand, and three right-handers to play with right hand. The only person left can't be taken into the team.

In the second example you can form a team of 14 people. You have to take all five left-handers, all five right-handers, two ambidexters to play with left hand and two ambidexters to play with right hand.

题意:有三部分人,第一部分只能用左手,第二部分只能用右手,第三部分两只手都可以用,第三部分可以加入到一二两部分中去,把这三部分的人分成两组,要求两组人数相同,问两组加起来的人数最大值

题解:直接对三部分的人数关系分类讨论

代码

#include<cstdio>
#include<math.h>

int max(int x, int y)
{
	if(x >= y)return x;
	else return y;
}

int main()
{
	int l, r, a;
	while(scanf("%d %d %d", &l, &r, &a) != EOF)
	{
		if(l + a <= r)
		{
			printf("%d\n", 2*(l + a));
			continue;
		}
		if(r + a <= l)
		{
			printf("%d\n", 2*(r + a));
			continue;
		}
		int x = fabs(l - r);
		int y = a - x;
		int maxx = max(l, r);
		int ans = 2*maxx;
		if(y % 2 == 0)
		{
			ans = ans + y;
		}
		else
		{
			ans = ans + y - 1;
		}
		printf("%d\n", ans);
	}
	return 0;
}

B. Intercepted Message
time limit per test
1 second
memory limit per test
512 megabytes
input
standard input
output
standard output

Hacker Zhorik wants to decipher two secret messages he intercepted yesterday. Yeah message is a sequence of encrypted blocks, each of them consists of several bytes of information.

Zhorik knows that each of the messages is an archive containing one or more files. Zhorik knows how each of these archives was transferred through the network: if an archive consists of k files of sizes l1, l2, ..., lk bytes, then the i-th file is split to one or more blocks bi, 1, bi, 2, ..., bi, mi (here the total length of the blocks bi, 1 + bi, 2 + ... + bi, mi is equal to the length of the file li), and after that all blocks are transferred through the network, maintaining the order of files in the archive.

Zhorik thinks that the two messages contain the same archive, because their total lengths are equal. However, each file can be split in blocks in different ways in the two messages.

You are given the lengths of blocks in each of the two messages. Help Zhorik to determine what is the maximum number of files could be in the archive, if the Zhorik's assumption is correct.

Input

The first line contains two integers nm (1 ≤ n, m ≤ 105) — the number of blocks in the first and in the second messages.

The second line contains n integers x1, x2, ..., xn (1 ≤ xi ≤ 106) — the length of the blocks that form the first message.

The third line contains m integers y1, y2, ..., ym (1 ≤ yi ≤ 106) — the length of the blocks that form the second message.

It is guaranteed that x1 + ... + xn = y1 + ... + ymAlso, it is guaranteed that x1 + ... + xn ≤ 106.

Output

Print the maximum number of files the intercepted array could consist of.

Examples
input
Copy
7 6
2 5 3 1 11 4 4
7 8 2 4 1 8
output
3
input
Copy
3 3
1 10 100
1 100 10
output
2
input
Copy
1 4
4
1 1 1 1
output
1
Note

In the first example the maximum number of files in the archive is 3. For example, it is possible that in the archive are three files of sizes 2 + 5 = 715 = 3 + 1 + 11 = 8 + 2 + 4 + 1 and 4 + 4 = 8.

In the second example it is possible that the archive contains two files of sizes 1 and 110 = 10 + 100 = 100 + 10. Note that the order of files is kept while transferring archives through the network, so we can't say that there are three files of sizes 110 and 100.

In the third example the only possibility is that the archive contains a single file of size 4.

题意:一种数据加密的方法,区块加密,每个区块加密的内存大小相同,给出两个加密过的序列,加密方法相同,求加密区块的数量

题解:直接暴力一边,分别用两个变量记录两条数据当前区块的内存和,如果两个变量相等,那么区块数++,然后重置这两个变量

代码

#include<cstdio>
#define Max 100010

int a[Max], b[Max];

int max(int x, int y)
{
	if(x >= y)return x;
	else return y;
}

int main()
{
	int n, m;
	while(scanf("%d %d", &n, &m) != EOF)
	{
		for(int i = 0; i < n; i++)
			scanf("%d", &a[i]);
		for(int i = 0; i < m; i++)
			scanf("%d", &b[i]);
			
			
		int ans = 0;
		int sum1 = a[0], sum2 = b[0];
		for(int i = 1, j = 1; i <= n;)
		{
			if(sum1 < sum2)
			{
				sum1 += a[i];
				i++;
			}
			else if(sum1 > sum2)
			{
				sum2 += b[j];
				j++;
			}
			else if(sum1 == sum2)
			{
				ans++;
				sum1 = a[i++];
				sum2 = b[j++];
				/*printf("ans = %d\n", ans);
				printf("sum1 = %d\n", sum1);
				printf("sum2 = %d\n", sum2);*/
			}
		}
		printf("%d\n", ans);
	}
	return 0;
}

C. Zebras
time limit per test
1 second
memory limit per test
512 megabytes
input
standard input
output
standard output

Oleg writes down the history of the days he lived. For each day he decides if it was good or bad. Oleg calls a non-empty sequence of days a zebra, if it starts with a bad day, ends with a bad day, and good and bad days are alternating in it. Let us denote bad days as 0 and good days as 1. Then, for example, sequences of days 001001010 are zebras, while sequences 101100101 are not.

Oleg tells you the story of days he lived in chronological order in form of string consisting of 0 and 1. Now you are interested if it is possible to divide Oleg's life history into several subsequences, each of which is a zebra, and the way it can be done. Each day must belong to exactly one of the subsequences. For each of the subsequences, days forming it must be ordered chronologically. Note that subsequence does not have to be a group of consecutive days.

Input

In the only line of input data there is a non-empty string s consisting of characters 0 and 1, which describes the history of Oleg's life. Its length (denoted as |s|) does not exceed 200 000 characters.

Output

If there is a way to divide history into zebra subsequences, in the first line of output you should print an integer k (1 ≤ k ≤ |s|), the resulting number of subsequences. In the i-th of following k lines first print the integer li (1 ≤ li ≤ |s|), which is the length of the i-th subsequence, and then li indices of days forming the subsequence. Indices must follow in ascending order. Days are numbered starting from 1. Each index from 1 to n must belong to exactly one subsequence. If there is no way to divide day history into zebra subsequences, print -1.

Subsequences may be printed in any order. If there are several solutions, you may print any of them. You do not have to minimize nor maximize the value of k.

Examples
input
Copy
0010100
output
3
3 1 3 4
3 2 5 6
1 7
input
Copy
111
output
-1

题意:有一个01串,问最多能否分成几个子串,字串要求以0开头,以0结尾,且01交替,并且子串的下标要在原串是升序的

题解:贪心+模拟,因为数据量为20w并不大,所以模拟一边O(n),遇到0的时候,尽量把0放在以1结尾的串后面,如果没有,则新生成一个串

代码

#include<cstdio>
#include<vector>
#include<cstring>
#define Max 200010

using namespace std;

char s[Max];
vector<int> v[Max];

int max(int x, int y)
{
	if(x >= y)return x;
	else return y;
}

int main()
{
	while(scanf("%s", s) != EOF)
	{
		int len = strlen(s);
		int pos = 0, ans = 0;
		bool flag = true;
		for(int i = 0; i < len; i++)
		{
			if(s[i] == '0')v[++pos].push_back(i + 1);
			else
			{
				if(!pos)
				{
					flag = false;
					break;
				}
				v[pos--].push_back(i + 1);
			}
			ans = max(ans, pos);
		}
		if(!flag)
		{
			printf("-1\n");
			continue;
		}
		if(pos != ans)
		{
			printf("-1\n");
			continue;
		}
		printf("%d\n", ans);
		for(int i = 1; i <= ans; i++)
		{
			printf("%d", v[i].size());
			for(int j = 0; j < v[i].size(); j++)
				printf(" %d", v[i][j]);
			printf("\n");
		}
	}
	return 0;
}


D. A Leapfrog in the Array
time limit per test
2 seconds
memory limit per test
512 megabytes
input
standard input
output
standard output

Dima is a beginner programmer. During his working process, he regularly has to repeat the following operation again and again: to remove every second element from the array. One day he has been bored with easy solutions of this problem, and he has come up with the following extravagant algorithm.

Let's consider that initially array contains n numbers from 1 to n and the number i is located in the cell with the index 2i - 1 (Indices are numbered starting from one) and other cells of the array are empty. Each step Dima selects a non-empty array cell with the maximum index and moves the number written in it to the nearest empty cell to the left of the selected one. The process continues until all n numbers will appear in the first n cells of the array. For example if n = 4, the array is changing as follows:

You have to write a program that allows you to determine what number will be in the cell with index x (1 ≤ x ≤ n) after Dima's algorithm finishes.

Input

The first line contains two integers n and q (1 ≤ n ≤ 10181 ≤ q ≤ 200 000), the number of elements in the array and the number of queries for which it is needed to find the answer.

Next q lines contain integers xi (1 ≤ xi ≤ n), the indices of cells for which it is necessary to output their content after Dima's algorithm finishes.

Output

For each of q queries output one integer number, the value that will appear in the corresponding array cell after Dima's algorithm finishes.

Examples
input
Copy
4 3
2
3
4
output
3
2
4
input
Copy
13 4
10
5
4
8
output
13
3
8
9
Note

The first example is shown in the picture.

In the second example the final array is [1, 12, 2, 8, 3, 11, 4, 9, 5, 13, 6, 10, 7].


题意:1-n按照2i - 1 的下表分布在数组中,每次都把数组最右边的数移动到这个数左边最近的空的位置,当前n个空都填上数字后,操作结束,然后q次询问,输出询问的位置上的数

题解:因为数是以2i - 1分布的,所以前n个中有n/2个空,这些空应该都有后n/2个数来补满,所以可以得出结论,在前n/2中,奇数位置上的数都是原位置,这个结论模拟一边也可以得出,所以只需考虑偶数位置。瞎找规律,逆推的时候发现了公式当位置为x的时候,它的前一个位置为x +(n - x/2),一直重复这个公式,当x为奇数时,就是原位置

代码

#include<cstdio>
#define ll long long

int main()
{
	ll n;
	int q;
	while(scanf("%I64d %d", &n, &q) != EOF)
	{
		for(int i = 1; i <= q; i++)
		{
			ll x;
			scanf("%lld", &x);
			while(true)
			{
				if(x % 2)break;
				x += n - x / 2;
			}
			printf("%lld\n", x / 2 + 1);
		}
	}
	return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值