当前位置:   article > 正文

1607D - Blue-Red Permutation(贪)_b - blue-red permutation

b - blue-red permutation

​​​​​​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反之。


  1. #include<bits/stdc++.h>
  2. #define ms(a) memset(a,0,sizeof(a));
  3. typedef long long ll;
  4. using namespace std;
  5. const int N = 2e5 + 5;
  6. pair<int, char> a[N];
  7. bool cmp(pair<int, char> a, pair<int, char> b) {
  8. if (a.second == b.second)return a.first < b.first;
  9. else return a.second < b.second;
  10. }
  11. bool solve() {
  12. int n;cin >> n;
  13. for (int i = 1; i <= n; i++) {
  14. cin >> a[i].first;
  15. }
  16. string s; cin >> s;
  17. for (int i = 1; i <= n; i++) {
  18. a[i].second = s[i-1];
  19. }
  20. sort(a + 1, a + 1 + n, cmp);
  21. for (int i = 1; i <= n; i++) {
  22. if (a[i].second == 'B' && a[i].first < i) return 0;
  23. if (a[i].second == 'R' && a[i].first > i) return 0;
  24. }
  25. return 1;
  26. }
  27. int main() {
  28. int t;
  29. cin >> t;
  30. while (t--) {
  31. if (solve())cout << "YES\n";
  32. else cout << "NO\n";
  33. }
  34. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Cpp五条/article/detail/395332
推荐阅读
  

闽ICP备14008679号