Polycarp has created his own training plan to prepare for the programming contests. He will train for n
, beginning from the first.
On the i
-th day Polycarp will necessarily solve aiproblems. 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.
The first line contains a single integer n
) — the number of days to prepare for the programming contests.
The second line contains a sequence a1,a2,…,an
( 1≤ai≤10000), where ai equals to the number of problems, which Polycarp will solve on the i-th day.
Print the index of the day when Polycarp will celebrate the equator.
4 1 3 2 1
2
6 2 2 2 2 2 2
3
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
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 12There 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+bstudents, which you can put in the railway carriage so that:
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).
The first line contain three integers n
) — 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.
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.
5 1 1 *...*
2
6 2 3 *...*.
4
11 3 10 .*....**.*.
7
3 2 3 ***
0
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; }
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.
The first line contains a single integer n
). The number is given without leading zeroes.
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.
8314
2
625
0
333
-1
In the first example we should delete from 8314
.
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; }
本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系我们删除。