2020 ICPC亚洲区域赛(沈阳)F-Kobolds and Catacombs(思维+模拟)
生活随笔
收集整理的這篇文章主要介紹了
2020 ICPC亚洲区域赛(沈阳)F-Kobolds and Catacombs(思维+模拟)
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
F-Kobolds and Catacombs
考慮每一段,先找到最小的,然后把編號小于它的都化為一組,此時有一個最大值需要把小于最大值的在劃分進(jìn)來,然后又有編號小于它的,于是再把編號小于它的劃分為一組,while1 循環(huán)一下即可模擬。
Code1
#include<bits/stdc++.h> using namespace std; using ll=long long; template <class T=int> T rd() {T res=0;T fg=1;char ch=getchar();while(!isdigit(ch)) {if(ch=='-') fg=-1;ch=getchar();}while( isdigit(ch)) res=(res<<1)+(res<<3)+(ch^48),ch=getchar();return res*fg; } const int N=1000010; int n; struct node {int v,id;bool operator<(const node&o)const{return v<o.v||v==o.v&&id<o.id;} }a[N]; int b[N];int main() {n=rd();for(int i=1;i<=n;i++) {a[i].v=rd(),a[i].id=i;b[i]=a[i].v;}sort(a+1,a+1+n);int ans=0;for(int i=1,j=1;i<=n&&j<=n;i++){ans++;int id=a[i].id;int mx=a[i].v;while(1){for(;j<=id;j++) mx=max(mx,b[j]);while(i<=n&&(a[i].v<mx||a[i].v==mx&&a[i].id<=id)) {if(a[i].v<mx) id=max(id,a[i].id);i++;}if(i==n+1) break;for(;j<=id;j++) mx=max(mx,b[j]);if(a[i].v>=mx) break;}i--;}printf("%d\n",ans);return 0; }Code2
題解大佬的代碼,一看就恍然大悟,非常簡潔
#include<bits/stdc++.h> using namespace std; using ll=long long; template <class T=int> T rd() {T res=0;T fg=1;char ch=getchar();while(!isdigit(ch)) {if(ch=='-') fg=-1;ch=getchar();}while( isdigit(ch)) res=(res<<1)+(res<<3)+(ch^48),ch=getchar();return res*fg; } const int N=1000010; int n; int a[N],b[N]; int main() {n=rd();for(int i=1;i<=n;i++) a[i]=rd(),b[i]=a[i];sort(b+1,b+1+n);ll sa=0,sb=0;int ans=0;for(int i=1;i<=n;i++){sa+=a[i];sb+=b[i];if(sa==sb) ans++;}printf("%d\n",ans);return 0; }總結(jié)
以上是生活随笔為你收集整理的2020 ICPC亚洲区域赛(沈阳)F-Kobolds and Catacombs(思维+模拟)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 电脑装机一筹莫展?一文看懂攒机全过程
- 下一篇: 2020 ICPC亚洲区域赛(沈阳)H-