题意

对于数字\(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. centos安装Vue

    一直以来,有关LINUX的系统安装包,都是比较随意,直接使用yum进行或者apt-get 安装 标准安装流程是什么的呢.我们通过centos安装Vue进行展示 1 首先下载安装nodejs , htt ...

  2. < Python全景系列-9 > Python 装饰器:优雅地增强你的函数和类

    欢迎来到我们的系列博客<Python全景系列>第九篇!在这个系列中,我们将带领你从Python的基础知识开始,一步步深入到高级话题,帮助你掌握这门强大而灵活的编程语法.无论你是编程新手,还 ...

  3. Grafana系列-GaC-1-Grafana即代码的几种实现方式

    系列文章 Grafana 系列文章 Terraform 系列文章 概述 GaC(Grafana as Code, Grafana 即代码) 很明显是扩展自 IaC(Infrastructure as ...

  4. 创建springboot工程失败解决 spring initializr Error:cannot download

    创建springboot工程失败解决 问题描述 原因分析: 网络不好,因为springBooT项目的创建时必须联网的 解决方案: 方案一: 将创建 springBoot 工程的地址更换为如下的地址 阿 ...

  5. Thinkphp6 连接达梦数据库

    Thinkphp6 连接达梦数据库 这里使用 IDEA phpEnv PHP7.3 Thinkphp6 桌面操作系统:Windows11 虚拟机:VMware 服务器操作系统:银河麒麟 在虚拟机操作与 ...

  6. 使用GoEasy快速实现Android原生app中的websocket消息推送

    摘要: GoEasy带来了一项令开发者振奋的消息:全面支持Android原生平台!现在,您可以在Android应用中使用最酷炫的实时通信功能,借助GoEasy轻松实现消息的发送和接收.本文将带您领略G ...

  7. ChatGPT「代码解释器」正式开放,图片转视频仅需30秒!十大令人震惊的魔法揭秘

    经过超过三个月的等待,ChatGPT「代码解释器」终于全面开放.这是一大波神奇魔法的高潮. OpenAI的科学家Karpathy对这个强大的代码解释器测试版赞不绝口.他把它比作你的个人数据分析师,可以 ...

  8. P2709 小B的询问题解

    本题需要用到莫队算法 关于莫队算法 莫队算法是一种离线算法,适用于序列中统计区间特定的目标的问题. 时间复杂度通常是 \(O(n \sqrt n)\) 或更高. P2709 小B的询问 点击查看原题 ...

  9. 2023ccpc大学生程序设计竞赛-wmh

    这算是我第一次参加这种团队赛,感谢程老师给我这个机会.刚开赛还算比较顺利,一眼看出来A是个签到,拿下之后开始跟榜F题.一开始想法比较简单,就是排序,记录相邻两个数的差,然后再排序.wa了后以为是范围出 ...

  10. python:导入库、模块失败

    一般发生在程序开始部分: from pymodbus.client.sync import ModbusSerialClient from pymodbus.payload import Binary ...