第一次CF涨分啊,真**不容易。
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!
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.
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).
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
In the first sample, there are already 2 cans, so the answer is "YES".
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<queue>
#include<set>
#include<cstdio>
#define ll long long
#define mset(a,x) memset(a,x,sizeof(a))
using namespace std;
const double PI=acos(-1);
const int inf=0x3f3f3f3f;
const double esp=1e-6;
const int maxn=1e6+5;
const int mod=1e9+7;
int dir[4][2]={0,1,1,0,0,-1,-1,0};
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
ll lcm(ll a,ll b){return a/gcd(a,b)*b;}
ll inv(ll b){if(b==1)return 1; return (mod-mod/b)*inv(mod%b)%mod;}
ll fpow(ll n,ll k){ll r=1;for(;k;k>>=1){if(k&1)r=r*n%mod;n=n*n%mod;}return r;}
struct node{
int x;
int y;
int c;
}p[100005];
int a[100005],b[100005];
int main()
{
int n,i,j,k;
while(cin>>n)
{
mset(p,0);
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
for(i=0;i<n;i++)
{
scanf("%d",&b[i]);
}
long long ans=0;
for(i=0;i<n;i++)
ans+=a[i];
sort(b,b+n);
ll sum=b[n-1]+b[n-2];
if(sum>=ans)
cout<<"YES"<<endl;
else
cout<<"NO"<<endl;
}
return 0;
}
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.
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.
Print one integer — the total number of alive people after the bell rings.
4 0 1 0 10
1
2 0 0
2
10 1 1 3 0 0 0 2 1 0 3
3
In first sample the last person kills everyone in front of him.
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#define ll long long
using namespace std;
const int INF=0x3f3f3f3f;
const long long MAX=1<<22;
struct tree{
int n;
int a[MAX],Max[MAX],Min[MAX]; //Max维护最大值 ,Min最小
void init(int N)
{
n=N;
for(int i=0;i<=N;i++)
Max[i]=-(Min[i]=INF);
}
void update(int x,int num)//单点更新
{
a[x]=num;
while(x<=n)
{
Min[x]=Max[x]=a[x];
int lx=x&-x;
for(int i=1;i<lx;i<<=1)
{
Max[x]=max(Max[x],Max[x-i]);
Min[x]=min(Min[x],Min[x-i]);
}
x+=x&-x;
}
}
int Qmax(int x,int y)//[x,y]最大值
{
int ans=-INF;
while(y>=x)
{
ans=max(a[y], ans);
y--;
for(;y-(y&-y)>=x;y-=(y&-y))
ans=max(Max[y],ans);
}
return ans;
}
int Qmin(int x, int y)
{
int ans=INF;
while(y>=x)
{
ans=min(a[y],ans);
y--;
for(;y-(y&-y)>= x; y-=(y&-y) )
ans=min(Min[y],ans);
}
return ans;
}
}C;
int a[1000005];
int main()
{
int q,i,n,j;
while (scanf("%d",&n) != EOF)
{
C.init(n);
for (i = 1; i<=n; i++)
{
scanf("%d",&a[i]);
C.update(i,max(i-a[i],1));
}
int ans=1;
for(i=n-1;i>=1;i--)
{
if(i<C.Qmin(i+1,n))
ans++;
}
cout<<ans<<endl;
}
return 0;
}
You have an array a with length n, you can perform operations. Each operation is like this: choose two adjacent elements from a, say xand 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?
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.
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.
5 2 2 3 4 6
5
4 2 4 6 8
-1
3 2 6 9
4
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.
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<queue>
#include<set>
#include<cstdio>
#define ll long long
#define mset(a,x) memset(a,x,sizeof(a))
using namespace std;
const double PI=acos(-1);
const int inf=0x3f3f3f3f;
const double esp=1e-6;
const int maxn=1e6+5;
const int mod=1e9+7;
int dir[4][2]={0,1,1,0,0,-1,-1,0};
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
ll lcm(ll a,ll b){return a/gcd(a,b)*b;}
ll inv(ll b){if(b==1)return 1; return (mod-mod/b)*inv(mod%b)%mod;}
ll fpow(ll n,ll k){ll r=1;for(;k;k>>=1){if(k&1)r=r*n%mod;n=n*n%mod;}return r;}
int map[maxn];
int main()
{
int i,j,k,n;
while(cin>>n)
{
int sum=0;
for(i=0;i<n;i++)
{
cin>>map[i];
if(map[i]==1)
sum++;
}
if(sum)
{
cout<<n-sum<<endl;
continue;
}
int minn=n+1;
for(i=0;i<n;i++)
{
int temp=map[i];
for(j=i+1;j<n;j++)
{
temp=gcd(temp,map[j]);
if(temp==1)
break;
}
if(j!=n)
{
minn=min(minn,j-i);
}
}
if(minn==n+1)
cout<<"-1"<<endl;
else
cout<<minn+n-1<<endl;
}
return 0;
}