#440 Div.2 D.Sorting the Coins 硬币排序问题
来源:互联网 发布:windows loader cw 编辑:程序博客网 时间:2024/06/01 10:36
原题链接:
sort the coins
大意:
一排硬币排成一排,有两种硬币,把其中一种按照两两交换的规则归到右侧,求需要归并多少次才能把所有这种硬币归到右侧。
分析:
由于硬币是按照时间顺序放的,容易想到第n个答案和n-1个答案必然存在某种关系。
找规律可以发现:
每次放置一个新的点,答案都比前一个多一,当这个点放在末尾时,相当于回退一个。
每次移动后 每个硬币就是在它的下一个非法硬币的前一个位置
算法每运行一次就是一枚非法硬币归位
具体实现:
#include <bits/stdc++.h>using namespace std;typedef long long ll;typedef pair <int,int> pii;#define mem(s,t) memset(s,t,sizeof(s))#define D(v) cout<<#v<<" "<<v<<endl#define inf 0x3f3f3f3f#define pb push_back//#define LOCALconst int mod=1e9+7;const int MAXN =3e5+10;int vis[MAXN],ret[MAXN];int main() {#ifdef LOCAL freopen("in.txt","r",stdin); freopen("out.txt","w",stdout);#endif int n; scanf("%d",&n); mem(vis,0); mem(ret,0); int cnt=0,last=n; ret[0]=1; for(int i=1;i<=n;i++){ cnt++; int x; scanf("%d",&x); vis[x]++; if(x==last) while(vis[last]){ cnt--; last--; } ret[i]=cnt+1; } for(int i=0;i<=n;i++) printf("%d%c",ret[i]," \n"[i==n]); return 0;}
阅读全文
0 0
- #440 Div.2 D.Sorting the Coins 硬币排序问题
- Codeforces Round #441 (Div. 2, by Moscow Team Olympiad) D. Sorting the Coins 乱搞
- 【反序表+树状数组】Codeforces Round #441(Div.2)D[Sorting the Coins]题解
- Codeforces Round #441 (Div. 2, by Moscow Team Olympiad) D. Sorting the Coins
- Codeforces Round #441 (Div. 2, by Moscow Team Olympiad) D. Sorting the Coins【规律】
- CF D. Sorting the Coins
- CodeForces 876D Sorting the Coins
- Codeforces 876 D. Sorting the Coins 模拟
- Codeforces Round #441 D. Sorting the Coins
- codeforces 876D Sorting the Coins
- Codeforces Round #441 D. Sorting the Coins
- [冒泡 反序表] Codeforces #876D. Sorting the Coins
- Codeforces Round #441 D. Sorting the Coins(线段树)
- Codeforces875B-Sorting the Coins
- 【Codeforces875B】Sorting the Coins
- Codeforces 875B Sorting the Coins
- Codeforces 875B Sorting the Coins 题解
- 洛谷 P2851 [USACO06DEC]最少的硬币The Fewest Coins
- Java用graphics和鼠标监听写了个象棋程序,简单线下对弈版
- c++教程——循环
- 在js中arguments.length的用法和意义
- 平方根
- Kali Linux桥接模式配置DNS服务器
- #440 Div.2 D.Sorting the Coins 硬币排序问题
- [caioj1441][主席树]第K小的数
- 构建高性能的ASP.NET应用(13)
- 第七周——项目4—“队列数组”
- unity中资源动态加载的方式
- K:eclipse导入github的maven项目
- Android 集成ShareSDK实现三方登录
- 什么?response.end在mvc中无效????
- java-常用的实体类