FJNU OJ 1053

1053: 黄黄的对对碰
Time Limit: 1 Sec Memory Limit: 128 MB
Submit: 133 Solved: 8

Submit Status Discuss
Description
黄黄最近在玩一个叫对对碰的游戏,虽然舍友都嘲笑他很弱智,但是黄黄觉得比起每天在宿舍lu伤身,这个游戏还是挺可爱的。对对碰的游戏是这样的:有n个石子,每个石子有一个价值,每次黄要从n个石子里面选择两个石子,他首先选择一个石子记下石子的价值,然后将石子放回,接着他继续选择一个石子同样记下价值放回。那么他就会得到对应石子价值相加的分数,黄黄很想知道如何选择石子能使他能得到的分数最小,但是要保证分数>=0.如果黄黄无法使得分数>=0他就会觉得自己很蠢。

Input
多组样例,每组样例的第一行是一个数字n表示石子的个数 ( 1 <= n <= 1e5 )

接下来的一行n个由空格隔开的数字Ai表示每个石子的价值 ( | Ai | <= 1e8 )

Output
如果能找到价值相加>=0的选择方案,请输出满足条件的最小分数

如果不能找到输出:HH is stupid!

Sample Input
5
-5 -1 -1 2 -5
4
-1 -1 -1 -1
Sample Output
1
HH is stupid!

二分查找

下面是手写二分查找:

#include"cstring"
#include"string.h"
#include"iostream"
#include"cstdio"
#include "cstdlib"
#include "algorithm"

using namespace std;
int a[200000];

long long ans;

void search(int selnum, int front, int end)
{
    if (front == end)
        return;
    int mid = (front + end) / 2;
    if (selnum + a[mid] >= 0)
    {
        if ((selnum + a[mid] >= 0) && (selnum + a[mid]<ans))
            ans = selnum + a[mid];
        search(selnum, front, mid);
    }
    else
    {
        search(selnum, mid + 1, end);
    }
}

int main(void)
{
    int nstone;
    while (scanf("%d", &nstone) != EOF)
    {
        for (int i = 0; i<nstone; i++)
        {
            scanf("%d", &a[i]);
        }
        sort(a, a + nstone);
        ans = 9999999999;
        for (int i = 0; i<nstone; i++)
        {
            if (2 * a[i] >= 0 && 2 * a[i]<ans)
            {
                ans = 2 * a[i];
            }
            search(a[i], 0, nstone - 1);
        }
        if (ans == 9999999999)
            printf("HH is stupid!\n");
        else
            printf("%ld\n", ans);
    }

}

直接调用algorithm库里头的lowerbound 查找当前数的相反数,可以起到和上面相同效果。

关于lower_bound()大神的解释:
函数lower_bound()在first和last中的前闭后开区间进行二分查找,返回大于或等于val的第一个元素位置。如果所有元素都小于val,则返回last的位置
举例如下:
一个数组number序列为:4,10,11,30,69,70,96,100.设要插入数字3,9,111.pos为要插入的位置的下标

pos = lower_bound( number, number + 8, 3) - number,pos = 0.即number数组的下标为0的位置。
pos = lower_bound( number, number + 8, 9) - number, pos = 1,即number数组的下标为1的位置(即10所在的位置)。
pos = lower_bound( number, number + 8, 111) - number, pos = 8,即number数组的下标为8的位置(但下标上限为7,所以返回最后一个元素的下一个元素)。
所以,要记住:函数lower_bound()在first和last中的前闭后开区间进行二分查找,返回大于或等于val的第一个元素位置。如果所有元素都小于val,则返回last的位置,且last的位置是越界的!!~
返回查找元素的第一个可安插位置,也就是“元素值>=查找值”的第一个元素的位置

#include"cstring"
#include"string.h"
#include"iostream"
#include"cstdio"
#include "cstdlib"
#include "algorithm"

using namespace std;
int a[200000];

long long ans;


int main(void)
{
    int nstone;
    while (scanf("%d", &nstone) != EOF)
    {
        for (int i = 0; i<nstone; i++)
        {
            scanf("%d", &a[i]);
        }
        sort(a, a + nstone);
        ans = 9999999999;
        for (int i = 0; i<nstone; i++)
        {
            long pos=lower_bound(a,a+nstone-1,-a[i])-a;
            if(a[i]+a[pos]>=0&&a[i]+a[pos]<ans)
                ans=a[i]+a[pos];
        }
        if (ans == 9999999999)
            printf("HH is stupid!\n");
        else
            printf("%lld\n", ans);
    }

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值