Codeforces Round #441 D. Sorting the Coins(模拟)

本文解析了CodeForces平台上的876D题目,该题要求计算给定序列达到稳定状态所需的步骤数。通过模拟操作过程,实现了一种有效的算法。

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值