题目
题意: 给定n个数,每个数具有B或者R属性之一。B表示该数可以执行任意次-1,R表示该数可以执行任意次+1。判断这n个数能否凑出一个permutation。
思路: 我直接线段树维护,发现没过样例,确实越来越菜了。
冷静分析,可以把b和r属性的数分别维护,进行排序。
之后按照数值now从小到大,如果b中的数值 < now,而他只能下降,根据鸽巢原理,不能凑出。
如果r中的数值 > now,而他只能上升,根据鸽巢原理,也不能凑出。
看代码就会了。
代码:
// 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) #include<iostream> #include<cstdio> #include<algorithm> #include<complex> #include<cstring> #include<cmath> #include<vector> #include<map> #include<unordered_map> #include<list> #include<set> #include<queue> #include<stack> #define OldTomato ios::sync_with_stdio(false),cin.tie(nullptr),cout.tie(nullptr) #define fir(i,a,b) for(int i=a;i<=b;++i) #define mem(a,x) memset(a,x,sizeof(a)) #define p_ priority_queue // round() 四舍五入 ceil() 向上取整 floor() 向下取整 // lower_bound(a.begin(),a.end(),tmp,greater<ll>()) 第一个小于等于的 // #define int long long //QAQ using namespace std; typedef complex<double> CP; typedef pair<int,int> PII; typedef long long ll; // typedef __int128 it; const double pi = acos(-1.0); const int INF = 0x3f3f3f3f; const ll inf = 1e18; const int N = 2e5+10; const int M = 1e6+10; const int mod = 1e9+7; const double eps = 1e-6; inline int lowbit(int x){ return x&(-x);} template<typename T>void write(T x) { if(x<0) { putchar('-'); x=-x; } if(x>9) { write(x/10); } putchar(x%10+'0'); } template<typename T> void read(T &x) { x = 0;char ch = getchar();ll f = 1; while(!isdigit(ch)){if(ch == '-')f*=-1;ch=getchar();} while(isdigit(ch)){x = x*10+ch-48;ch=getchar();}x*=f; } int n,m,k,T; int a[N]; void solve() { // read(n); cin>>n; vector<int> a(n); vector<int> b; vector<int> r; for(int i=0;i<n;++i) { cin>>a[i]; } for(int i=0;i<n;++i) { char ch; cin>>ch; if(ch == 'B') b.push_back(a[i]); else r.push_back(a[i]); } bool flag = true; sort(b.begin(),b.end()); //只能下降,取他们为小的数 sort(r.begin(),r.end()); //只能上升 int now = 1; for(int i=0;i<b.size();++i) { if(b[i] < now) { flag = false; } else now++; } for(int i=0;i<r.size();++i) { if(r[i] > now) { flag = false; } else now ++ ; } if(now == n+1) puts("YES"); else puts("NO"); } signed main(void) { // T = 1; OldTomato; cin>>T; // read(T); while(T--) { solve(); } return 0; }