日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

CodeForces - 1607D Blue-Red Permutation(贪心)

發布時間:2024/4/11 编程问答 59 豆豆
生活随笔 收集整理的這篇文章主要介紹了 CodeForces - 1607D Blue-Red Permutation(贪心) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

題目鏈接:點擊查看

題目大意:給出一個長度為 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(贪心)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。