Codeforces 892 - A/B/C
题目链接:https://cn.vjudge.net/problem/CodeForces-892A
Jafar has n cans of cola. Each can is described by two integers: remaining volume of cola ai and can's capacity bi (ai ≤ bi).
Jafar has decided to pour all remaining cola into just 2 cans, determine if he can do this or not!
Input
The first line of the input contains one integer n (2 ≤ n ≤ 100 000) — number of cola cans.
The second line contains n space-separated integers a1, a2, ..., an (0 ≤ ai ≤ 109) — volume of remaining cola in cans.
The third line contains n space-separated integers that b1, b2, ..., bn (ai ≤ bi ≤ 109) — capacities of the cans.
Output
Print "YES" (without quotes) if it is possible to pour all remaining cola in 2 cans. Otherwise print "NO" (without quotes).
You can print each letter in any case (upper or lower).
Example
2
3 5
3 6
YES
3
6 8 9
6 10 12
NO
5
0 0 5 0 0
1 1 8 10 5
YES
4
4 1 0 3
5 2 2 3
YES
贼JR的水,根本不需要题解……
只是想贴一个记录一下O(n)得到数组内最大和次大的for循环代码,免得以后什么时候脑抽忘记了下不出来僵掉了……
#include<bits/stdc++.h> using namespace std; typedef long long LL; int n; int main() { scanf("%d",&n); LL sum=0; for(int i=1,tmp;i<=n;i++) { scanf("%d",&tmp); sum+=tmp; } LL max1=-1,max2=-2; for(int i=1,tmp;i<=n;i++) { scanf("%d",&tmp); if(max2<=max1 && max1<=tmp) max2=max1,max1=tmp; else if(max2<tmp && tmp<=max1) max2=tmp; } if(max1+max2 >= sum) cout<<"YES"<<endl; else cout<<"NO"<<endl; }
————————————————————————————————————————————————————————————————
题目链接:https://cn.vjudge.net/problem/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 Li. 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 - Li.
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 L1, L2, ..., Ln (0 ≤ Li ≤ 109), where Li is the length of the i-th person's claw.
Output
Print one integer — the total number of alive people after the bell rings.
Example
4
0 1 0 10
1
2
0 0
2
10
1 1 3 0 0 0 2 1 0 3
3
题意:
对于一排人,第i个人有长度为L[i]的爪子,他会杀死所有第j个人,其中j满足 i - L[i] <= j 且 j < i ;
求最后存活下来的有几个人;
题解:
一看n的范围,最大到1e6,看来只能用O(n)的做法;
那么,考虑从后往前遍历,对于第i个人会不会被杀掉:
我们用mini来维护所有 i+1 ~ n 的人会杀掉的最前面的人是哪个,也就是所有 i+1 ~ n 的人的 i - L[i] 最小值;
那么,如果i >= mini,那么第i个人会被干掉;
这样一来,就能O(n)的得到被杀的人数了;
AC代码:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
#include<bits/stdc++.h> using namespace std; const int INF = 0x3f3f3f3f; int n,L[1000000+5]; int main() { scanf("%d",&n); for(int i=1;i<=n;i++) scanf("%d",&L[i]); int mini=INF,cnt=0; for(int i=n;i>=1;i--) { if(i>=mini) cnt++; mini=min(mini,i-L[i]); } cout<<n-cnt<<endl; }
————————————————————————————————————————————————————————————————
题目链接:https://cn.vjudge.net/problem/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 a1, a2, ..., an (1 ≤ ai ≤ 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.
Example
5
2 2 3 4 6
5
4
2 4 6 8
-1
3
2 6 9
4
Note
In the first sample you can turn all numbers to 1 using the following 5 moves:
- [2, 2, 3, 4, 6].
- [2, 1, 3, 4, 6]
- [2, 1, 3, 1, 6]
- [2, 1, 1, 1, 6]
- [1, 1, 1, 1, 6]
- [1, 1, 1, 1, 1]
We can prove that in this case it is not possible to make all numbers one using less than 5 moves.
题意:
题解:
AC代码:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
#include<bits/stdc++.h> using namespace std; const int INF = 0x3f3f3f3f; int n; vector<int> a; int gcd(int a,int b){return b==0?a:gcd(b,a%b);} int main() { scanf("%d",&n); int cnt_1=0; for(int i=1,tmp;i<=n;i++) { scanf("%d",&tmp); a.push_back(tmp); if(tmp==1) cnt_1++; } if(cnt_1>0) { cout<<n-cnt_1<<endl; return 0; } if(n==1 && a[0]!=1) { cout<<"-1"<<endl; return 0; } int cnt=0,ok=0; vector<int> tmp; while(1) { cnt++; tmp.clear(); for(int i=0;i<a.size()-1;i++) { if(gcd(a[i],a[i+1])==1) { ok=1; break; } tmp.push_back(gcd(a[i],a[i+1])); } if(ok) { cout<<cnt+n-1<<endl; return 0; } if(tmp.size()==1 && tmp[0]!=1) { cout<<"-1"<<endl; return 0; } a.clear(); for(int i=0;i<tmp.size();i++) a.push_back(tmp[i]); } }