Codeforces Round #441 D. Sorting the Coins(模拟)
生活随笔
收集整理的這篇文章主要介紹了
Codeforces Round #441 D. Sorting the Coins(模拟)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
http://codeforces.com/contest/876/problem/D
題意:
題意真是難懂,就是給一串序列,第i次操作會在p[x](1<=x<=i)這些位置放上硬幣,然后從左到右觀察,如果第i個位置有硬幣但第i+1個位置沒有硬幣,那么互換,然后繼續從第i+1個硬幣開始看。直到不需要交換,需要計算出到終極狀態需要多少步。
?
思路:
模擬。
1 #include<iostream> 2 #include<algorithm> 3 #include<cstring> 4 #include<cstdio> 5 #include<vector> 6 #include<stack> 7 #include<queue> 8 #include<cmath> 9 #include<map> 10 #include<set> 11 using namespace std; 12 typedef long long ll; 13 typedef pair<int,int> pll; 14 const int INF = 0x3f3f3f3f; 15 const int maxn = 300000+5; 16 17 int n; 18 int p[maxn]; 19 int a[maxn]; 20 vector<int> ans; 21 22 int main() 23 { 24 //freopen("in.txt","r",stdin); 25 while(~scanf("%d",&n)) 26 { 27 ans.clear(); 28 memset(a,0,sizeof(a)); 29 for(int i=1;i<=n;i++) scanf("%d",&p[i]); 30 int max_right=n; 31 int cnt=0; 32 for(int i=1;i<=n;i++) 33 { 34 a[p[i]]=1; 35 if(p[i]==max_right) 36 { 37 for(int j=max_right-1;j>=1;j--) 38 { 39 if(!a[j]) {max_right=j;break;} 40 else cnt--; 41 } 42 } 43 else 44 { 45 cnt++; 46 } 47 ans.push_back(cnt+1); 48 } 49 printf("1"); 50 for(int i=0;i<ans.size();i++) 51 printf(" %d",ans[i]); 52 printf("\n"); 53 } 54 return 0; 55 }?
轉載于:https://www.cnblogs.com/zyb993963526/p/7679291.html
總結
以上是生活随笔為你收集整理的Codeforces Round #441 D. Sorting the Coins(模拟)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: [SNS]Mixi
- 下一篇: ora--12154 :TNS :cou