Educational Codeforces Round 42 (Rated for Div. 2)

A. Equator
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Polycarp has created his own training plan to prepare for the programming contests. He will train for n

days, all days are numbered from 1 to n

, beginning from the first.

On the i

-th day Polycarp will necessarily solve ai

problems. One evening Polycarp plans to celebrate the equator. He will celebrate it on the first evening of such a day that from the beginning of the training and to this day inclusive he will solve half or more of all the problems.

Determine the index of day when Polycarp will celebrate the equator.

Input

The first line contains a single integer n

( 1n200000

) — the number of days to prepare for the programming contests.

The second line contains a sequence a1,a2,,an

( 1ai10000), where ai equals to the number of problems, which Polycarp will solve on the i

-th day.

Output

Print the index of the day when Polycarp will celebrate the equator.

Examples
Input
Copy
4
1 3 2 1
Output
Copy
2
Input
Copy
6
2 2 2 2 2 2
Output
Copy
3
Note

In the first example Polycarp will celebrate the equator on the evening of the second day, because up to this day (inclusive) he will solve 4

out of 7

scheduled problems on four days of the training.

In the second example Polycarp will celebrate the equator on the evening of the third day, because up to this day (inclusive) he will solve 6

out of 12

scheduled problems on six days of the training.

题意:输入n个数,问从1-n,第几个前缀和大于等于总n的一半,输出i。

#include<stdio.h>
const int maxn = 200010;
int vis[maxn];

int main()
{
	int n,sum;
	while(scanf("%d",&n)!=EOF)
	{
		for(int i = 0; i < n; i ++)
			scanf("%d",&vis[i]);
		for(int i = 1; i < n; i ++)
			vis[i] += vis[i-1];
		for(int i = 0; i < n; i ++)
		{
			if(vis[i]*1.0 >= (vis[n-1]*1.0/2))
			{
				printf("%d\n",i+1);
				break;
			}
		}
	}
	return 0;
}


B. Students in Railway Carriage
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

There are n

consecutive seat places in a railway carriage. Each place is either empty or occupied by a passenger.

The university team for the Olympiad consists of a

student-programmers and b student-athletes. Determine the largest number of students from all a+b

students, which you can put in the railway carriage so that:

  • no student-programmer is sitting next to the student-programmer;
  • and no student-athlete is sitting next to the student-athlete.

In the other words, there should not be two consecutive (adjacent) places where two student-athletes or two student-programmers are sitting.

Consider that initially occupied seat places are occupied by jury members (who obviously are not students at all).

Input

The first line contain three integers n

, a and b ( 1n2105, 0a,b2105, a+b>0

) — total number of seat places in the railway carriage, the number of student-programmers and the number of student-athletes.

The second line contains a string with length n

, consisting of characters "." and "*". The dot means that the corresponding place is empty. The asterisk means that the corresponding place is occupied by the jury member.

Output

Print the largest number of students, which you can put in the railway carriage so that no student-programmer is sitting next to a student-programmer and no student-athlete is sitting next to a student-athlete.

Examples
Input
Copy
5 1 1
*...*
Output
Copy
2
Input
Copy
6 2 3
*...*.
Output
Copy
4
Input
Copy
11 3 10
.*....**.*.
Output
Copy
7
Input
Copy
3 2 3
***
Output
Copy
0
Note

In the first example you can put all student, for example, in the following way: *.AB*

In the second example you can put four students, for example, in the following way: *BAB*B

In the third example you can put seven students, for example, in the following way: B*ABAB**A*B

The letter A means a student-programmer, and the letter B — student-athlete.

题意:输入n,a,b,再输入长为n的串,'*'表示座位已占,'.'表示空座位,问最多能坐下多少个人,要求不能出现连续的a或者b。

思路:据说是贪心,but,我居然给把所有情况给列出来了,也就是分为以下三种情况:*.*和*..*和*...*以及以上,再讨论a人数和b人数的大小剩余情况,具体见注释,原本以为这个脑洞不可能过,结果还给过了。

#include<stdio.h>
#include<algorithm>
#include<string.h>
using namespace std;
const int maxn = 200000;
char str[maxn];

