首页 > 代码库 > [原创题] 逆伐苍穹 two-pointer
[原创题] 逆伐苍穹 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
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。