题意

对于数字\(1\)~\(2n\),可以构造出\(n\)个二元组,对于\(n\)个二元组,选择一个数组\(x\),留下\(x\)个二元组的最小值,留下\(n-x\)个二元组的最大值,其构成了一个集合。

现在给定一个集合\(b\),问有多少种\(x\)的取值可以得到该集合。

分析

首先判定的是\(x\),所以对于任意\(x\)来说,前\(x\)个一定为留下的最小值,否则若\(x<y\),且\(x\)为取的最大值而\(y\)为取的最小值,那不妨交换\(x\)和\(y\)同样满足条件。那么我们只需要判定每一个\(x\)的取值是否合法。

假设当前取值为\(i\),那么对于前\(i\)个数,都可以找到一个比其大的数配对,而对于后\(n-i\)个数,都可以找到一个比其小的数配对。贪心的将丢弃的数放置在一个\(set\)中,对于前\(i\)个数,贪心的找到最小的比其大的数,若是无法找到,则表示该位置无法取值。同样对于后\(n-i\)个数,贪心的找到其最大的比其小的数,若是无法找到,则表示该位置无法取值。

我们可以发现一个性质,如果第\(i\)个位置是可行的,那么要保证前\(i\)个数是可以有配对数的,同时后\(n-i\)个数也是可以有配对数的,那么我们正着判断前\(i\)个数,反着判断后\(n-i\)个数,判断完后扫一遍即可。

#pragma GCC optimize(3, "Ofast", "inline")

#include <bits/stdc++.h>

#define start ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define ll long long
#define ull unsigned long long
#define int ll
#define ls st<<1
#define rs st<<1|1
#define pii pair<int,int>
#define rep(z, x, y) for(int z=x;z<=y;++z)
#define repd(z, x, y) for(int z=x;z>=y;--z)
#define com bool operator<(const node &b)const
using namespace std;
mt19937 rnd(chrono::high_resolution_clock::now().time_since_epoch().count());
const int maxn = (ll) 1e6 + 5;
const int mod = 998244353;
const int inf = 0x3f3f3f3f;
int T = 1;
int a[maxn];
int n;
bool pre[maxn], last[maxn]; void solve() {
cin >> n;
set<int> s, t;
rep(i, 1, 2 * n)s.insert(i), t.insert(i);
rep(i, 1, n) {
pre[i] = last[i] = false;
int x;
cin >> x;
s.erase(x);
t.erase(x);
a[i] = x;
}
sort(a + 1, a + n + 1);
int ans = 0;
rep(i, 1, n) {
while (!s.empty() && *s.begin() < a[i])
s.erase(s.begin());
if (s.empty())
break;
if (*s.begin() > a[i])
s.erase(s.begin());
else
break;
pre[i] = true;
}
repd(i, n, 1) {
while (!t.empty() && *(--t.end()) > a[i])
t.erase(--t.end());
if (t.empty())
break;
if (*(--t.end()) < a[i])
t.erase(--t.end());
else
break;
last[i] = true;
}
pre[0] = last[n + 1] = true;
rep(i, 0, n)ans += (pre[i] && last[i + 1]);
cout << ans << '\n';
} signed main() {
start;
cin >> T;
while (T--)
solve();
return 0;
}