int main()
{
	int n,a,b;
	int flag,sum;
	while(scanf("%d%d%d",&n,&a,&b)!=EOF)
	{
		int x = a,y = b;
		scanf("%s",str);
		flag = 0;
		for(int i = 0; i < n; i ++)
		{
			
			if(str[i] == '.')
				flag ++;//记录连续的"."的数目 
			if(str[i] == '*'||(i == n-1&&str[i] =='.'))
			{
				if(flag == 1)//情况1:*.* 
				{
					if( x >= y&& x > 0)//a人数足够 
						x -= 1;
					else if( y > x&&y > 0)//b人数足够 
						y -= 1;
				}
				else if(flag == 2)//情况2:*..* 
				{
					if(x > 0)
						x -= 1;
					if(y > 0)
						y -= 1;
				}
				else if(flag > 2)//情况3:*...*及其以上 
				{
					int number = flag/2;
					if( x >= y)
					{
						if(x >= flag-number)
						{
							x -= flag-number;
							if(y >= number)
								y -= number;
							else
								y = 0;
						}
						else
						{
							x = 0;
							if(y >= number)
								y -= number;
							else
								y = 0;
						}
					}	
					else if(y > x)
					{
						if(y >= flag-number)
						{
							y -= flag-number;
							if(x >= number)
								x -= number;
							else
								x = 0;
						}
						else
						{
							y =  0;
							if(x >= number)
								x -= number;
							else
								x = 0;
						}
					}
				}
				if( x == 0&&y == 0)//当a人数和b人数都没有剩余的时候,直接跳出循环 
					break;
				flag = 0;
			}
		}
		printf("%d\n",a+b-x-y);
	}
	return 0;
}

C. Make a Square
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

You are given a positive integer n

, written without leading zeroes (for example, the number 04 is incorrect).

In one operation you can delete any digit of the given integer so that the result remains a positive integer without leading zeros.

Determine the minimum number of operations that you need to consistently apply to the given integer n

to make from it the square of some positive integer or report that it is impossible.

An integer x

is the square of some positive integer if and only if x=y2 for some positive integer y

.

Input

The first line contains a single integer n

( 1n2109

). The number is given without leading zeroes.

Output

If it is impossible to make the square of some positive integer from n

, print -1. In the other case, print the minimal number of operations required to do it.

Examples
Input
Copy
8314
Output
Copy
2
Input
Copy
625
Output
Copy
0
Input
Copy
333
Output
Copy
-1
Note

In the first example we should delete from 8314

the digits 3 and 4. After that 8314 become equals to 81, which is the square of the integer 9

.

In the second example the given 625

is the square of the integer 25

, so you should not delete anything.

In the third example it is impossible to make the square from 333

, so the answer is -1.

题意:输入数n, 判断最少去掉多少个数能够使得剩下的数是一个数的平方,有前缀0是不被允许滴。

思路:数据是10亿,所以我们将10万以内的数字的平方存入数组,输入10亿以内的的任何一个数n时,枚举1-10万的平方数,判断n上最多有多少位数和平方数相等,用n原本的位数-最多的匹配位数=最小删去数。

这道题好尬啊,前缀0并没有什么用居然还写上去,幸好胆子大试了一下。

#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
#define inf 0x3f3f3f3f
const int maxn = 100010;
int vis1[maxn],vis2[maxn],num[maxn];
int main()
{
	int n,m,sum,flag;
	int p,q,ans;
	while(scanf("%d",&n)!=EOF)
	{
		for(int i = 1;i <= 100000; i ++)
			num[i] = i*i;
		m = n;
		p = 0;
		while(m)
		{
			vis1[p++] = m%10;
			m /= 10;
		}
		ans = inf;
		for(int i = 1; i <= 100000; i ++)
		{
			int number = num[i];
			q = 0;
			while(number)
			{
				vis2[q++] = number%10;
				number /= 10;
			}
			if(num[i]>n)
				break;
			int j,k;
			sum = 0;
			j= k = 0;
			while(j<p&&k < q)
			{
				if(vis1[j] == vis2[k])
				{
					j ++;
					k ++;
					sum ++;
				}
				else
					j ++;
			}
			if(q!=0&&sum == q)
				ans = min(ans,p-q);
		}
		if(ans == inf)
			printf("-1\n");
		else
			printf("%d\n",ans);
	}
	return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值