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.
The only line contains three integers l, r 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.
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.
1 4 2
6
5 5 5
14
0 2 0
0
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;
}
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.
The first line contains two integers n, m (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 + ... + ym. Also, it is guaranteed that x1 + ... + xn ≤ 106.
Print the maximum number of files the intercepted array could consist of.
7 6 2 5 3 1 11 4 4 7 8 2 4 1 8
3
3 3 1 10 100 1 100 10
2
1 4 4 1 1 1 1
1
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 = 7, 15 = 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 1, 10 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;
}
题意:有一个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;
}
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.
The first line contains two integers n and q (1 ≤ n ≤ 1018, 1 ≤ 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.
For each of q queries output one integer number, the value that will appear in the corresponding array cell after Dima's algorithm finishes.
4 3 2 3 4
3 2 4
13 4 10 5 4 8
13 3 8 9
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;
}