Codeforces 1463D Pairs的更多相关文章

  1. Codeforces 1169B Pairs

    题目链接:http://codeforces.com/contest/1169/problem/B 题意:给你 m 对数 ,问你能不能在 1 − n 之间找到俩个不相等的 x 和 y 使得 对于前面每 ...

  2. codeforces 1391E Pairs of Pairs dfs树的性质

    https://codeforces.com/problemset/problem/1391/E 题意:给一个无向图,找出以下任意一种输出答案 1,长度>=n/2(上界)的简单路径(没有同一个点 ...

  3. Educational Codeforces Round 10 C. Foe Pairs 水题

    C. Foe Pairs 题目连接: http://www.codeforces.com/contest/652/problem/C Description You are given a permu ...

  4. Codeforces Round #562 (Div. 2) B. Pairs

    链接:https://codeforces.com/contest/1169/problem/B 题意: Toad Ivan has mm pairs of integers, each intege ...

  5. Codeforces 1404 D. Game of Pairs

    Codeforces 1404 D.Game of Pairs 给定\(2n\)个数\(1,2,...,2n\),A 和 B 将进行交互,规则如下: A 需要将元素分成 n 组 \(\mathbf{p ...

  6. Codeforces 159D Palindrome pairs

    http://codeforces.com/problemset/problem/159/D 题目大意: 给出一个字符串,求取这个字符串中互相不覆盖的两个回文子串的对数. 思路:num[i]代表左端点 ...

  7. codeforces 652C C. Foe Pairs(尺取法+线段树查询一个区间覆盖线段)

    题目链接: C. Foe Pairs time limit per test 1 second memory limit per test 256 megabytes input standard i ...

  8. codeforces#572Div2 E---Count Pairs【数学】【同余】

    题目:http://codeforces.com/contest/1189/problem/E 题意:给定$n$个互不相同数,一个$k$和一个质数$p$.问这$n$个数中有多少对数$(a_i+a_j) ...

  9. CodeForces - 1189E Count Pairs(平方差)

    Count Pairs You are given a prime number pp, nn integers a1,a2,…,ana1,a2,…,an, and an integer kk. Fi ...

  10. CodeForces - 1189 E.Count Pairs (数学)

    You are given a prime number pp, nn integers a1,a2,…,ana1,a2,…,an, and an integer kk. Find the numbe ...

随机推荐

  1. C语言访问数据对象在内存中真实位模式的一个方法

    在判定机器采用大端还是小端存储时,可以按字节输出某数据对象的机器表示的位模式.机器表示的位模式即某数据对象在内存中的二进制串.下面是一个访问数据对象位模式的方法: //传入一个数据对象,从低地址到高地 ...

  2. phpstudy-sqlilabs-less-3

    题目:GET - Error based - Single quotes with twist 基于错误的单引号GET型变形注入 ?id=1 )and 1=2--+ ?id=1 "and 1 ...

  3. drf——反序列化校验源码(了解)、断言、drf之请求和响应、视图之两个视图基类

    1.模块与包 # 模块与包 模块:一个py文件 被别的py文件导入使用,这个py文件称之为模块,运行的这个py文件称之为脚本文件 包:一个文件夹下有__init__.py # 模块与包的导入问题 '' ...

  4. There is not enough memory to perform the requested operation

    今日在写bug 时 ide 突发脑溢血,崩溃了 一.修改用户目录下的 .vmoptions 找到C:\用户\用户名.WebStorm2018.1\config\webstorm64.exe.vmopt ...

  5. 2023-06-03:redis中pipeline有什么好处,为什么要用 pipeline?

    2023-06-03:redis中pipeline有什么好处,为什么要用 pipeline? 答案2023-06-03: Redis客户端执行一条命令通常包括以下四个阶段: 1.发送命令:客户端将要执 ...

  6. 案例实践 | 某能源企业API安全实践

    随着智能电网.全球能源互联网."互联网+电力".新电改的全面实施,分布式能源.新能源.电力交易.智能用电等新型业务不断涌现,运营模式.用户群体都将发生较大变化,电力市场由相对专业向 ...

  7. 安装指定版本的mysql(mysql5.7)

    安装指定版本的mysql(mysql5.7) 目标:解决需求,安装mysql5.7 前言: 安装软件的三种方式: rpm 安装 源代码编译安装 yum仓库安装 本地光盘 阿里云yum源 自建yum仓库 ...

  8. 什么是Sparse by default for crates.io

    当 Rust crate 发布到 crates.io 上时,可以启用"Sparse by default"特性,这意味着默认情况下,crate 不会包含所有依赖项在上传到 crat ...

  9. Mysql数据库体系化详细笔记(b站韩顺平)

    Mysql数据库 一.数据库 使用命令行窗口连接MYSQL数据库 mysql服务启动,在cmd输入net start mysql 1.mysql -h主机名-Р端口-u用户名-p密码 2.登录前,保证 ...

  10. 如何构建您的第一部AWS数据库服务

    目录 2.1. 基本概念解释 2.2. 技术原理介绍 2.3. 相关技术比较 实现步骤与流程 2.3.1 准备工作:环境配置与依赖安装 2.3.2 核心模块实现 2.3.3 集成与测试 4. 应用示例 ...