赞
踩
1607D - Blue-Red Permutation
题目:
int数组a和char数组b,当b[i]=R时可以将a[i]增大,b[i]=B时可以将a[i]减小。求是否能将a[i]变成包含1~n的数组。
题解:
用pair数组存储,对其进行排序,贪心一下,让B在前面,R在后面。
因为B只能减小,所以放在前面,R反之。
- #include<bits/stdc++.h>
- #define ms(a) memset(a,0,sizeof(a));
- typedef long long ll;
- using namespace std;
- const int N = 2e5 + 5;
-
- pair<int, char> a[N];
- bool cmp(pair<int, char> a, pair<int, char> b) {
- if (a.second == b.second)return a.first < b.first;
- else return a.second < b.second;
- }
- bool solve() {
- int n;cin >> n;
- for (int i = 1; i <= n; i++) {
- cin >> a[i].first;
- }
- string s; cin >> s;
- for (int i = 1; i <= n; i++) {
- a[i].second = s[i-1];
- }
- sort(a + 1, a + 1 + n, cmp);
- for (int i = 1; i <= n; i++) {
- if (a[i].second == 'B' && a[i].first < i) return 0;
- if (a[i].second == 'R' && a[i].first > i) return 0;
- }
- return 1;
- }
- int main() {
- int t;
- cin >> t;
- while (t--) {
- if (solve())cout << "YES\n";
- else cout << "NO\n";
- }
- }

Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。