C语言问题: Greed

版权声明:本人原创文章若需转载请标明出处和作者!沙沙 https://blog.csdn.net/weixin_44143702/article/details/86375843

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 ≤ 10^9) — volume of remaining cola in cans.

The third line contains n space-separated integers that b1, b2, ..., bn (ai ≤ bi ≤ 10^9) — 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".

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>

int main()
{
    int n, a[100010], b[100010], i, j, t, sum;
    memset(a, 0, sizeof(a));
    memset(b, 0, sizeof(b));
    scanf("%d", &n);
    sum = 0;
    for(i = 0; i < n; i++)
    {
        scanf("%d", &a[i]);
        sum += a[i];
    }
    for(i = 0; i < n; i++)
    {
        scanf("%d", &b[i]);
    }
    for(i = 0; i < n; i++)
    {
        for(j = 0; j < n - 1 - i; j++)
        {
            if(b[j] < b[j + 1])
            {
                t = b[j];
                b[j] = b[j + 1];
                b[j + 1] = t;
            }
        }
    }
    if(sum <= (b[0] + b[1]))  printf("YES\n");
    else  printf("NO\n");
    return 0;
}

 上方代码 time limit exceeded  

后面我把冒泡排序改为选择排序,并且只排除两个最大的数,但是变成了 wrong answer 

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>

int main()
{
    int n, a[100010], b[100010], i, j, t, sum, k, max;
    memset(a, 0, sizeof(a));
    memset(b, 0, sizeof(b));
    scanf("%d", &n);
    sum = 0;
    for(i = 0; i < n; i++)
    {
        scanf("%d", &a[i]);
        sum += a[i];
    }
    for(i = 0; i < n; i++)
    {
        scanf("%d", &b[i]);
    }
    for(i = 0; i < 2; i++)
    {
        k = i;
        max = k;
        for(j = k; j < n; j++)
        {
            if(b[max] < b[j])  max = j;
        }
        if(max != k)
        {
            t = b[k];
            b[k] = b[max];
            b[max] = t;
        }
    }
    if(sum <= (b[0] + b[1]))  printf("YES\n");
    else  printf("NO\n");
    return 0;
}

改正点:

1. 题目所给数过大,n大小为1e5,sum大小可能为1e10,而int范围是2^32=4e9, long long int 范围是1.8e19,此处sum的定义应定义为long long int

2. 此题不需要排序,直接找出最大的两个数就好,只需要循环2次,比排序耗时少很多

改正代码: 

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
long long int a[100010], b[100010];//数组开在main外面比较稳定
int main()
{
    int n, i, max1, k, max2;
    long long int sum;//这道题的数据量太大,sum定义为int会出问题
    memset(a, 0, sizeof(a));
    memset(b, 0, sizeof(b));
    scanf("%d", &n);
    sum = 0;
    for(i = 0; i < n; i++)
    {
        scanf("%lld", &a[i]);
        sum += a[i];//计算可乐的总量
    }
    max1 = 0;
    for(i = 0; i < n; i++)
    {
        scanf("%lld", &b[i]);
        if(max1 < b[i])
        {
            max1 = b[i];
            k = i;
        }
    }//找到第一大容量的瓶子,并记录其下标
    max2 = 0;
    for(i = 0; i < n; i++)
    {
        if(max2 < b[i] && i != k)  max2 = b[i];
    }//找到第二大容量的瓶子,并且这个数不是第一个数
    if(sum <= (max1 + max2))  printf("YES\n");
    else  printf("NO\n");
    return 0;
}

猜你喜欢

转载自blog.csdn.net/weixin_44143702/article/details/86375843