题目链接:点击查看

题目大意:给出一个长度为 nnn 的数列,每个数字有一个颜色,如果是蓝色,每次操作则可以减一;如果是红色,每次操作则可以加一。

问有限次操作后,能否将数组变为一个长度为 nnn 的排列。

题目分析:第一次做的时候用 multiset 乱搞过了,但感觉不算正解,看了题解后发现这不就是一个经典的贪心套路:区间和数字的最大匹配问题。

先将区间按左端点排序,再将数字从小到大排序,然后用优先队列控制区间右端点是否过期就可以了。

代码:

// Problem: D. Blue-Red Permutation
// Contest: Codeforces - Codeforces Round #753 (Div. 3)
// URL: https://codeforces.com/contest/1607/problem/D
// Memory Limit: 256 MB
// Time Limit: 1000 ms
//
// Powered by CP Editor (https://cpeditor.org)// #pragma GCC optimize(2)
// #pragma GCC optimize("Ofast","inline","-ffast-math")
// #pragma GCC target("avx,sse2,sse3,sse4,mmx")
#include<iostream>
#include<cstdio>
#include<string>
#include<ctime>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<stack>
#include<climits>
#include<queue>
#include<map>
#include<set>
#include<sstream>
#include<cassert>
#include<bitset>
#include<list>
#include<unordered_map>
#define lowbit(x) (x&-x)
using namespace std;
typedef long long LL;
typedef unsigned long long ull;
template<typename T>
inline void read(T &x)
{T f=1;x=0;char ch=getchar();while(0==isdigit(ch)){if(ch=='-')f=-1;ch=getchar();}while(0!=isdigit(ch)) x=(x<<1)+(x<<3)+ch-'0',ch=getchar();x*=f;
}
template<typename T>
inline void write(T x)
{if(x<0){x=~(x-1);putchar('-');}if(x>9)write(x/10);putchar(x%10+'0');
}
const int inf=0x3f3f3f3f;
const int N=1e6+100;
struct Node {int l,r;bool operator<(const Node& t)const {return r>t.r;}
}node[N];
int a[N];
char s[N];
int main()
{#ifndef ONLINE_JUDGE
//  freopen("data.in.txt","r",stdin);
//  freopen("data.out.txt","w",stdout);
#endif
//  ios::sync_with_stdio(false);int w;cin>>w;while(w--) {int n;read(n);for(int i=1;i<=n;i++) read(a[i]);scanf("%s",s+1);for(int i=1;i<=n;i++) {if(s[i]=='B') node[i]={1,a[i]};else node[i]={a[i],n};}sort(node+1,node+1+n,[&](Node a,Node b) {return a.l<b.l;});bool flag=true;priority_queue<Node>q;int p=1;for(int i=1;i<=n;i++) {while(p<=n&&node[p].l<=i) q.push(node[p++]);int can=false;while(q.size()) {Node cur=q.top();q.pop();if(cur.r>=i) {can=true;break;}}flag&=can;}puts(flag?"YES":"NO");}return 0;
}

CodeForces - 1607D Blue-Red Permutation(贪心)相关推荐

  1. CodeForces - 351E Jeff and Permutation(贪心)

    题目链接:点击查看 题目大意:给出一个长度为 n 的序列,可以选择某些位置使得 a[ i ] =  - a[ i ],问逆序对最少可以是多少 题目分析:需要看出的一个关系就是,在某一对关系 ( i , ...

  2. codeforces Gym 100338E Numbers (贪心,实现)

    题目:http://codeforces.com/gym/100338/attachments 贪心,每次枚举10的i次幂,除k后取余数r在用k-r补在10的幂上作为候选答案. #include< ...

  3. codeforces 7.22 E Permutation Shift

    codeforces 7.22 E Permutation Shift 给出一个1到n的排列,每次可以交换两个数,问在交换最多m次(m <= n/3)之后能不能得到由1 2 3 - n循环右移所 ...

  4. Codeforces 671E Organizing a Race (贪心、线段树)

    题目链接 https://codeforces.com/contest/671/problem/E 题解 完全不会做--基本是抄lk的代码 ruogu的题解: https://www.luogu.co ...

  5. CodeForces - 1529E Trees of Tranquillity(贪心+线段树)

    题目链接:https://vjudge.net/problem/CodeForces-1529E 题目大意:给出两棵根节点为 111 的树,分别称为 AAA 树和 BBB 树,现在通过两棵树可以构造出 ...

  6. [CodeForces 1603C] Extreme Extension(贪心 + 数论分块优化dp)

    problem CodeForces solution observation1:\text{observation1}:observation1: 对于一个非空子段 [l,r][l,r][l,r], ...

  7. codeforces 416C C. Booking System(贪心)

    题目链接: codeforces 416C 题目大意: 给出n个请求,每个请求包括客人数量和支付金额,再给出m个桌子,包括桌子大小,问如何安排才能最大盈利.给出最大盈利和一个能够最大盈利的方案. 题目 ...

  8. Codeforces 540B School Marks 【贪心构造】

    题目链接:Codeforces 540B School Marks Little Vova studies programming in an elite school. Vova and his c ...

  9. CodeForces 508E Arthur and Brackets 贪心

    题目: E. Arthur and Brackets time limit per test 2 seconds memory limit per test 128 megabytes input s ...

最新文章

  1. Windows 2008 部署服务之Windows 7 应答文件创建
  2. AI理论知识整理(3)-正定矩阵
  3. python django框架_Python Web框架【Django框架第一篇基础】
  4. 安装maya过程记录
  5. 车用总线技术 | J1939协议实用指南与J1939数据记录方案
  6. 计算机流程图代表判断的是什么意思,算法流程图符号含义有哪些?用奇偶数判断算法流程图理解...
  7. 初级嵌入式软件工程师学习路线(在校本科或者硕士)
  8. STM32LED--基于HAL库(LCD与LED冲突?一文看懂如何精准劝架)
  9. codesmith mysql 注释_完美解决CodeSmith无法获取MySQL表及列Description说明注释的方案...
  10. Windows和ubuntu互传文件
  11. JavaScript九九乘法口诀表
  12. leedcode每日一题:860. 柠檬水找零
  13. 游戏BI数据统计分析相关
  14. 1、VS2019配置glfw和glad
  15. 纯Web前端打造的元宇宙展厅——开箱即用的Lingo3D游戏引擎 支持原生、React、Vue
  16. 访问Oracle数据库的四款工具软件介绍
  17. lbe+android6.0+免root,LBE安全大师|lbe安全大师免root下载v6.1.2482 - 欧普软件下载
  18. Kubernetes 集群基于 Rook 的 Ceph 存储之块设备、文件系统、对象存储
  19. 一文带你怼明白进程和线程通信原理
  20. 常用的跟外链相关的几种工具

热门文章

  1. (史上最全)104个实用网络爬虫合集。
  2. c#保存数据格式为.cvs_C#读取csv格式文件的方法
  3. java表达式语法格式为_2009(选修)JavaWeb模拟试卷(2011-2012)
  4. SpringBoot的优势
  5. MySQL 高级 - 触发器 - 查看及删除
  6. 织入业务代码-ModifyService
  7. 基于Xml 的IOC 容器-分配解析策略
  8. 常用json框架介绍和Jackson返回结果处理
  9. Hive的基本操作-表结构修改和数据加载
  10. 绕过网关访问图片上传并解决跨域问题