B - Wrath CodeForces - 892B
Hands that shed innocent blood!
There are n guilty people in a line, the i-th of them holds a claw with length L i. The bell rings and every person kills some of people in front of him. All people kill others at the same time. Namely, the i-th person kills the j-th person if and only if j < i and j ≥ i - L i.
You are given lengths of the claws. You need to find the total number of alive people after the bell rings.
Input
The first line contains one integer n (1 ≤ n ≤ 106) — the number of guilty people.
Second line contains n space-separated integers L 1, L 2, …, L n (0 ≤ L i ≤ 109), where L i is the length of the i-th person’s claw.
Output
Print one integer — the total number of alive people after the bell rings.
Examples
InputCopy
4
0 1 0 10
Output
1
Input
2
0 0
Output
2
Input
10
1 1 3 0 0 0 2 1 0 3
Output
3
Note
In first sample the last person kills everyone in front of him.
题意:一群人排着队,每个人有把长短不一的长剑,每个人向前面的人(左边)出剑,在其攻击范围内的人就会死。求存活的人数。
思路:在最右边的人一定会存活,其它在攻击范围外的人也会存活。从右往左遍历,用个标记记录目前的最远的攻击范围,每遍历到在攻击范围外的人,存活数+1.(做题的时候记录的是死亡数,重复记了死亡数wa了好多次。)
#include
using namespace std;
const int N = 1e6+7;
int arr[N];
int main(){
int n,x,sum;
while(~scanf("%d",&n)){
for(int i = 1;i <= n;i ++){
scanf("%d",&arr[i]);
}
sum = 1;
x = n;
for(int i = n;i >= 1;i --){
int t = i-arr[i];
if(x > i){
sum ++;
}
if(t <= 0)
t = 1;
if(t < x)
x = t;
}
printf("%d\n",sum);
}
}
C - Pride CodeForces - 892C
You have an array a with length n, you can perform operations. Each operation is like this: choose two adjacent elements from a, say x and y, and replace one of them with gcd(x, y), where gcd denotes the greatest common divisor.
What is the minimum number of operations you need to make all of the elements equal to 1?
Input
The first line of the input contains one integer n (1 ≤ n ≤ 2000) — the number of elements in the array.
The second line contains n space separated integers a 1, a 2, …, a n (1 ≤ a i ≤ 109) — the elements of the array.
Output
Print -1, if it is impossible to turn all numbers to 1. Otherwise, print the minimum number of operations needed to make all numbers equal to 1.
Examples
Input
5
2 2 3 4 6
Output
5
Input
4
2 4 6 8
Output
-1
Input
3
2 6 9
Output
4
题意:给定一串数,可以将一个数置为它与相邻数的最大公约数。问是否能通过有限次操作将数字都变为1,如果能,输出最小操作数。
思路:如果这串数中有1,那操作数为总数减去1的数量。没有1的话,查找将一个数置为一的最少操作数,加上剩余的数便是最小操作数。如果不能将任何一个数置为一,那就输出-1.
#include
#include
using namespace std;
const int inf = 1e9;
int gcd(int a,int b) {
if(b == 0)return a;
else return gcd(b,a%b);
}
int main() {
int n,arr[2010];
while(~scanf("%d",&n)) {
int x = 0;
for(int i = 0; i < n; i ++) {
scanf("%d",&arr[i]);
if(arr[i] == 1) x ++;
}
if(x)
printf("%d\n",n-x);
else {
int MAXN = inf;
for(int i = 0;i < n-1;i ++){
int a = arr[i];
int count = 0;
for(int j = i+1;j < n;j ++){
count ++;
a = gcd(a,arr[j]);
if(a == 1)
break;
}
if(a == 1)
MAXN = min(MAXN,count);
}
if(MAXN == inf) printf("-1\n");
else printf("%d\n",MAXN+n-1);
}
}
}
E - Months and Years CodeForces - 899B
Everybody in Russia uses Gregorian calendar. In this calendar there are 31 days in January, 28 or 29 days in February (depending on whether the year is leap or not), 31 days in March, 30 days in April, 31 days in May, 30 in June, 31 in July, 31 in August, 30 in September, 31 in October, 30 in November, 31 in December.
A year is leap in one of two cases: either its number is divisible by 4, but not divisible by 100, or is divisible by 400. For example, the following years are leap: 2000, 2004, but years 1900 and 2018 are not leap.
In this problem you are given n (1 ≤ n ≤ 24) integers a 1, a 2, …, a n, and you have to check if these integers could be durations in days of n consecutive months, according to Gregorian calendar. Note that these months could belong to several consecutive years. In other words, check if there is a month in some year, such that its duration is a 1 days, duration of the next month is a 2 days, and so on.
Input
The first line contains single integer n (1 ≤ n ≤ 24) — the number of integers.
The second line contains n integers a 1, a 2, …, a n (28 ≤ a i ≤ 31) — the numbers you are to check.
Output
If there are several consecutive months that fit the sequence, print “YES” (without quotes). Otherwise, print “NO” (without quotes).
You can print each letter in arbitrary case (small or large).
Examples
Input
4
31 31 30 31
Output
Yes
InputCopy
2
30 30
Output
No
Input
5
29 31 30 31 30
Output
Yes
Input
3
31 28 30
Output
No
Input
3
31 31 28
Output
Yes
题意:判断所给的几个月的天数是否是连续的月份。
思路:暴力打表。(所给的月份天数最多有24个,所以要列出连续的5个月的天数,因为这个wa了)
#include
using namespace std;
int main() {
int n,m[60] = {
31,28,31,30,31,30,31,31,30,31,30,31,
31,28,31,30,31,30,31,31,30,31,30,31,
31,29,31,30,31,30,31,31,30,31,30,31,
31,28,31,30,31,30,31,31,30,31,30,31,
31,28,31,30,31,30,31,31,30,31,30,31
},x[24];
while(~scanf("%d",&n)) {
int xx = 0;
for(int i = 0; i < n; i ++) {
scanf("%d",&x[i]);
}
for(int i = 0; i <= 60 - n; i ++) {
int k = i,j = 0;
if(x[j] == m[k]) {
int flag = 1;
while(m[k+1] == x[j+1]) {
k ++;
j ++;
flag ++;
if(j+1 >= n)
break;
}
if(flag >= n) {
xx = 1;
break;
}
}
}
if(xx == 1) {
printf("Yes\n");
} else
printf("No\n");
}
}