[原创题] 逆伐苍穹 two-pointer

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[原创题] 逆伐苍穹 two-pointer相关的知识,希望对你有一定的参考价值。

题意

  技术分享

 

实现

  有两个指针: 主指针, 附指针.

  主指针移动, 则仍然要满足要求.

  附指针保持在满足与不满足的分界线.

#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cctype>
#include <algorithm>
using namespace std;

#define F(i, a, b) for (register int i = (a); i <= (b); i++)

#define LL long long

const int N = 1000005;

int t, n, s, a[N]; LL sum;

inline int rd(void) {
    int f = 1; char c = getchar(); for (; !isdigit(c); c = getchar()) if (c == -) f = -1;
    int x = 0; for (; isdigit(c); c = getchar()) x = x*10+c-0; return x*f;
}

int main(void) {
    #ifndef ONLINE_JUDGE
        freopen("god.in", "r", stdin);
        freopen("god.out", "w", stdout);
    #endif
    
    t = rd();
    n = rd(), s = rd();
    F(i, 1, n) a[i] = rd();
    sort(a+1, a+n+1);
    
    if (t == 1) {
        for (int fir = n, sec = 0; fir >= 1; fir--) {
            while (sec + 1 <= n && a[fir] + a[sec+1] < s) sec++;
            sum += sec;
        }
    }
    else if (t == 2) {
        for (int fir = n, sec = 0; fir >= 1; fir--) {
            while (sec + 1 <= fir && a[fir] + a[sec+1] < s) sec++;
            while (sec > fir) sec--;
            sum += sec;
        }
    }
    else if (t == 3) {
        for (int fir = 1, sec = n+1; fir <= n; fir++) {
            while (sec - 1 >= 1 && a[fir] + a[sec-1] > s) sec--;
            sum += (n+1-sec);
        }
    }
    else {
        for (int fir = 1, sec = n+1; fir <= n; fir++) {
            while (sec - 1 >= fir && a[fir] + a[sec-1] > s) sec--;
            while (sec < fir) sec++;
            sum += (n+1-sec);
        }
    }
    printf("%lld\\n", sum);
    
    return 0;
}

 

以上是关于[原创题] 逆伐苍穹 two-pointer的主要内容,如果未能解决你的问题,请参考以下文章

斗破苍穹读后感

航类C语言解谜赛《苍穹一粟》题解

博客首页全新改版;博客代码片支持折叠;原创博文新增打赏功能……2022.1.17

金蝶云苍穹作为一款无代码\低代码平台怎么样,有类似产品推荐吗?

博客首页全新改版;博客代码片支持折叠;原创博文新增打赏功能……2022.1.17

题解51nod 1686第K大区间