HDU 5246 超级赛亚ACMer (lower_bound 与 upper_bound)

题目链接 : 超级赛亚ACMer

 

看别人AC代码学会了一个C++STL  lower_bound

ForwardIter lower_bound(ForwardIter first, ForwardIter last,const _Tp& val)算法返回一个非递减序列[first, last)中的第一个大于等于值val的位置

ForwardIter upper_bound(ForwardIter first, ForwardIter last, const _Tp& val)算法返回一个非递减序列[first, last)中第一个大于val的位置。

lower_bound和upper_bound如下图所示:

 

 

例如 a = [1, 2, 2, 3, 4, 4, 4]          

int a[10] = {1,2,3,4,4,4,4, 6};
lower_bound(a, a+8, 3) - a = 2, 即下标为2的那个数,要记得减去数组的首地址. 如果lower_bound(a, a+8, 5) - a = 7;

同理upper_bound

本人AC代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cctype>
#include <cmath>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <map>
 
using namespace std;
 
const int inf = 0x3f;
const int INF = 0x3f3f3f3f;
const int maxn = 1e5+5;
__int64 a[maxn];
int main()
{
    //freopen("1.txt", "r", stdin);
    int T, t, n;
    __int64 fig, k;
    scanf("%d", &T);
    for(t = 1; t <= T; t++)
    {
        scanf("%d %I64d %I64d", &n, &fig, &k);
        for(int i = 0; i < n; i++)
        {
            scanf("%I64d", &a[i]);
        }
        sort(a, a+n);
        int i, j;
        printf("Case #%d:\n", t);
        if(a[0] > fig)
        {
            puts("madan!");continue;
        }
        for(i = 0; i < n; i++)
        {
            if(a[i] > fig) break;
        }
        fig = a[i-1];
        for(i = i-1; i < n-1;i++)
        {
            if(fig+k >= a[i] && fig+k < a[i+1])
            {
                fig = a[i]; k--;
            }
        }
        fig+k >= a[n-1] ? puts("why am I so diao?") : puts("madan!");
    }
    return 0;
}

使用lower_bound 的代码

 

posted @   豪气干云  阅读(351)  评论(2编辑  收藏  举报
点击右上角即可分享
微信分享提示