Codeforces Round #446 (Div. 2)

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
2
3 5
3 6
output
YES
input
3
6 8 9
6 10 12
output
NO
input
5
0 0 5 0 0
1 1 8 10 5
output
YES
input
4
4 1 0 3
5 2 2 3
output
YES
Note

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

 

n个数据的和,然后在另一个n个数取两个大就行了,我不小心爆了LL

#include<stdio.h>
#include<bits/stdc++.h>
using namespace std;
const int N=100005;
int a[N];
int main()
{
    int n;
    scanf("%d",&n);
    long long s=0;
    for(int i=1;i<=n;i++)
    {
        int x;
        scanf("%d",&x);
        s+=x;
        if(s>int(2e9)){printf("NO");return 0;}
    }
    for(int i=1;i<=n;i++)
        scanf("%d",&a[i]);
        sort(a+1,a+n+1);
    printf("%s",(s<=a[n]+a[n-1])?"YES":"NO");
    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
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.

 

 直接暴力下取大

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=1e6+5;
ll l[N],ma[N];
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    ll n;
    cin>>n;
    memset(ma,0,sizeof(ma));
    for(ll i=1;i<=n;i++){
        cin>>l[i];
        ll x=max(1LL,i-l[i]);
        ma[x]=max(i-x,ma[x]);
    }
    ll ans=0,f=0;
    for(ll i=1;i<=n;i++){
        f=max(f,ma[i]);
        if(f==0)ans++;
        f--;
    }
    cout<<ans;
    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 adjacentelements 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.

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

这个有1的话就得直接输出啊,n-f

#include<stdio.h>
#include<bits/stdc++.h>
using namespace std;
const int N=2005;
int a[N],b[N];
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    int n;
    cin>>n;
    int f=0;
    for(int i=1; i<=n; i++)
    {
        cin>>a[i];
        if(a[i]==1)f++;
    }
    if(f)printf("%d",n-f);
    else
    {
        f=1;
        for(;;)
        {
            if(f>888*n)
            {
                printf("-1");
                return 0;
            }
            for(int i=f+1; i<=n; i++)
            {
                b[i]=__gcd(a[i-1],a[i]);
                if(b[i]==1)
                {
                    cout<<f+n-1;
                    return 0;
                }
            }
            f++;
            for(int i=1; i<=n; i++)a[i]=b[i];
        }
    }
    return 0;
}
D. Gluttony
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

You are given an array a with n distinct integers. Construct an array b by permuting a such that for every non-empty subset of indices S = {x1, x2, ..., xk} (1 ≤ xi ≤ n0 < k < n) the sums of elements on that positions in aand b are different, i. e.

Input

The first line contains one integer n (1 ≤ n ≤ 22) — the size of the array.

The second line contains n space-separated distinct integers a1, a2, ..., an (0 ≤ ai ≤ 109) — the elements of the array.

Output

If there is no such array b, print -1.

Otherwise in the only line print n space-separated integers b1, b2, ..., bn. Note that b must be a permutation of a.

If there are multiple answers, print any of them.

Examples
input
2
1 2
output
2 1 
input
4
1000 100 10 1
output
100 1 1000 10
Note

An array x is a permutation of y, if we can shuffle elements of y such that it will coincide with x.

Note that the empty subset and the subset containing all indices are not counted.

 

 这个P金爷讲思路,跟着做就行了

#include<bits/stdc++.h>
using namespace std;
const int N=200;
pair<int,int>a[N];
int ans[N];
int main()
{
    int n;
    scanf("%d",&n);
    for(int i=0; i<n; i++)
        scanf("%d",&a[i].first),a[i].second=i;
    sort(a,a+n);
    for(int i=0; i<n; i++)
    {
        int to=a[i].second;
        if(i==0)
            ans[to]=a[n-1].first;
        else
            ans[to]=a[i-1].first;
    }
    for(int i=0; i<n; i++)
    {
        printf("%d ",ans[i]);
    }
    return 0;
}

 

 

 

posted @ 2017-11-18 22:40  暴力都不会的蒟蒻  阅读(455)  评论(0编辑  收藏  举报