Codeforces 1463D Pairs
题意
对于数字\(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的更多相关文章
- Codeforces 1169B Pairs
题目链接:http://codeforces.com/contest/1169/problem/B 题意:给你 m 对数 ,问你能不能在 1 − n 之间找到俩个不相等的 x 和 y 使得 对于前面每 ...
- codeforces 1391E Pairs of Pairs dfs树的性质
https://codeforces.com/problemset/problem/1391/E 题意:给一个无向图,找出以下任意一种输出答案 1,长度>=n/2(上界)的简单路径(没有同一个点 ...
- Educational Codeforces Round 10 C. Foe Pairs 水题
C. Foe Pairs 题目连接: http://www.codeforces.com/contest/652/problem/C Description You are given a permu ...
- Codeforces Round #562 (Div. 2) B. Pairs
链接:https://codeforces.com/contest/1169/problem/B 题意: Toad Ivan has mm pairs of integers, each intege ...
- Codeforces 1404 D. Game of Pairs
Codeforces 1404 D.Game of Pairs 给定\(2n\)个数\(1,2,...,2n\),A 和 B 将进行交互,规则如下: A 需要将元素分成 n 组 \(\mathbf{p ...
- Codeforces 159D Palindrome pairs
http://codeforces.com/problemset/problem/159/D 题目大意: 给出一个字符串,求取这个字符串中互相不覆盖的两个回文子串的对数. 思路:num[i]代表左端点 ...
- codeforces 652C C. Foe Pairs(尺取法+线段树查询一个区间覆盖线段)
题目链接: C. Foe Pairs time limit per test 1 second memory limit per test 256 megabytes input standard i ...
- codeforces#572Div2 E---Count Pairs【数学】【同余】
题目:http://codeforces.com/contest/1189/problem/E 题意:给定$n$个互不相同数,一个$k$和一个质数$p$.问这$n$个数中有多少对数$(a_i+a_j) ...
- 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 ...
- 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 ...
随机推荐
- 通过实例了解vue3.3更新的特征
开场白 5月份,vue团队发布了 vue3.3. 这次小版本的发布主要解决了-- Vue 与 TypeScript 一起使用时的许多长期存在的痛点. 下面我们一起来学习一下vue3.3新特征 准备新新 ...
- ORM核心功能之导航属性- EFCore和 SqlSugar
导航属性 导航属性是作为ORM核心功能中的核心,在SqlSugar没有支持导航属性前,都说只是一个高级DbHelper, 经过3年的SqlSugar重构已经拥有了一套 非常成熟的导航属性体系,本文不是 ...
- Active Directory Basic
Active Directory 是 Windows 域网络的目录服务 介绍 Active Directory 是在域内部连接的机器和服务器的集合,它们是构成 Active Directory 网络的 ...
- JS基础语法(一)
javascript简介 javascrpit是是一种轻量级的编程语言,常用于web前端开发.另外js还可以用来写node.js类型的服务和工具,在测试web项目的时候需要了解. 变量 js定义变量有 ...
- VUE路由传参的实用方式
本文讲解了VUE项目中路由之间的传值方式,涉及到的方法都是开发时常用的,希望对大家有多帮助. 1. 方式一:使用router-link标签 1.1 params 传参 首先定义好路由 const ro ...
- 搭建springbootweb环境
搭建springboot环境(idea环境) 实现步骤: 1.基础环境配置 2.maven配置 3.编写第一个程序helloworld(可能有两个小问题) 4.运行(jar包运行,命令行运行) 一.基 ...
- 沉思篇-剖析JetPack的Lifecycle
这几年,对于Android开发者来说,最时髦的技术当属Jetpack了.谷歌官方从19年开始,就在极力推动Jetpack的使用,经过这几年的发展,Jetpack也基本完成了当时的设计目标--简单,一致 ...
- SpringBoot基础学习(番外9.1)Spring MVC或Spring Boot配置默认访问页面不生效?
1.tomcat配置默认访问页面 进入 tomcat 的 conf 目录,编辑 web.xml 文件.在 <web-app></web-app> 添加默认访问页面. <w ...
- “easyExcel”导入的代码实现
使用easyExcel在导入数据事有很好的使用性,方便操作. 添加依赖: <dependency> <groupId>com.alibaba</groupId> & ...
- LAL v0.36.7发布,Customize Sub,我有的都给你
Go语言流媒体开源项目 LAL 今天发布了v0.36.7版本. LAL 项目地址:https://github.com/q191201771/lal 老规矩,简单介绍一下: ▦ Customize S ...