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 ...
随机推荐
- Mybatis查询
查询 查询的数据为单条实体类 使用实体类进行接受即可,或者使用list,map接口均可.后面两者比较浪费 使用实体类接受 mapper接口: User selectUserById(int useri ...
- 【Java】Eclipse常用快捷键整理
前言 还是最近在上Java课,由于疫情原因,看的网课,那里的老师比较实战派,很多时候不知道按了什么快捷键就立马出现了很骚的操作.网上查询后发现了一些快捷键对于我这个eclipse小白还是挺常用的,整理 ...
- Python-PyQt5的安装与简单使用
一.安装 1.安装 PyQt5 和 PyQt5-tools pip install PyQt5 -i https://pypi.douban.com/simple pip install PyQt5- ...
- EndNote参考文献格式Output Styles界面介绍
本文对EndNote软件修改论文参考文献引用格式的界面与各选项参数加以详细介绍. 利用EndNote软件进行论文参考文献的插入可以说是非常方便:但其亦具有一个问题,就是对中文文献的支持不太友好 ...
- chatgpt入口,免费在线chatgpt--与人工智能聊天?尝试chatgpt入口,免费在线chatgpt吧!
介绍一款人工智能聊天机器人--chatgpt入口 chatgpt是一款智能聊天机器人,它能够与人类进行自然语言对话,可以回答问题.提供建议,还可以玩游戏和聊天互动,是当前最受欢迎的人工智能聊天工具之一 ...
- expected unqualified-id on oneapi tbb12
230628-expected unqualified-id eton@230628 在编译supra基于debian12 Qt5.15 tbb12 libtbb12/stable,now 2021. ...
- Github秒变VSCode在线编辑器
在仓库页面 在网页地址中github后面加上 1s 即可
- 跟着 GPT-4 从0到1学习 Golang 并发机制(三)
目录 一.前言 二.开聊 2.1 关于 goroutine 泄露问题 2.2 内存模型 2.3 Race Detector 检测数据竞争 三.总结 一.前言 话接上回<跟着 GPT-4 从0到1 ...
- 【Python】从同步到异步多核:测试桩性能优化,加速应用的开发和验证
测试工作中常用到的测试桩mock能力 在我们的测试工作过程中,可能会遇到多个项目并行开发的时候,后端服务还没有开发完成,或者我们需要压测某个服务,这个服务测在试环境的依赖组件(如 MQ) 无法支撑我们 ...
- MybatisPlus的各种查询方法
MybatisPlus的各种查询方法 合并转载于https://my.oschina.net/u/241218/blog/1838534/和https://my.oschina.net/u/24275 ...