Codeforces Round #446 (Div. 2) A-C 题解

来源:互联网 发布:360浏览器下载mac版本 编辑:程序博客网 时间:2024/05/22 02:31

第一次CF涨分啊,真**不容易。

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

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).

Examples
input
23 53 6
output
YES
input
36 8 96 10 12
output
NO
input
50 0 5 0 01 1 8 10 5
output
YES
input
44 1 0 35 2 2 3
output
YES
Note

In the first sample, there are already 2 cans, so the answer is "YES".


大水题,我一开始还在天真的按照剩余空间排序,醉了。
就是所有饮料能不能倒入两个瓶子内,找到两个最大的,判断一下就ok。
代码实现:
#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;elsecout<<"NO"<<endl;}return 0;}

B. Wrath
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

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.

Examples
input
40 1 0 10
output
1
input
20 0
output
2
input
101 1 3 0 0 0 2 1 0 3
output
3
Note

In first sample the last person kills everyone in front of him.


神坑,最后几分钟过的B,迷之WA,最后不得已用了树状数组,单点查询区间最小值才过的,话说,别人都是几行模拟,怀疑人生。。
为确保压入的数非负,可以压入一个0或1作为最小。

代码实现:
#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;  }  

C. Pride
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

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?

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.

Examples
input
52 2 3 4 6
output
5
input
42 4 6 8
output
-1
input
32 6 9
output
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.


本来C题可以搏一搏,单车变摩托。但B题实在费心,最后导致没时间做C题,菜是原罪啊。没什么好说的,就是一个找1的过程,找到1就一切都解决了。

代码实现:
#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;elsecout<<minn+n-1<<endl;}return 0;}